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[]> {
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)
}
}

View File

@ -252,11 +252,11 @@ 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]!
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))

View File

@ -11,10 +11,10 @@ export default <NitroErrorHandler> 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] = `<span class="stack${i.internal ? ' internal' : ''}">${i.text}</span>`
consoleStack[count] = ' ' + i.text
let index = 0
for (const i of stack) {
errorStack[index] = `<span class="stack${i.internal ? ' internal' : ''}">${i.text}</span>`
consoleStack[index++] = ' ' + i.text
}
// Create an error object
const errorObject = {

View File

@ -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,

View File

@ -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<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 }])
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))
}