mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-20 15:41:11 +00:00
perf: more pre-allocated arrays
This commit is contained in:
parent
ad029a6693
commit
ca678867d6
@ -169,15 +169,16 @@ export function createResolver (base: string | URL): Resolver {
|
||||
}
|
||||
|
||||
export async function resolveNuxtModule (base: string, paths: string[]): Promise<string[]> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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}`
|
||||
|
@ -253,7 +253,7 @@ const IS_TSX = /\.[jt]sx$/
|
||||
export async function annotatePlugins (nuxt: Nuxt, plugins: NuxtPlugin[]) {
|
||||
const _plugins: Array<NuxtPlugin & Omit<PluginMeta, 'enforce'>> = 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] = {
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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<Promise<[string, RenderedModule]>> = []
|
||||
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<Promise<[string, RenderedModule]>> = 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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user