Skip to content

Commit

Permalink
preserve querystring in emitDependency
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Nov 8, 2022
1 parent 40a8710 commit 37c2a03
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/node-file-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isMatch } from 'micromatch';
import { sharedLibEmit } from './utils/sharedlib-emit';
import { join } from 'path';
import { Sema } from 'async-sema';
import * as url from "url";

const fsReadFile = fs.promises.readFile;
const fsReadlink = fs.promises.readlink;
Expand Down Expand Up @@ -240,19 +241,41 @@ export class Job {
return;
}

let queryString: string | null = null;
try {
// Store the querystring (including the leading `?`, in order to distinguish it from any
// similar values in the path itself) so that it can be restored if it gets stripped during
// resolution.
queryString = url.parse(dep).search;
} catch (e: any) {
this.warnings.add(new Error(`Failed to parse dependency ${dep} for querystring:\n${e && e.message}`));
}

if (Array.isArray(resolved)) {
for (const item of resolved) {
// ignore builtins
if (item.startsWith('node:')) return;
await this.emitDependency(item, path);
await this.emitResolved(item, queryString, path);
}
} else {
// ignore builtins
if (resolved.startsWith('node:')) return;
await this.emitDependency(resolved, path);
await this.emitResolved(resolved, queryString, path);
}
}

private emitResolved (resolvedPath: string, queryString: string | null, parent?: string): Promise<void> {
// ignore builtins
if (resolvedPath.startsWith('node:')) {
return Promise.resolve();
}

// Dependencies which differ only by querystring are considered separate dependencies by
// webpack, and therefore need to be treated separately here, too. Resolution can strip
// the querystring off, in which case we need to put it back on to preserve the distinction.
if (queryString && !resolvedPath.endsWith(queryString)) {
resolvedPath += queryString
}

return this.emitDependency(resolvedPath, parent)
}

async resolve (id: string, parent: string, job: Job, cjsResolve: boolean): Promise<string | string[]> {
return resolveDependency(id, parent, job, cjsResolve);
}
Expand Down

0 comments on commit 37c2a03

Please sign in to comment.