diff --git a/packages/kit/src/resolve.ts b/packages/kit/src/resolve.ts index 31ae08d2ed..2a7997e1d0 100644 --- a/packages/kit/src/resolve.ts +++ b/packages/kit/src/resolve.ts @@ -169,15 +169,16 @@ export function createResolver (base: string | URL): Resolver { } export async function resolveNuxtModule (base: string, paths: string[]): Promise { - const resolved: string[] = [] + const resolved: string[] = new Array(paths.length) const resolver = createResolver(base) - for (const path of paths) { + for (let i = 0; i < paths.length; i++) { + const path = paths[i]! if (path.startsWith(base)) { - resolved.push(path.split('/index.ts')[0]!) + resolved[i] = path.split('/index.ts')[0]! } else { const resolvedPath = await resolver.resolvePath(path) - resolved.push(resolvedPath.slice(0, resolvedPath.lastIndexOf(path) + path.length)) + resolved[i] = resolvedPath.slice(0, resolvedPath.lastIndexOf(path) + path.length) } } diff --git a/packages/nuxt/src/components/templates.ts b/packages/nuxt/src/components/templates.ts index 96e0a6f4fd..24b4482e87 100644 --- a/packages/nuxt/src/components/templates.ts +++ b/packages/nuxt/src/components/templates.ts @@ -122,7 +122,7 @@ export const componentsTypeTemplate = { const globalSyncComponentsExport: string[] = new Array(componentTypes.length) const globalAsyncComponentsExport: string[] = new Array(componentTypes.length) for (let i = 0; i < componentTypes.length; i++) { - const [pascalName, type] = componentTypes[i] + const [pascalName, type] = componentTypes[i]! globalSyncComponents[i] = ` '${pascalName}': ${type}` globalAsyncComponents[i] = ` 'Lazy${pascalName}': ${type}` globalSyncComponentsExport[i] = `export const ${pascalName}: ${type}` diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index d41688462a..2aa9d1e13c 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -253,7 +253,7 @@ const IS_TSX = /\.[jt]sx$/ export async function annotatePlugins (nuxt: Nuxt, plugins: NuxtPlugin[]) { const _plugins: Array> = new Array(plugins.length) for (let i = 0; i < plugins.length; i++) { - const plugin = plugins[i] + const plugin = plugins[i]! try { const code = plugin.src in nuxt.vfs ? nuxt.vfs[plugin.src]! : await fsp.readFile(plugin.src!, 'utf-8') _plugins[i] = { diff --git a/packages/nuxt/src/core/plugins/import-protection.ts b/packages/nuxt/src/core/plugins/import-protection.ts index 0e6e7fa999..f4c2fb8678 100644 --- a/packages/nuxt/src/core/plugins/import-protection.ts +++ b/packages/nuxt/src/core/plugins/import-protection.ts @@ -10,42 +10,43 @@ interface ImportProtectionOptions { } export const nuxtImportProtections = (nuxt: { options: NuxtOptions }, options: { isNitro?: boolean } = {}) => { - const patterns: ImportProtectionOptions['patterns'] = [] - - patterns.push([ + const string_modules = nuxt.options.modules.filter(m => typeof m === 'string') + const other_pkgs = [/(^|node_modules\/)@nuxt\/(kit|test-utils)/, /(^|node_modules\/)nuxi/, /(^|node_modules\/)nitro(?:pack)?(?:-nightly)?(?:$|\/)(?!(?:dist\/)?runtime|types)/, /(^|node_modules\/)nuxt\/(config|kit|schema)/] + const patterns: ImportProtectionOptions['patterns'] = new Array(4 + string_modules.length + other_pkgs.length) + patterns[0] = [ /^(nuxt|nuxt3|nuxt-nightly)$/, '`nuxt`, `nuxt3` or `nuxt-nightly` cannot be imported directly.' + (options.isNitro ? '' : ' Instead, import runtime Nuxt composables from `#app` or `#imports`.'), - ]) + ] - patterns.push([ + patterns[1] = [ /^((~|~~|@|@@)?\/)?nuxt\.config(\.|$)/, 'Importing directly from a `nuxt.config` file is not allowed. Instead, use runtime config or a module.', - ]) + ] - patterns.push([/(^|node_modules\/)@vue\/composition-api/]) - - for (const mod of nuxt.options.modules.filter(m => typeof m === 'string')) { - patterns.push([ + patterns[2] = [/(^|node_modules\/)@vue\/composition-api/] + let count = 3 + for (const mod of string_modules) { + patterns[count++] = [ new RegExp(`^${escapeRE(mod as string)}$`), 'Importing directly from module entry-points is not allowed.', - ]) + ] } - for (const i of [/(^|node_modules\/)@nuxt\/(kit|test-utils)/, /(^|node_modules\/)nuxi/, /(^|node_modules\/)nitro(?:pack)?(?:-nightly)?(?:$|\/)(?!(?:dist\/)?runtime|types)/, /(^|node_modules\/)nuxt\/(config|kit|schema)/]) { - patterns.push([i, 'This module cannot be imported' + (options.isNitro ? ' in server runtime.' : ' in the Vue part of your app.')]) + for (const i of other_pkgs) { + patterns[count++] = [i, 'This module cannot be imported' + (options.isNitro ? ' in server runtime.' : ' in the Vue part of your app.')] } if (options.isNitro) { - for (const i of ['#app', /^#build(\/|$)/]) { - patterns.push([i, 'Vue app aliases are not allowed in server runtime.']) - } + patterns[count++] = ['#app', 'Vue app aliases are not allowed in server runtime.'] + // This is pushed because it surpasses the max known length of the array + patterns.push([/^#build(\/|$)/, 'Vue app aliases are not allowed in server runtime.']) } if (!options.isNitro) { - patterns.push([ + patterns[count++] = [ new RegExp(escapeRE(relative(nuxt.options.srcDir, resolve(nuxt.options.srcDir, nuxt.options.serverDir || 'server'))) + '\\/(api|routes|middleware|plugins)\\/'), 'Importing from server is not allowed in the Vue part of your app.', - ]) + ] } return patterns diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index 9e9566c41d..1976b231c7 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -62,13 +62,14 @@ export const clientPluginTemplate: NuxtTemplate = { async getContents (ctx) { const clientPlugins = await annotatePlugins(ctx.nuxt, ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server')) checkForCircularDependencies(clientPlugins) - const exports: string[] = [] - const imports: string[] = [] - for (const plugin of clientPlugins) { + const exports: string[] = new Array(clientPlugins.length) + const imports: string[] = new Array(clientPlugins.length) + for (let i = 0; i < clientPlugins.length; i++) { + const plugin = clientPlugins[i]! const path = relative(ctx.nuxt.options.rootDir, plugin.src) const variable = genSafeVariableName(filename(plugin.src)).replace(/_(45|46|47)/g, '_') + '_' + hash(path) - exports.push(variable) - imports.push(genImport(plugin.src, variable)) + exports[i] = variable + imports[i] = genImport(plugin.src, variable) } return [ ...imports, @@ -82,13 +83,14 @@ export const serverPluginTemplate: NuxtTemplate = { async getContents (ctx) { const serverPlugins = await annotatePlugins(ctx.nuxt, ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client')) checkForCircularDependencies(serverPlugins) - const exports: string[] = [] - const imports: string[] = [] - for (const plugin of serverPlugins) { + const exports: string[] = new Array(serverPlugins.length) + const imports: string[] = new Array(serverPlugins.length) + for (let i = 0; i < serverPlugins.length; i++) { + const plugin = serverPlugins[i]! const path = relative(ctx.nuxt.options.rootDir, plugin.src) const variable = genSafeVariableName(filename(path)).replace(/_(45|46|47)/g, '_') + '_' + hash(path) - exports.push(variable) - imports.push(genImport(plugin.src, variable)) + exports[i] = variable + imports[i] = genImport(plugin.src, variable) } return [ ...imports, diff --git a/packages/vite/src/plugins/analyze.ts b/packages/vite/src/plugins/analyze.ts index d5e8e4432e..5ef542d585 100644 --- a/packages/vite/src/plugins/analyze.ts +++ b/packages/vite/src/plugins/analyze.ts @@ -17,12 +17,11 @@ export function analyzePlugin (ctx: ViteBuildContext): Plugin[] { for (const _bundleId in outputBundle) { const bundle = outputBundle[_bundleId] if (!bundle || bundle.type !== 'chunk') { continue } - const minifiedModuleEntryPromises: Array> = [] - for (const [moduleId, module] of Object.entries(bundle.modules)) { - minifiedModuleEntryPromises.push( - transform(module.code || '', { minify: true }) - .then(result => [moduleId, { ...module, code: result.code }]), - ) + const allModules = Object.entries(bundle.modules) + const minifiedModuleEntryPromises: Array> = new Array(allModules.length) + for (let i = 0; i < allModules.length; i++) { + const [moduleId, module] = allModules[i]! + minifiedModuleEntryPromises[i] = transform(module.code || '', { minify: true }).then(result => [moduleId, { ...module, code: result.code }]) } bundle.modules = Object.fromEntries(await Promise.all(minifiedModuleEntryPromises)) }