From c53b6a5f2edeb329ac9fb70d43e889523c17156b Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Tue, 16 Nov 2021 04:20:51 +0900 Subject: [PATCH] refactor(nuxt3): use the `@nuxt/kit` template utilites (#1908) --- packages/nuxt3/src/core/template.utils.ts | 19 ------------------- packages/nuxt3/src/core/templates.ts | 12 +++++------- packages/nuxt3/src/pages/module.ts | 6 +++--- 3 files changed, 8 insertions(+), 29 deletions(-) delete mode 100644 packages/nuxt3/src/core/template.utils.ts diff --git a/packages/nuxt3/src/core/template.utils.ts b/packages/nuxt3/src/core/template.utils.ts deleted file mode 100644 index 082a8573da..0000000000 --- a/packages/nuxt3/src/core/template.utils.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { basename, extname } from 'pathe' -import hash from 'hash-sum' -import { camelCase } from 'scule' - -export const serialize = data => JSON.stringify(data, null, 2).replace(/"{(.+)}"/g, '$1') - -export const importName = (src: string) => `${camelCase(basename(src, extname(src))).replace(/[^a-zA-Z?\d\s:]/g, '')}_${hash(src)}` - -export const importSources = (sources: string | string[], { lazy = false } = {}) => { - if (!Array.isArray(sources)) { - sources = [sources] - } - return sources.map((src) => { - if (lazy) { - return `const ${importName(src)} = () => import('${src}' /* webpackChunkName: '${src}' */)` - } - return `import ${importName(src)} from '${src}'` - }).join('\n') -} diff --git a/packages/nuxt3/src/core/templates.ts b/packages/nuxt3/src/core/templates.ts index d1d7613b3a..c38f277497 100644 --- a/packages/nuxt3/src/core/templates.ts +++ b/packages/nuxt3/src/core/templates.ts @@ -1,8 +1,6 @@ - +import { templateUtils } from '@nuxt/kit' import type { Nuxt, NuxtApp } from '@nuxt/kit' -import { importName, importSources } from './template.utils' - type TemplateContext = { nuxt: Nuxt; app: NuxtApp; @@ -35,9 +33,9 @@ export const clientPluginTemplate = { getContents (ctx: TemplateContext) { const clientPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server') return [ - importSources(clientPlugins.map(p => p.src)), + templateUtils.importSources(clientPlugins.map(p => p.src)), 'export default [', - clientPlugins.map(p => importName(p.src)).join(',\n '), + clientPlugins.map(p => templateUtils.importName(p.src)).join(',\n '), ']' ].join('\n') } @@ -49,10 +47,10 @@ export const serverPluginTemplate = { const serverPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client') return [ "import preload from '#app/plugins/preload.server'", - importSources(serverPlugins.map(p => p.src)), + templateUtils.importSources(serverPlugins.map(p => p.src)), 'export default [', ' preload,', - serverPlugins.map(p => importName(p.src)).join(',\n '), + serverPlugins.map(p => templateUtils.importName(p.src)).join(',\n '), ']' ].join('\n') } diff --git a/packages/nuxt3/src/pages/module.ts b/packages/nuxt3/src/pages/module.ts index f65c668c8a..bb26bceffe 100644 --- a/packages/nuxt3/src/pages/module.ts +++ b/packages/nuxt3/src/pages/module.ts @@ -1,5 +1,5 @@ import { existsSync } from 'fs' -import { defineNuxtModule, addTemplate, addPlugin } from '@nuxt/kit' +import { defineNuxtModule, addTemplate, addPlugin, templateUtils } from '@nuxt/kit' import { resolve } from 'pathe' import { distDir } from '../dirs' import { resolveLayouts, resolvePagesRoutes, addComponentToRoutes } from './utils' @@ -45,7 +45,7 @@ export default defineNuxtModule({ const pages = await resolvePagesRoutes(nuxt) await nuxt.callHook('pages:extend', pages) const serializedRoutes = addComponentToRoutes(pages) - return `export default ${JSON.stringify(serializedRoutes, null, 2).replace(/"{(.+)}"/g, '$1')}` + return `export default ${templateUtils.serialize(serializedRoutes)}` } }) @@ -59,7 +59,7 @@ export default defineNuxtModule({ })) return [ 'import { defineAsyncComponent } from \'vue\'', - `export default ${JSON.stringify(layoutsObject, null, 2).replace(/"{(.+)}"/g, '$1')}` + `export default ${templateUtils.serialize(layoutsObject)}` ].join('\n') } })