mirror of
https://github.com/nuxt/nuxt.git
synced 2025-03-09 03:03:18 +00:00
fix(nuxt): use relative path to generate plugin variables (#6030)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
parent
78618f1f21
commit
d135608ef0
@ -3,6 +3,8 @@ import lodashTemplate from 'lodash.template'
|
|||||||
import { genSafeVariableName, genDynamicImport, genImport } from 'knitwork'
|
import { genSafeVariableName, genDynamicImport, genImport } from 'knitwork'
|
||||||
|
|
||||||
import type { NuxtTemplate } from '@nuxt/schema'
|
import type { NuxtTemplate } from '@nuxt/schema'
|
||||||
|
import { relative } from 'pathe'
|
||||||
|
import { hash } from 'ohash'
|
||||||
|
|
||||||
export async function compileTemplate (template: NuxtTemplate, ctx: any) {
|
export async function compileTemplate (template: NuxtTemplate, ctx: any) {
|
||||||
const data = { ...ctx, options: template.options }
|
const data = { ...ctx, options: template.options }
|
||||||
@ -23,16 +25,25 @@ export async function compileTemplate (template: NuxtTemplate, ctx: any) {
|
|||||||
|
|
||||||
const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"{(.+)}"(?=,?$)/gm, r => JSON.parse(r).replace(/^{(.*)}$/, '$1'))
|
const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"{(.+)}"(?=,?$)/gm, r => JSON.parse(r).replace(/^{(.*)}$/, '$1'))
|
||||||
|
|
||||||
const importSources = (sources: string | string[], { lazy = false } = {}) => {
|
const importSources = (sources: string | string[], root: string, { lazy = false } = {}) => {
|
||||||
if (!Array.isArray(sources)) {
|
if (!Array.isArray(sources)) {
|
||||||
sources = [sources]
|
sources = [sources]
|
||||||
}
|
}
|
||||||
return sources.map((src) => {
|
const exports: string[] = []
|
||||||
if (lazy) {
|
const imports: string[] = []
|
||||||
return `const ${genSafeVariableName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`
|
for (const src of sources) {
|
||||||
}
|
const path = relative(root, src)
|
||||||
return genImport(src, genSafeVariableName(src))
|
const variable = genSafeVariableName(path).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
||||||
}).join('\n')
|
exports.push(variable)
|
||||||
|
imports.push(lazy
|
||||||
|
? `const ${variable} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`
|
||||||
|
: genImport(src, variable)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
exports,
|
||||||
|
imports
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const templateUtils = { serialize, importName: genSafeVariableName, importSources }
|
export const templateUtils = { serialize, importName: genSafeVariableName, importSources }
|
||||||
|
@ -48,9 +48,11 @@ export const clientPluginTemplate = {
|
|||||||
filename: 'plugins/client.mjs',
|
filename: 'plugins/client.mjs',
|
||||||
getContents (ctx: TemplateContext) {
|
getContents (ctx: TemplateContext) {
|
||||||
const clientPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server')
|
const clientPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server')
|
||||||
|
const rootDir = ctx.nuxt.options.rootDir
|
||||||
|
const { imports, exports } = templateUtils.importSources(clientPlugins.map(p => p.src), rootDir)
|
||||||
return [
|
return [
|
||||||
templateUtils.importSources(clientPlugins.map(p => p.src)),
|
...imports,
|
||||||
`export default ${genArrayFromRaw(clientPlugins.map(p => genSafeVariableName(p.src)))}`
|
`export default ${genArrayFromRaw(exports)}`
|
||||||
].join('\n')
|
].join('\n')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,12 +61,14 @@ export const serverPluginTemplate = {
|
|||||||
filename: 'plugins/server.mjs',
|
filename: 'plugins/server.mjs',
|
||||||
getContents (ctx: TemplateContext) {
|
getContents (ctx: TemplateContext) {
|
||||||
const serverPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client')
|
const serverPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client')
|
||||||
|
const rootDir = ctx.nuxt.options.rootDir
|
||||||
|
const { imports, exports } = templateUtils.importSources(serverPlugins.map(p => p.src), rootDir)
|
||||||
return [
|
return [
|
||||||
"import preload from '#app/plugins/preload.server'",
|
"import preload from '#app/plugins/preload.server'",
|
||||||
templateUtils.importSources(serverPlugins.map(p => p.src)),
|
...imports,
|
||||||
`export default ${genArrayFromRaw([
|
`export default ${genArrayFromRaw([
|
||||||
'preload',
|
'preload',
|
||||||
...serverPlugins.map(p => genSafeVariableName(p.src))
|
...exports
|
||||||
])}`
|
])}`
|
||||||
].join('\n')
|
].join('\n')
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user