Skip to content

Commit

Permalink
fix: don't expose full project path throuh langDir option (#1786)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Jan 9, 2023
1 parent 7956539 commit 0738a0f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"@babel/traverse": "^7.18.10",
"@intlify/vue-i18n-extensions": "^1.0.2",
"@intlify/vue-i18n-loader": "^1.1.0",
"@nuxt/utils": "2.x",
"cookie": "^0.5.0",
"devalue": "^2.0.1",
"is-https": "^4.0.0",
Expand Down
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { resolve, join } from 'path'
import { readdirSync } from 'fs'
import merge from 'lodash.merge'
// @ts-ignore
import { relativeTo } from '@nuxt/utils'
// @ts-ignore
import { directive as i18nExtensionsDirective } from '@intlify/vue-i18n-extensions'
import { COMPONENT_OPTIONS_KEY, DEFAULT_OPTIONS, ROOT_DIR, STRATEGIES, REDIRECT_ON_OPTIONS } from './helpers/constants'
import { buildHook, createExtendRoutesHook } from './core/hooks'
Expand All @@ -22,16 +24,26 @@ export default async function (moduleOptions) {
throw new Error(formatMessage('When using the "lazy" option you must also set the "langDir" option.'))
}

/** @type {Record<string, string>} */
const localeFileMap = {}

if (options.langDir) {
/**
* Helper for `langDir` resources resolving
* @param {any[]} args
*/
const relativeToBuild = (...args) => relativeTo(this.options.buildDir, ...args)
const resolvedLangDir = this.nuxt.resolver.resolveAlias(options.langDir)

if (!options.locales.length || typeof options.locales[0] === 'string') {
throw new Error(formatMessage('When using the "langDir" option the "locales" must be a list of objects.'))
}
for (const locale of options.locales) {
if (typeof (locale) === 'string' || !locale.file) {
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)}.`))
}
localeFileMap[String(locale.file)] = relativeToBuild(resolvedLangDir, locale.file)
}
options.langDir = this.nuxt.resolver.resolveAlias(options.langDir)
}

// Templates (including plugins).
Expand Down Expand Up @@ -60,7 +72,8 @@ export default async function (moduleOptions) {
isUniversalMode: nuxtOptions.mode === 'universal',
trailingSlash: nuxtOptions.router.trailingSlash
},
options
options,
localeFiles: JSON.stringify(localeFileMap)
}

const templatesPath = join(__dirname, '/templates')
Expand Down
1 change: 1 addition & 0 deletions src/templates/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export const localeMessages: Record<string, () => Promise<LocaleFileExport>>
export const Constants: ModuleConstants
export const nuxtOptions: ModuleNuxtOptions
export const options: ResolvedOptions
export const localeFiles: string
5 changes: 3 additions & 2 deletions src/templates/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { lazy, locales, langDir, vueI18n } = options.options
const { fallbackLocale } = vueI18n || {}
const syncLocaleFiles = new Set()
const asyncLocaleFiles = new Set()
const localeFiles = JSON.parse(options.localeFiles)

if (langDir) {
if (fallbackLocale && typeof (fallbackLocale) === 'string') {
Expand All @@ -17,7 +18,7 @@ if (langDir) {
}
}
for (const file of syncLocaleFiles) {
%>import locale<%= hash(file) %> from '<%= `../${relativeToBuild(langDir, file)}` %>'
%>import locale<%= hash(file) %> from '<%= `../${localeFiles[file]}` %>'
<%
}
}
Expand Down Expand Up @@ -62,7 +63,7 @@ export const localeMessages = {
<%= `'${file}': () => Promise.resolve(locale${hash(file)}),` %><%
}
for (const file of asyncLocaleFiles) {%>
<%= `'${file}': () => import('../${relativeToBuild(langDir, file)}' /* webpackChunkName: "lang-${file}" */),` %><%
<%= `'${file}': () => import('../${localeFiles[file]}' /* webpackChunkName: "lang-${file}" */),` %><%
}
%>
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,7 @@
"@types/webpack-hot-middleware" "2.25.4"
sass-loader "10.1.1"

"@nuxt/utils@2.15.8":
"@nuxt/utils@2.15.8", "@nuxt/utils@2.x":
version "2.15.8"
resolved "https://registry.yarnpkg.com/@nuxt/utils/-/utils-2.15.8.tgz#0c3594f01be63ab521583904cafd32215b719d4c"
integrity sha512-e0VBarUbPiQ4ZO1T58puoFIuXme7L5gk1QfwyxOONlp2ryE7aRyZ8X/mryuOiIeyP64c4nwSUtN7q9EUWRb7Lg==
Expand Down

0 comments on commit 0738a0f

Please sign in to comment.