From e4d9614eab91e735a5269e02d8013c089b5381ec Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 11:20:35 +0100 Subject: [PATCH 1/2] fix(kit): move relative path handling back into nuxt templates This reverts commit d135608ef0d607259c0ae6f5156c8f46ff78ddc3. --- packages/kit/package.json | 1 - packages/kit/src/internal/template.ts | 25 +++++++------------------ packages/nuxt/src/core/templates.ts | 17 +++++++++++------ yarn.lock | 1 - 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/packages/kit/package.json b/packages/kit/package.json index 3f642272f68..3d387795d9d 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -24,7 +24,6 @@ "knitwork": "^0.1.2", "lodash.template": "^4.5.0", "mlly": "^0.5.7", - "ohash": "^0.1.5", "pathe": "^0.3.3", "pkg-types": "^0.3.3", "scule": "^0.3.2", diff --git a/packages/kit/src/internal/template.ts b/packages/kit/src/internal/template.ts index 9be902eb7ec..86b97d74834 100644 --- a/packages/kit/src/internal/template.ts +++ b/packages/kit/src/internal/template.ts @@ -3,8 +3,6 @@ import lodashTemplate from 'lodash.template' import { genSafeVariableName, genDynamicImport, genImport } from 'knitwork' import type { NuxtTemplate } from '@nuxt/schema' -import { relative } from 'pathe' -import { hash } from 'ohash' export async function compileTemplate (template: NuxtTemplate, ctx: any) { const data = { ...ctx, options: template.options } @@ -25,25 +23,16 @@ 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[], root: string, { lazy = false } = {}) => { +const importSources = (sources: string | string[], { lazy = false } = {}) => { if (!Array.isArray(sources)) { sources = [sources] } - const exports: string[] = [] - const imports: string[] = [] - for (const src of sources) { - const path = relative(root, src) - const variable = genSafeVariableName(path).replace(/_(45|46|47)/g, '_') + '_' + hash(path) - exports.push(variable) - imports.push(lazy - ? `const ${variable} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}` - : genImport(src, variable) - ) - } - return { - exports, - imports - } + return sources.map((src) => { + if (lazy) { + return `const ${genSafeVariableName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}` + } + return genImport(src, genSafeVariableName(src)) + }).join('\n') } export const templateUtils = { serialize, importName: genSafeVariableName, importSources } diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index 57299a0a6f0..e423403f99f 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -5,6 +5,7 @@ import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromR import { isAbsolute, join, relative } from 'pathe' import { resolveSchema, generateTypes } from 'untyped' import escapeRE from 'escape-string-regexp' +import { hash } from 'ohash' export interface TemplateContext { nuxt: Nuxt @@ -48,8 +49,14 @@ 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 { imports, exports } = templateUtils.importSources(clientPlugins.map(p => p.src), rootDir) + const exports: string[] = [] + 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 [ ...imports, `export default ${genArrayFromRaw(exports)}` @@ -61,14 +68,12 @@ 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 { imports, exports } = templateUtils.importSources(serverPlugins.map(p => p.src), rootDir) return [ "import preload from '#app/plugins/preload.server'", - ...imports, + templateUtils.importSources(serverPlugins.map(p => p.src)), `export default ${genArrayFromRaw([ 'preload', - ...exports + ...serverPlugins.map(p => genSafeVariableName(p.src)) ])}` ].join('\n') } diff --git a/yarn.lock b/yarn.lock index 9a5fb361680..bb2252e1b9a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1736,7 +1736,6 @@ __metadata: knitwork: ^0.1.2 lodash.template: ^4.5.0 mlly: ^0.5.7 - ohash: ^0.1.5 pathe: ^0.3.3 pkg-types: ^0.3.3 scule: ^0.3.2 From 05e568314746361d0f24f61ea85a2708c94d5770 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 11:25:14 +0100 Subject: [PATCH 2/2] fix: deprecate template utils and update server plugins too --- packages/kit/src/internal/template.ts | 8 +++++++- packages/nuxt/src/core/templates.ts | 17 ++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/kit/src/internal/template.ts b/packages/kit/src/internal/template.ts index 86b97d74834..19115415154 100644 --- a/packages/kit/src/internal/template.ts +++ b/packages/kit/src/internal/template.ts @@ -21,8 +21,10 @@ export async function compileTemplate (template: NuxtTemplate, ctx: any) { 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')) +/** @deprecated */ const importSources = (sources: string | string[], { lazy = false } = {}) => { if (!Array.isArray(sources)) { sources = [sources] @@ -35,4 +37,8 @@ const importSources = (sources: string | string[], { lazy = false } = {}) => { }).join('\n') } -export const templateUtils = { serialize, importName: genSafeVariableName, importSources } +/** @deprecated */ +const importName = genSafeVariableName + +/** @deprecated */ +export const templateUtils = { serialize, importName, importSources } diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index e423403f99f..b4347549a3d 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -1,4 +1,3 @@ -import { templateUtils } from '@nuxt/kit' import type { Nuxt, NuxtApp, NuxtTemplate } from '@nuxt/schema' import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString, genSafeVariableName } from 'knitwork' @@ -68,13 +67,17 @@ export const serverPluginTemplate = { filename: 'plugins/server.mjs', getContents (ctx: TemplateContext) { const serverPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client') + const exports: string[] = ['preload'] + 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 [ - "import preload from '#app/plugins/preload.server'", - templateUtils.importSources(serverPlugins.map(p => p.src)), - `export default ${genArrayFromRaw([ - 'preload', - ...serverPlugins.map(p => genSafeVariableName(p.src)) - ])}` + ...imports, + `export default ${genArrayFromRaw(exports)}` ].join('\n') } }