Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(nuxt): use relative path to generate plugin variables #6030

Merged
merged 4 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 17 additions & 7 deletions packages/kit/src/internal/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import lodashTemplate from 'lodash.template'
import { genSafeVariableName, genDynamicImport, genImport } from 'knitwork'

import type { NuxtTemplate } from '@nuxt/schema'
import { relative } from 'pathe'

export async function compileTemplate (template: NuxtTemplate, ctx: any) {
const data = { ...ctx, options: template.options }
Expand All @@ -23,16 +24,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 importSources = (sources: string | string[], { lazy = false } = {}) => {
const importSources = (sources: string | string[], root: string, { lazy = false } = {}) => {
if (!Array.isArray(sources)) {
sources = [sources]
}
return sources.map((src) => {
if (lazy) {
return `const ${genSafeVariableName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`
}
return genImport(src, genSafeVariableName(src))
}).join('\n')
const variables: string[] = []
const imports: string[] = []
sources.forEach((src) => {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
const path = relative(root, src)
const variable = genSafeVariableName(path)
pi0 marked this conversation as resolved.
Show resolved Hide resolved
variables.push(variable)
imports.push(lazy
? `const ${variable} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`
: genImport(src, variable)
)
})
return {
variables,
imports
}
}

export const templateUtils = { serialize, importName: genSafeVariableName, importSources }
12 changes: 8 additions & 4 deletions packages/nuxt/src/core/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ export const clientPluginTemplate = {
filename: 'plugins/client.mjs',
getContents (ctx: TemplateContext) {
const clientPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server')
const rootDir = ctx.nuxt.options.rootDir
const { variables, imports } = templateUtils.importSources(clientPlugins.map(p => p.src), rootDir)
return [
templateUtils.importSources(clientPlugins.map(p => p.src)),
`export default ${genArrayFromRaw(clientPlugins.map(p => genSafeVariableName(p.src)))}`
...imports,
`export default ${genArrayFromRaw(variables)}`
].join('\n')
}
}
Expand All @@ -59,12 +61,14 @@ export const serverPluginTemplate = {
filename: 'plugins/server.mjs',
getContents (ctx: TemplateContext) {
const serverPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client')
const rootDir = ctx.nuxt.options.rootDir
const { variables, imports } = templateUtils.importSources(serverPlugins.map(p => p.src), rootDir)
return [
"import preload from '#app/plugins/preload.server'",
templateUtils.importSources(serverPlugins.map(p => p.src)),
...imports,
`export default ${genArrayFromRaw([
'preload',
...serverPlugins.map(p => genSafeVariableName(p.src))
...variables
])}`
].join('\n')
}
Expand Down