mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 07:32:01 +00:00
fix(kit): move relative path handling back into nuxt templates (#6430)
This commit is contained in:
parent
82f2740ab5
commit
e3b0608470
@ -24,7 +24,6 @@
|
|||||||
"knitwork": "^0.1.2",
|
"knitwork": "^0.1.2",
|
||||||
"lodash.template": "^4.5.0",
|
"lodash.template": "^4.5.0",
|
||||||
"mlly": "^0.5.7",
|
"mlly": "^0.5.7",
|
||||||
"ohash": "^0.1.5",
|
|
||||||
"pathe": "^0.3.3",
|
"pathe": "^0.3.3",
|
||||||
"pkg-types": "^0.3.3",
|
"pkg-types": "^0.3.3",
|
||||||
"scule": "^0.3.2",
|
"scule": "^0.3.2",
|
||||||
|
@ -3,8 +3,6 @@ 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,27 +21,24 @@ export async function compileTemplate (template: NuxtTemplate, ctx: any) {
|
|||||||
throw new Error('Invalid template: ' + JSON.stringify(template))
|
throw new Error('Invalid template: ' + JSON.stringify(template))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
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[], root: string, { lazy = false } = {}) => {
|
/** @deprecated */
|
||||||
|
const importSources = (sources: string | string[], { lazy = false } = {}) => {
|
||||||
if (!Array.isArray(sources)) {
|
if (!Array.isArray(sources)) {
|
||||||
sources = [sources]
|
sources = [sources]
|
||||||
}
|
}
|
||||||
const exports: string[] = []
|
return sources.map((src) => {
|
||||||
const imports: string[] = []
|
if (lazy) {
|
||||||
for (const src of sources) {
|
return `const ${genSafeVariableName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`
|
||||||
const path = relative(root, src)
|
}
|
||||||
const variable = genSafeVariableName(path).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
return genImport(src, genSafeVariableName(src))
|
||||||
exports.push(variable)
|
}).join('\n')
|
||||||
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 }
|
/** @deprecated */
|
||||||
|
const importName = genSafeVariableName
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
|
export const templateUtils = { serialize, importName, importSources }
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import { templateUtils } from '@nuxt/kit'
|
|
||||||
import type { Nuxt, NuxtApp, NuxtTemplate } from '@nuxt/schema'
|
import type { Nuxt, NuxtApp, NuxtTemplate } from '@nuxt/schema'
|
||||||
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString, genSafeVariableName } from 'knitwork'
|
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString, genSafeVariableName } from 'knitwork'
|
||||||
|
|
||||||
import { isAbsolute, join, relative } from 'pathe'
|
import { isAbsolute, join, relative } from 'pathe'
|
||||||
import { resolveSchema, generateTypes } from 'untyped'
|
import { resolveSchema, generateTypes } from 'untyped'
|
||||||
import escapeRE from 'escape-string-regexp'
|
import escapeRE from 'escape-string-regexp'
|
||||||
|
import { hash } from 'ohash'
|
||||||
|
|
||||||
export interface TemplateContext {
|
export interface TemplateContext {
|
||||||
nuxt: Nuxt
|
nuxt: Nuxt
|
||||||
@ -48,8 +48,14 @@ 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 exports: string[] = []
|
||||||
const { imports, exports } = templateUtils.importSources(clientPlugins.map(p => p.src), rootDir)
|
const imports: string[] = []
|
||||||
|
for (const plugin of clientPlugins) {
|
||||||
|
const path = relative(ctx.nuxt.options.rootDir, plugin.src)
|
||||||
|
const variable = genSafeVariableName(path).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
||||||
|
exports.push(variable)
|
||||||
|
imports.push(genImport(plugin.src, variable))
|
||||||
|
}
|
||||||
return [
|
return [
|
||||||
...imports,
|
...imports,
|
||||||
`export default ${genArrayFromRaw(exports)}`
|
`export default ${genArrayFromRaw(exports)}`
|
||||||
@ -61,15 +67,17 @@ 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 exports: string[] = ['preload']
|
||||||
const { imports, exports } = templateUtils.importSources(serverPlugins.map(p => p.src), rootDir)
|
const imports: string[] = ["import preload from '#app/plugins/preload.server'"]
|
||||||
|
for (const plugin of serverPlugins) {
|
||||||
|
const path = relative(ctx.nuxt.options.rootDir, plugin.src)
|
||||||
|
const variable = genSafeVariableName(path).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
||||||
|
exports.push(variable)
|
||||||
|
imports.push(genImport(plugin.src, variable))
|
||||||
|
}
|
||||||
return [
|
return [
|
||||||
"import preload from '#app/plugins/preload.server'",
|
|
||||||
...imports,
|
...imports,
|
||||||
`export default ${genArrayFromRaw([
|
`export default ${genArrayFromRaw(exports)}`
|
||||||
'preload',
|
|
||||||
...exports
|
|
||||||
])}`
|
|
||||||
].join('\n')
|
].join('\n')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user