Skip to content

Commit

Permalink
rename helper function and return values
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Jan 3, 2023
1 parent b76cfeb commit 9eeed58
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/node-file-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ const fsReadlink = fs.promises.readlink;
const fsStat = fs.promises.stat;

type ParseSpecifierResult = {
path: string;
remainingSpecifier: string;
queryString: string | null
}

// Splits an ESM specifier into path and querystring (including the leading `?`). (If the specifier is CJS,
// it is passed through untouched.)
export function parseSpecifier(specifier: string, cjsResolve: boolean = true): ParseSpecifierResult {
let path = specifier;
export function splitQueryStringFromSpecifier(specifier: string, cjsResolve: boolean = true): ParseSpecifierResult {
let remainingSpecifier = specifier;
let queryString = null;

if (!cjsResolve) {
const specifierUrl = url.parse(specifier);
queryString = specifierUrl.search;

if (specifierUrl.search) {
path = specifier.replace(specifierUrl.search, '');
remainingSpecifier = specifier.replace(specifierUrl.search, '');
} else {
path = specifier;
remainingSpecifier = specifier;
}
}

return {path, queryString};
return {queryString, remainingSpecifier};
}

function inPath (path: string, parent: string) {
Expand Down Expand Up @@ -241,7 +241,7 @@ export class Job {

private maybeEmitDep = async (dep: string, path: string, cjsResolve: boolean) => {
// Only affects ESM dependencies
const { path: strippedDep, queryString = '' } = parseSpecifier(dep, cjsResolve)
const { remainingSpecifier: strippedDep, queryString = '' } = splitQueryStringFromSpecifier(dep, cjsResolve)
let resolved: string | string[] = '';
let error: Error | undefined;
try {
Expand Down Expand Up @@ -379,7 +379,7 @@ export class Job {
private async analyzeAndEmitDependency(rawPath: string, parent?: string, cjsResolve?: boolean) {

// Strip the querystring, if any. (Only affects ESM dependencies.)
const { path } = parseSpecifier(rawPath, cjsResolve)
const { remainingSpecifier: path } = splitQueryStringFromSpecifier(rawPath, cjsResolve)

// Since different querystrings may lead to different results, include the full path
// when noting whether or not we've already seen this path
Expand Down
4 changes: 2 additions & 2 deletions src/resolve-dependency.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAbsolute, resolve, sep } from 'path';
import { Job, parseSpecifier } from './node-file-trace';
import { Job, splitQueryStringFromSpecifier } from './node-file-trace';

// node resolver
// custom implementation to emit only needed package.json files for resolver
Expand All @@ -9,7 +9,7 @@ export default async function resolveDependency (specifier: string, parent: stri

// 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
specifier = splitQueryStringFromSpecifier(specifier, cjsResolve).remainingSpecifier

if (isAbsolute(specifier) || specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../')) {
const trailingSlash = specifier.endsWith('/');
Expand Down

0 comments on commit 9eeed58

Please sign in to comment.