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

fix(resolution): Fix resolution of querystringed imports in ESM files #322

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
strip querystring from ESM imports in resolveDependency
  • Loading branch information
lobsterkatie committed Dec 6, 2022
commit fb936206d4e0ff4786ff0cdcc0f966518bfc2de2
7 changes: 6 additions & 1 deletion src/resolve-dependency.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { isAbsolute, resolve, sep } from 'path';
import { Job } from './node-file-trace';
import { Job, parseSpecifier } from './node-file-trace';

// node resolver
// custom implementation to emit only needed package.json files for resolver
// (package.json files are emitted as they are hit)
export default async function resolveDependency (specifier: string, parent: string, job: Job, cjsResolve = true): Promise<string | string[]> {
let resolved: string | string[];

// ESM imports are allowed to have querystrings, but the native Node behavior is to ignore them when doing
// file resolution, so emulate that here by stripping any querystring off before continuing
specifier = parseSpecifier(specifier, cjsResolve).path

if (isAbsolute(specifier) || specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../')) {
const trailingSlash = specifier.endsWith('/');
resolved = await resolvePath(resolve(parent, '..', specifier) + (trailingSlash ? '/' : ''), parent, job);
Expand Down