refactor: explicitly manage index

This commit is contained in:
tbitw2549 2024-10-26 18:32:01 +03:00
parent fc520d139f
commit b2d273fa2f
5 changed files with 24 additions and 24 deletions

View File

@ -171,14 +171,13 @@ 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[] = new Array(paths.length) const resolved: string[] = new Array(paths.length)
const resolver = createResolver(base) const resolver = createResolver(base)
let index = 0
for (let i = 0; i < paths.length; i++) { for (const path of paths) {
const path = paths[i]!
if (path.startsWith(base)) { if (path.startsWith(base)) {
resolved[i] = path.split('/index.ts')[0]! resolved[index++] = path.split('/index.ts')[0]!
} else { } else {
const resolvedPath = await resolver.resolvePath(path) const resolvedPath = await resolver.resolvePath(path)
resolved[i] = resolvedPath.slice(0, resolvedPath.lastIndexOf(path) + path.length) resolved[index++] = resolvedPath.slice(0, resolvedPath.lastIndexOf(path) + path.length)
} }
} }

View File

@ -252,11 +252,11 @@ 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++) { let index = 0
const plugin = plugins[i]! for (const plugin of plugins) {
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[index] = {
...await extractMetadata(code, IS_TSX.test(plugin.src) ? 'tsx' : 'ts'), ...await extractMetadata(code, IS_TSX.test(plugin.src) ? 'tsx' : 'ts'),
...plugin, ...plugin,
} }
@ -267,8 +267,9 @@ export async function annotatePlugins (nuxt: Nuxt, plugins: NuxtPlugin[]) {
} else { } else {
logger.warn(`Failed to parse static properties from plugin \`${relativePluginSrc}\`.`, e) logger.warn(`Failed to parse static properties from plugin \`${relativePluginSrc}\`.`, e)
} }
_plugins[i] = plugin _plugins[index] = plugin
} }
index++
} }
return _plugins.sort((a, b) => (a.order ?? orderMap.default) - (b.order ?? orderMap.default)) return _plugins.sort((a, b) => (a.order ?? orderMap.default) - (b.order ?? orderMap.default))

View File

@ -11,10 +11,10 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
const errorStack: string[] = new Array(stack.length) const errorStack: string[] = new Array(stack.length)
const consoleStack: string[] = new Array(stack.length) const consoleStack: string[] = new Array(stack.length)
for (let count = 0; count < stack.length; count++) { let index = 0
const i = stack[count]! for (const i of stack) {
errorStack[count] = `<span class="stack${i.internal ? ' internal' : ''}">${i.text}</span>` errorStack[index] = `<span class="stack${i.internal ? ' internal' : ''}">${i.text}</span>`
consoleStack[count] = ' ' + i.text consoleStack[index++] = ' ' + i.text
} }
// Create an error object // Create an error object
const errorObject = { const errorObject = {

View File

@ -66,12 +66,12 @@ export const clientPluginTemplate: NuxtTemplate = {
checkForCircularDependencies(clientPlugins) checkForCircularDependencies(clientPlugins)
const exports: string[] = new Array(clientPlugins.length) const exports: string[] = new Array(clientPlugins.length)
const imports: string[] = new Array(clientPlugins.length) const imports: string[] = new Array(clientPlugins.length)
for (let i = 0; i < clientPlugins.length; i++) { let index = 0
const plugin = clientPlugins[i]! for (const plugin of clientPlugins) {
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(PLUGIN_TEMPLATE_RE, '_') + '_' + hash(path) const variable = genSafeVariableName(filename(plugin.src)).replace(PLUGIN_TEMPLATE_RE, '_') + '_' + hash(path)
exports[i] = variable exports[index] = variable
imports[i] = genImport(plugin.src, variable) imports[index++] = genImport(plugin.src, variable)
} }
return [ return [
...imports, ...imports,
@ -87,12 +87,12 @@ export const serverPluginTemplate: NuxtTemplate = {
checkForCircularDependencies(serverPlugins) checkForCircularDependencies(serverPlugins)
const exports: string[] = new Array(serverPlugins.length) const exports: string[] = new Array(serverPlugins.length)
const imports: string[] = new Array(serverPlugins.length) const imports: string[] = new Array(serverPlugins.length)
for (let i = 0; i < serverPlugins.length; i++) { let index = 0
const plugin = serverPlugins[i]! for (const plugin of serverPlugins) {
const path = relative(ctx.nuxt.options.rootDir, plugin.src) const path = relative(ctx.nuxt.options.rootDir, plugin.src)
const variable = genSafeVariableName(filename(path)).replace(PLUGIN_TEMPLATE_RE, '_') + '_' + hash(path) const variable = genSafeVariableName(filename(path)).replace(PLUGIN_TEMPLATE_RE, '_') + '_' + hash(path)
exports[i] = variable exports[index] = variable
imports[i] = genImport(plugin.src, variable) imports[index++] = genImport(plugin.src, variable)
} }
return [ return [
...imports, ...imports,

View File

@ -19,9 +19,9 @@ export function analyzePlugin (ctx: ViteBuildContext): Plugin[] {
if (!bundle || bundle.type !== 'chunk') { continue } if (!bundle || bundle.type !== 'chunk') { continue }
const allModules = Object.entries(bundle.modules) const allModules = Object.entries(bundle.modules)
const minifiedModuleEntryPromises: Array<Promise<[string, RenderedModule]>> = new Array(allModules.length) const minifiedModuleEntryPromises: Array<Promise<[string, RenderedModule]>> = new Array(allModules.length)
for (let i = 0; i < allModules.length; i++) { let index = 0
const [moduleId, module] = allModules[i]! for (const [moduleId, module] of allModules) {
minifiedModuleEntryPromises[i] = transform(module.code || '', { minify: true }).then(result => [moduleId, { ...module, code: result.code }]) minifiedModuleEntryPromises[index++] = 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))
} }