mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-21 16:09:52 +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[]> {
|
export async function resolveNuxtModule (base: string, paths: string[]): Promise<string[]> {
|
||||||
const resolved: string[] = []
|
const resolved: string[] = new Array(paths.length)
|
||||||
const resolver = createResolver(base)
|
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)) {
|
if (path.startsWith(base)) {
|
||||||
resolved.push(path.split('/index.ts')[0]!)
|
resolved[i] = path.split('/index.ts')[0]!
|
||||||
} else {
|
} else {
|
||||||
const resolvedPath = await resolver.resolvePath(path)
|
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 globalSyncComponentsExport: string[] = new Array(componentTypes.length)
|
||||||
const globalAsyncComponentsExport: string[] = new Array(componentTypes.length)
|
const globalAsyncComponentsExport: string[] = new Array(componentTypes.length)
|
||||||
for (let i = 0; i < componentTypes.length; i++) {
|
for (let i = 0; i < componentTypes.length; i++) {
|
||||||
const [pascalName, type] = componentTypes[i]
|
const [pascalName, type] = componentTypes[i]!
|
||||||
globalSyncComponents[i] = ` '${pascalName}': ${type}`
|
globalSyncComponents[i] = ` '${pascalName}': ${type}`
|
||||||
globalAsyncComponents[i] = ` 'Lazy${pascalName}': ${type}`
|
globalAsyncComponents[i] = ` 'Lazy${pascalName}': ${type}`
|
||||||
globalSyncComponentsExport[i] = `export const ${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[]) {
|
export async function annotatePlugins (nuxt: Nuxt, plugins: NuxtPlugin[]) {
|
||||||
const _plugins: Array<NuxtPlugin & Omit<PluginMeta, 'enforce'>> = new Array(plugins.length)
|
const _plugins: Array<NuxtPlugin & Omit<PluginMeta, 'enforce'>> = new Array(plugins.length)
|
||||||
for (let i = 0; i < plugins.length; i++) {
|
for (let i = 0; i < plugins.length; i++) {
|
||||||
const plugin = plugins[i]
|
const plugin = plugins[i]!
|
||||||
try {
|
try {
|
||||||
const code = plugin.src in nuxt.vfs ? nuxt.vfs[plugin.src]! : await fsp.readFile(plugin.src!, 'utf-8')
|
const code = plugin.src in nuxt.vfs ? nuxt.vfs[plugin.src]! : await fsp.readFile(plugin.src!, 'utf-8')
|
||||||
_plugins[i] = {
|
_plugins[i] = {
|
||||||
|
@ -10,42 +10,43 @@ interface ImportProtectionOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const nuxtImportProtections = (nuxt: { options: NuxtOptions }, options: { isNitro?: boolean } = {}) => {
|
export const nuxtImportProtections = (nuxt: { options: NuxtOptions }, options: { isNitro?: boolean } = {}) => {
|
||||||
const patterns: ImportProtectionOptions['patterns'] = []
|
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)/]
|
||||||
patterns.push([
|
const patterns: ImportProtectionOptions['patterns'] = new Array(4 + string_modules.length + other_pkgs.length)
|
||||||
|
patterns[0] = [
|
||||||
/^(nuxt|nuxt3|nuxt-nightly)$/,
|
/^(nuxt|nuxt3|nuxt-nightly)$/,
|
||||||
'`nuxt`, `nuxt3` or `nuxt-nightly` cannot be imported directly.' + (options.isNitro ? '' : ' Instead, import runtime Nuxt composables from `#app` or `#imports`.'),
|
'`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(\.|$)/,
|
/^((~|~~|@|@@)?\/)?nuxt\.config(\.|$)/,
|
||||||
'Importing directly from a `nuxt.config` file is not allowed. Instead, use runtime config or a module.',
|
'Importing directly from a `nuxt.config` file is not allowed. Instead, use runtime config or a module.',
|
||||||
])
|
]
|
||||||
|
|
||||||
patterns.push([/(^|node_modules\/)@vue\/composition-api/])
|
patterns[2] = [/(^|node_modules\/)@vue\/composition-api/]
|
||||||
|
let count = 3
|
||||||
for (const mod of nuxt.options.modules.filter(m => typeof m === 'string')) {
|
for (const mod of string_modules) {
|
||||||
patterns.push([
|
patterns[count++] = [
|
||||||
new RegExp(`^${escapeRE(mod as string)}$`),
|
new RegExp(`^${escapeRE(mod as string)}$`),
|
||||||
'Importing directly from module entry-points is not allowed.',
|
'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)/]) {
|
for (const i of other_pkgs) {
|
||||||
patterns.push([i, 'This module cannot be imported' + (options.isNitro ? ' in server runtime.' : ' in the Vue part of your app.')])
|
patterns[count++] = [i, 'This module cannot be imported' + (options.isNitro ? ' in server runtime.' : ' in the Vue part of your app.')]
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.isNitro) {
|
if (options.isNitro) {
|
||||||
for (const i of ['#app', /^#build(\/|$)/]) {
|
patterns[count++] = ['#app', 'Vue app aliases are not allowed in server runtime.']
|
||||||
patterns.push([i, '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) {
|
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)\\/'),
|
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.',
|
'Importing from server is not allowed in the Vue part of your app.',
|
||||||
])
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
return patterns
|
return patterns
|
||||||
|
@ -62,13 +62,14 @@ export const clientPluginTemplate: NuxtTemplate = {
|
|||||||
async getContents (ctx) {
|
async getContents (ctx) {
|
||||||
const clientPlugins = await annotatePlugins(ctx.nuxt, ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server'))
|
const clientPlugins = await annotatePlugins(ctx.nuxt, ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server'))
|
||||||
checkForCircularDependencies(clientPlugins)
|
checkForCircularDependencies(clientPlugins)
|
||||||
const exports: string[] = []
|
const exports: string[] = new Array(clientPlugins.length)
|
||||||
const imports: string[] = []
|
const imports: string[] = new Array(clientPlugins.length)
|
||||||
for (const plugin of clientPlugins) {
|
for (let i = 0; i < clientPlugins.length; i++) {
|
||||||
|
const plugin = clientPlugins[i]!
|
||||||
const path = relative(ctx.nuxt.options.rootDir, plugin.src)
|
const path = relative(ctx.nuxt.options.rootDir, plugin.src)
|
||||||
const variable = genSafeVariableName(filename(plugin.src)).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
const variable = genSafeVariableName(filename(plugin.src)).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
||||||
exports.push(variable)
|
exports[i] = variable
|
||||||
imports.push(genImport(plugin.src, variable))
|
imports[i] = genImport(plugin.src, variable)
|
||||||
}
|
}
|
||||||
return [
|
return [
|
||||||
...imports,
|
...imports,
|
||||||
@ -82,13 +83,14 @@ export const serverPluginTemplate: NuxtTemplate = {
|
|||||||
async getContents (ctx) {
|
async getContents (ctx) {
|
||||||
const serverPlugins = await annotatePlugins(ctx.nuxt, ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client'))
|
const serverPlugins = await annotatePlugins(ctx.nuxt, ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client'))
|
||||||
checkForCircularDependencies(serverPlugins)
|
checkForCircularDependencies(serverPlugins)
|
||||||
const exports: string[] = []
|
const exports: string[] = new Array(serverPlugins.length)
|
||||||
const imports: string[] = []
|
const imports: string[] = new Array(serverPlugins.length)
|
||||||
for (const plugin of serverPlugins) {
|
for (let i = 0; i < serverPlugins.length; i++) {
|
||||||
|
const plugin = serverPlugins[i]!
|
||||||
const path = relative(ctx.nuxt.options.rootDir, plugin.src)
|
const path = relative(ctx.nuxt.options.rootDir, plugin.src)
|
||||||
const variable = genSafeVariableName(filename(path)).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
const variable = genSafeVariableName(filename(path)).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
||||||
exports.push(variable)
|
exports[i] = variable
|
||||||
imports.push(genImport(plugin.src, variable))
|
imports[i] = genImport(plugin.src, variable)
|
||||||
}
|
}
|
||||||
return [
|
return [
|
||||||
...imports,
|
...imports,
|
||||||
|
@ -17,12 +17,11 @@ export function analyzePlugin (ctx: ViteBuildContext): Plugin[] {
|
|||||||
for (const _bundleId in outputBundle) {
|
for (const _bundleId in outputBundle) {
|
||||||
const bundle = outputBundle[_bundleId]
|
const bundle = outputBundle[_bundleId]
|
||||||
if (!bundle || bundle.type !== 'chunk') { continue }
|
if (!bundle || bundle.type !== 'chunk') { continue }
|
||||||
const minifiedModuleEntryPromises: Array<Promise<[string, RenderedModule]>> = []
|
const allModules = Object.entries(bundle.modules)
|
||||||
for (const [moduleId, module] of Object.entries(bundle.modules)) {
|
const minifiedModuleEntryPromises: Array<Promise<[string, RenderedModule]>> = new Array(allModules.length)
|
||||||
minifiedModuleEntryPromises.push(
|
for (let i = 0; i < allModules.length; i++) {
|
||||||
transform(module.code || '', { minify: true })
|
const [moduleId, module] = allModules[i]!
|
||||||
.then(result => [moduleId, { ...module, code: result.code }]),
|
minifiedModuleEntryPromises[i] = transform(module.code || '', { minify: true }).then(result => [moduleId, { ...module, code: result.code }])
|
||||||
)
|
|
||||||
}
|
}
|
||||||
bundle.modules = Object.fromEntries(await Promise.all(minifiedModuleEntryPromises))
|
bundle.modules = Object.fromEntries(await Promise.all(minifiedModuleEntryPromises))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user