From f016d4cf41103ba92857176d627a880ce71527eb Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 21 Nov 2021 14:46:32 -0500 Subject: [PATCH 01/11] add function return type --- src/utils/resolveId.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/resolveId.ts b/src/utils/resolveId.ts index c6a908f926f..ab49c86ede8 100644 --- a/src/utils/resolveId.ts +++ b/src/utils/resolveId.ts @@ -45,7 +45,7 @@ export async function resolveId( ); } -function addJsExtensionIfNecessary(file: string, preserveSymlinks: boolean) { +function addJsExtensionIfNecessary(file: string, preserveSymlinks: boolean): string | undefined { let found = findFile(file, preserveSymlinks); if (found) return found; found = findFile(file + '.mjs', preserveSymlinks); From 6f68357f56a419bce13f50fb92a2594bc6914218 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 21 Nov 2021 14:47:45 -0500 Subject: [PATCH 02/11] simplify --- src/utils/resolveId.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/utils/resolveId.ts b/src/utils/resolveId.ts index ab49c86ede8..95b7557809d 100644 --- a/src/utils/resolveId.ts +++ b/src/utils/resolveId.ts @@ -46,12 +46,11 @@ export async function resolveId( } function addJsExtensionIfNecessary(file: string, preserveSymlinks: boolean): string | undefined { - let found = findFile(file, preserveSymlinks); - if (found) return found; - found = findFile(file + '.mjs', preserveSymlinks); - if (found) return found; - found = findFile(file + '.js', preserveSymlinks); - return found; + return ( + findFile(file, preserveSymlinks) ?? + findFile(file + '.mjs', preserveSymlinks) ?? + findFile(file + '.js', preserveSymlinks) + ); } function findFile(file: string, preserveSymlinks: boolean): string | undefined { From 738d8f594dbc29540c75dac6330a6df9b416e5f0 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 21 Nov 2021 14:48:52 -0500 Subject: [PATCH 03/11] use includes --- src/utils/resolveId.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/resolveId.ts b/src/utils/resolveId.ts index 95b7557809d..08ce52cfabb 100644 --- a/src/utils/resolveId.ts +++ b/src/utils/resolveId.ts @@ -63,7 +63,7 @@ function findFile(file: string, preserveSymlinks: boolean): string | undefined { const name = basename(file); const files = readdirSync(dirname(file)); - if (files.indexOf(name) !== -1) return file; + if (files.includes(name)) return file; } } catch { // suppress From 8fa7422e66779dcb3c1291f8e9d43dd1b689f9bf Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 21 Nov 2021 15:02:29 -0500 Subject: [PATCH 04/11] add method return types --- src/ModuleLoader.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index 3442b5f1061..0dfe6c5090b 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -237,7 +237,11 @@ export class ModuleLoader { ); } - private async addModuleSource(id: string, importer: string | undefined, module: Module) { + private async addModuleSource( + id: string, + importer: string | undefined, + module: Module + ): Promise { timeStart('load modules', 3); let source: string | SourceDescription; try { @@ -379,7 +383,7 @@ export class ModuleLoader { module: Module, resolveStaticDependencyPromises: ResolveStaticDependencyPromise[], resolveDynamicDependencyPromises: ResolveDynamicDependencyPromise[] - ) { + ): Promise { await Promise.all([ this.fetchStaticDependencies(module, resolveStaticDependencyPromises), this.fetchDynamicDependencies(module, resolveDynamicDependencyPromises) @@ -520,7 +524,11 @@ export class ModuleLoader { ); } - private async handleExistingModule(module: Module, isEntry: boolean, isPreload: boolean) { + private async handleExistingModule( + module: Module, + isEntry: boolean, + isPreload: boolean + ): Promise { const loadPromise = this.moduleLoadPromises.get(module); if (isPreload) { await loadPromise; @@ -664,7 +672,7 @@ function addChunkNamesToModule( module: Module, { fileName, name }: UnresolvedModule, isUserDefined: boolean -) { +): void { if (fileName !== null) { module.chunkFileNames.add(fileName); } else if (name !== null) { @@ -681,7 +689,7 @@ function isNotAbsoluteExternal( id: string, source: string, makeAbsoluteExternalsRelative: boolean | 'ifRelativeSource' -) { +): boolean { return ( makeAbsoluteExternalsRelative === true || (makeAbsoluteExternalsRelative === 'ifRelativeSource' && isRelative(source)) || From c50644235adbfb6b323815c54ef01195369e0c96 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 21 Nov 2021 15:03:12 -0500 Subject: [PATCH 05/11] convert to async function --- src/ModuleLoader.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index 0dfe6c5090b..9f574554326 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -158,10 +158,14 @@ export class ModuleLoader { return module; } - public preloadModule(resolvedId: NormalizedResolveIdWithoutDefaults): Promise { - return this.fetchModule(this.addDefaultsToResolvedId(resolvedId)!, undefined, false, true).then( - module => module.info + async preloadModule(resolvedId: NormalizedResolveIdWithoutDefaults): Promise { + const module = await this.fetchModule( + this.addDefaultsToResolvedId(resolvedId)!, + undefined, + false, + true ); + return module.info; } resolveId = async ( From 1bd13be0379e4652bd78f9b9eddbe5365405397c Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 21 Nov 2021 20:04:43 -0500 Subject: [PATCH 06/11] remove type declaration --- src/ModuleLoader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index 9f574554326..2891de29418 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -348,7 +348,7 @@ export class ModuleLoader { return existingModule; } - const module: Module = new Module( + const module = new Module( this.graph, id, this.options, From e6b5b1aac78e09c48ca06bf9254f97dd13550394 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Mon, 22 Nov 2021 23:39:02 -0500 Subject: [PATCH 07/11] use startsWith --- src/utils/resolveId.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/resolveId.ts b/src/utils/resolveId.ts index 08ce52cfabb..c98ea780336 100644 --- a/src/utils/resolveId.ts +++ b/src/utils/resolveId.ts @@ -33,7 +33,7 @@ export async function resolveId( // external modules (non-entry modules that start with neither '.' or '/') // are skipped at this stage. - if (importer !== undefined && !isAbsolute(source) && source[0] !== '.') return null; + if (importer !== undefined && !isAbsolute(source) && !source.startsWith('.')) return null; // `resolve` processes paths from right to left, prepending them until an // absolute path is created. Absolute importees therefore shortcircuit the From c6fca6c9ad8a8238cbfaf2fee0200c2829c56f1f Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Mon, 22 Nov 2021 23:45:47 -0500 Subject: [PATCH 08/11] use statement --- src/utils/fs.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 01990bd88a3..7d5f9fdfb2f 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -3,10 +3,11 @@ import { dirname } from './path'; export * from 'fs'; -export const readFile = (file: string): Promise => - new Promise((fulfil, reject) => +export function readFile(file: string): Promise { + return new Promise((fulfil, reject) => fs.readFile(file, 'utf-8', (err, contents) => (err ? reject(err) : fulfil(contents))) ); +} function mkdirpath(path: string) { const dir = dirname(path); From c840d6367795b4fad6a67f59d058d1649e7250e8 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Mon, 22 Nov 2021 23:46:40 -0500 Subject: [PATCH 09/11] use type inference, remove explicit promise type parameter --- src/utils/fs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 7d5f9fdfb2f..a264db28a38 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -4,7 +4,7 @@ import { dirname } from './path'; export * from 'fs'; export function readFile(file: string): Promise { - return new Promise((fulfil, reject) => + return new Promise((fulfil, reject) => fs.readFile(file, 'utf-8', (err, contents) => (err ? reject(err) : fulfil(contents))) ); } @@ -26,7 +26,7 @@ function mkdirpath(path: string) { } export function writeFile(dest: string, data: string | Uint8Array): Promise { - return new Promise((fulfil, reject) => { + return new Promise((fulfil, reject) => { mkdirpath(dest); fs.writeFile(dest, data, err => { From 4ae64e4870ea9c2f19b7daffc02028e528f661c4 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Mon, 22 Nov 2021 23:47:16 -0500 Subject: [PATCH 10/11] add explicit function return type --- src/utils/fs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/fs.ts b/src/utils/fs.ts index a264db28a38..efab8a79343 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -9,7 +9,7 @@ export function readFile(file: string): Promise { ); } -function mkdirpath(path: string) { +function mkdirpath(path: string): void { const dir = dirname(path); try { fs.readdirSync(dir); From 8fdf7d41145fcf2e6aa40062712ef93618ec3e24 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Mon, 22 Nov 2021 23:48:16 -0500 Subject: [PATCH 11/11] use string.prototype.includes --- src/utils/path.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/path.ts b/src/utils/path.ts index e855ec294c5..870d591630d 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -10,7 +10,7 @@ export function isRelative(path: string): boolean { } export function normalize(path: string): string { - if (path.indexOf('\\') == -1) return path; + if (!path.includes('\\')) return path; return path.replace(/\\/g, '/'); }