diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index 3442b5f1061..2891de29418 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 ( @@ -237,7 +241,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 { @@ -340,7 +348,7 @@ export class ModuleLoader { return existingModule; } - const module: Module = new Module( + const module = new Module( this.graph, id, this.options, @@ -379,7 +387,7 @@ export class ModuleLoader { module: Module, resolveStaticDependencyPromises: ResolveStaticDependencyPromise[], resolveDynamicDependencyPromises: ResolveDynamicDependencyPromise[] - ) { + ): Promise { await Promise.all([ this.fetchStaticDependencies(module, resolveStaticDependencyPromises), this.fetchDynamicDependencies(module, resolveDynamicDependencyPromises) @@ -520,7 +528,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 +676,7 @@ function addChunkNamesToModule( module: Module, { fileName, name }: UnresolvedModule, isUserDefined: boolean -) { +): void { if (fileName !== null) { module.chunkFileNames.add(fileName); } else if (name !== null) { @@ -681,7 +693,7 @@ function isNotAbsoluteExternal( id: string, source: string, makeAbsoluteExternalsRelative: boolean | 'ifRelativeSource' -) { +): boolean { return ( makeAbsoluteExternalsRelative === true || (makeAbsoluteExternalsRelative === 'ifRelativeSource' && isRelative(source)) || diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 01990bd88a3..efab8a79343 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -3,12 +3,13 @@ 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) { +function mkdirpath(path: string): void { const dir = dirname(path); try { fs.readdirSync(dir); @@ -25,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 => { 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, '/'); } diff --git a/src/utils/resolveId.ts b/src/utils/resolveId.ts index c6a908f926f..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 @@ -45,13 +45,12 @@ export async function resolveId( ); } -function addJsExtensionIfNecessary(file: string, preserveSymlinks: boolean) { - 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; +function addJsExtensionIfNecessary(file: string, preserveSymlinks: boolean): string | undefined { + return ( + findFile(file, preserveSymlinks) ?? + findFile(file + '.mjs', preserveSymlinks) ?? + findFile(file + '.js', preserveSymlinks) + ); } function findFile(file: string, preserveSymlinks: boolean): string | undefined { @@ -64,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