Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support query parameters and fragments #4279

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
30 changes: 21 additions & 9 deletions src/ModuleLoader.ts
Expand Up @@ -158,10 +158,14 @@ export class ModuleLoader {
return module;
}

public preloadModule(resolvedId: NormalizedResolveIdWithoutDefaults): Promise<ModuleInfo> {
return this.fetchModule(this.addDefaultsToResolvedId(resolvedId)!, undefined, false, true).then(
module => module.info
async preloadModule(resolvedId: NormalizedResolveIdWithoutDefaults): Promise<ModuleInfo> {
const module = await this.fetchModule(
this.addDefaultsToResolvedId(resolvedId)!,
undefined,
false,
true
);
return module.info;
}

resolveId = async (
Expand Down Expand Up @@ -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<void> {
timeStart('load modules', 3);
let source: string | SourceDescription;
try {
Expand Down Expand Up @@ -340,7 +348,7 @@ export class ModuleLoader {
return existingModule;
}

const module: Module = new Module(
const module = new Module(
this.graph,
id,
this.options,
Expand Down Expand Up @@ -379,7 +387,7 @@ export class ModuleLoader {
module: Module,
resolveStaticDependencyPromises: ResolveStaticDependencyPromise[],
resolveDynamicDependencyPromises: ResolveDynamicDependencyPromise[]
) {
): Promise<void> {
await Promise.all([
this.fetchStaticDependencies(module, resolveStaticDependencyPromises),
this.fetchDynamicDependencies(module, resolveDynamicDependencyPromises)
Expand Down Expand Up @@ -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<void> {
const loadPromise = this.moduleLoadPromises.get(module);
if (isPreload) {
await loadPromise;
Expand Down Expand Up @@ -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) {
Expand All @@ -681,7 +693,7 @@ function isNotAbsoluteExternal(
id: string,
source: string,
makeAbsoluteExternalsRelative: boolean | 'ifRelativeSource'
) {
): boolean {
return (
makeAbsoluteExternalsRelative === true ||
(makeAbsoluteExternalsRelative === 'ifRelativeSource' && isRelative(source)) ||
Expand Down
9 changes: 5 additions & 4 deletions src/utils/fs.ts
Expand Up @@ -3,12 +3,13 @@ import { dirname } from './path';

export * from 'fs';

export const readFile = (file: string): Promise<string> =>
new Promise<string>((fulfil, reject) =>
export function readFile(file: string): Promise<string> {
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);
Expand All @@ -25,7 +26,7 @@ function mkdirpath(path: string) {
}

export function writeFile(dest: string, data: string | Uint8Array): Promise<void> {
return new Promise<void>((fulfil, reject) => {
return new Promise((fulfil, reject) => {
mkdirpath(dest);

fs.writeFile(dest, data, err => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/path.ts
Expand Up @@ -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, '/');
}

Expand Down
17 changes: 8 additions & 9 deletions src/utils/resolveId.ts
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down