From b2d273fa2faa0a2699273ab9592cfd5878f1a0c0 Mon Sep 17 00:00:00 2001 From: tbitw2549 Date: Sat, 26 Oct 2024 18:32:01 +0300 Subject: [PATCH] refactor: explicitly manage index --- packages/kit/src/resolve.ts | 9 ++++----- packages/nuxt/src/core/app.ts | 9 +++++---- packages/nuxt/src/core/runtime/nitro/error.ts | 8 ++++---- packages/nuxt/src/core/templates.ts | 16 ++++++++-------- packages/vite/src/plugins/analyze.ts | 6 +++--- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/kit/src/resolve.ts b/packages/kit/src/resolve.ts index 2a7997e1d0..f1af9475a1 100644 --- a/packages/kit/src/resolve.ts +++ b/packages/kit/src/resolve.ts @@ -171,14 +171,13 @@ export function createResolver (base: string | URL): Resolver { export async function resolveNuxtModule (base: string, paths: string[]): Promise { const resolved: string[] = new Array(paths.length) const resolver = createResolver(base) - - for (let i = 0; i < paths.length; i++) { - const path = paths[i]! + let index = 0 + for (const path of paths) { if (path.startsWith(base)) { - resolved[i] = path.split('/index.ts')[0]! + resolved[index++] = path.split('/index.ts')[0]! } else { 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) } } diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index ed4ac6d1dd..5961334428 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -252,11 +252,11 @@ 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]! + let index = 0 + for (const plugin of plugins) { try { 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'), ...plugin, } @@ -267,8 +267,9 @@ export async function annotatePlugins (nuxt: Nuxt, plugins: NuxtPlugin[]) { } else { 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)) diff --git a/packages/nuxt/src/core/runtime/nitro/error.ts b/packages/nuxt/src/core/runtime/nitro/error.ts index 8e522cfc75..8acfff59d8 100644 --- a/packages/nuxt/src/core/runtime/nitro/error.ts +++ b/packages/nuxt/src/core/runtime/nitro/error.ts @@ -11,10 +11,10 @@ export default async function errorhandler (error: H3Error, const errorStack: string[] = new Array(stack.length) const consoleStack: string[] = new Array(stack.length) - for (let count = 0; count < stack.length; count++) { - const i = stack[count]! - errorStack[count] = `${i.text}` - consoleStack[count] = ' ' + i.text + let index = 0 + for (const i of stack) { + errorStack[index] = `${i.text}` + consoleStack[index++] = ' ' + i.text } // Create an error object const errorObject = { diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index f116d64155..bb65ca2fd0 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -66,12 +66,12 @@ export const clientPluginTemplate: NuxtTemplate = { checkForCircularDependencies(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]! + let index = 0 + for (const plugin of clientPlugins) { const path = relative(ctx.nuxt.options.rootDir, plugin.src) const variable = genSafeVariableName(filename(plugin.src)).replace(PLUGIN_TEMPLATE_RE, '_') + '_' + hash(path) - exports[i] = variable - imports[i] = genImport(plugin.src, variable) + exports[index] = variable + imports[index++] = genImport(plugin.src, variable) } return [ ...imports, @@ -87,12 +87,12 @@ export const serverPluginTemplate: NuxtTemplate = { checkForCircularDependencies(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]! + let index = 0 + for (const plugin of serverPlugins) { const path = relative(ctx.nuxt.options.rootDir, plugin.src) const variable = genSafeVariableName(filename(path)).replace(PLUGIN_TEMPLATE_RE, '_') + '_' + hash(path) - exports[i] = variable - imports[i] = genImport(plugin.src, variable) + exports[index] = variable + imports[index++] = genImport(plugin.src, variable) } return [ ...imports, diff --git a/packages/vite/src/plugins/analyze.ts b/packages/vite/src/plugins/analyze.ts index 5ef542d585..0d248c3c1f 100644 --- a/packages/vite/src/plugins/analyze.ts +++ b/packages/vite/src/plugins/analyze.ts @@ -19,9 +19,9 @@ export function analyzePlugin (ctx: ViteBuildContext): Plugin[] { if (!bundle || bundle.type !== 'chunk') { continue } 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 }]) + let index = 0 + for (const [moduleId, module] of allModules) { + minifiedModuleEntryPromises[index++] = transform(module.code || '', { minify: true }).then(result => [moduleId, { ...module, code: result.code }]) } bundle.modules = Object.fromEntries(await Promise.all(minifiedModuleEntryPromises)) }