Skip to content

Commit 0738a0f

Browse files
authored
fix: don't expose full project path throuh langDir option (#1786)
1 parent 7956539 commit 0738a0f

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"@babel/traverse": "^7.18.10",
103103
"@intlify/vue-i18n-extensions": "^1.0.2",
104104
"@intlify/vue-i18n-loader": "^1.1.0",
105+
"@nuxt/utils": "2.x",
105106
"cookie": "^0.5.0",
106107
"devalue": "^2.0.1",
107108
"is-https": "^4.0.0",

src/index.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { resolve, join } from 'path'
22
import { readdirSync } from 'fs'
33
import merge from 'lodash.merge'
44
// @ts-ignore
5+
import { relativeTo } from '@nuxt/utils'
6+
// @ts-ignore
57
import { directive as i18nExtensionsDirective } from '@intlify/vue-i18n-extensions'
68
import { COMPONENT_OPTIONS_KEY, DEFAULT_OPTIONS, ROOT_DIR, STRATEGIES, REDIRECT_ON_OPTIONS } from './helpers/constants'
79
import { buildHook, createExtendRoutesHook } from './core/hooks'
@@ -22,16 +24,26 @@ export default async function (moduleOptions) {
2224
throw new Error(formatMessage('When using the "lazy" option you must also set the "langDir" option.'))
2325
}
2426

27+
/** @type {Record<string, string>} */
28+
const localeFileMap = {}
29+
2530
if (options.langDir) {
31+
/**
32+
* Helper for `langDir` resources resolving
33+
* @param {any[]} args
34+
*/
35+
const relativeToBuild = (...args) => relativeTo(this.options.buildDir, ...args)
36+
const resolvedLangDir = this.nuxt.resolver.resolveAlias(options.langDir)
37+
2638
if (!options.locales.length || typeof options.locales[0] === 'string') {
2739
throw new Error(formatMessage('When using the "langDir" option the "locales" must be a list of objects.'))
2840
}
2941
for (const locale of options.locales) {
3042
if (typeof (locale) === 'string' || !locale.file) {
3143
throw new Error(formatMessage(`All locales must be objects and have the "file" property set when using "langDir".\nFound none in:\n${JSON.stringify(locale, null, 2)}.`))
3244
}
45+
localeFileMap[String(locale.file)] = relativeToBuild(resolvedLangDir, locale.file)
3346
}
34-
options.langDir = this.nuxt.resolver.resolveAlias(options.langDir)
3547
}
3648

3749
// Templates (including plugins).
@@ -60,7 +72,8 @@ export default async function (moduleOptions) {
6072
isUniversalMode: nuxtOptions.mode === 'universal',
6173
trailingSlash: nuxtOptions.router.trailingSlash
6274
},
63-
options
75+
options,
76+
localeFiles: JSON.stringify(localeFileMap)
6477
}
6578

6679
const templatesPath = join(__dirname, '/templates')

src/templates/options.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export const localeMessages: Record<string, () => Promise<LocaleFileExport>>
1818
export const Constants: ModuleConstants
1919
export const nuxtOptions: ModuleNuxtOptions
2020
export const options: ResolvedOptions
21+
export const localeFiles: string

src/templates/options.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { lazy, locales, langDir, vueI18n } = options.options
33
const { fallbackLocale } = vueI18n || {}
44
const syncLocaleFiles = new Set()
55
const asyncLocaleFiles = new Set()
6+
const localeFiles = JSON.parse(options.localeFiles)
67

78
if (langDir) {
89
if (fallbackLocale && typeof (fallbackLocale) === 'string') {
@@ -17,7 +18,7 @@ if (langDir) {
1718
}
1819
}
1920
for (const file of syncLocaleFiles) {
20-
%>import locale<%= hash(file) %> from '<%= `../${relativeToBuild(langDir, file)}` %>'
21+
%>import locale<%= hash(file) %> from '<%= `../${localeFiles[file]}` %>'
2122
<%
2223
}
2324
}
@@ -62,7 +63,7 @@ export const localeMessages = {
6263
<%= `'${file}': () => Promise.resolve(locale${hash(file)}),` %><%
6364
}
6465
for (const file of asyncLocaleFiles) {%>
65-
<%= `'${file}': () => import('../${relativeToBuild(langDir, file)}' /* webpackChunkName: "lang-${file}" */),` %><%
66+
<%= `'${file}': () => import('../${localeFiles[file]}' /* webpackChunkName: "lang-${file}" */),` %><%
6667
}
6768
%>
6869
}

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,7 @@
17521752
"@types/webpack-hot-middleware" "2.25.4"
17531753
sass-loader "10.1.1"
17541754

1755-
"@nuxt/utils@2.15.8":
1755+
"@nuxt/utils@2.15.8", "@nuxt/utils@2.x":
17561756
version "2.15.8"
17571757
resolved "https://registry.yarnpkg.com/@nuxt/utils/-/utils-2.15.8.tgz#0c3594f01be63ab521583904cafd32215b719d4c"
17581758
integrity sha512-e0VBarUbPiQ4ZO1T58puoFIuXme7L5gk1QfwyxOONlp2ryE7aRyZ8X/mryuOiIeyP64c4nwSUtN7q9EUWRb7Lg==

0 commit comments

Comments
 (0)