Skip to content

Commit

Permalink
Make resolveModuleIds.ts resolve bare/non-relative package imports, too.
Browse files Browse the repository at this point in the history
For example, this rewrites the import from "ts-invariant/process" in
@apollo/client/utilities/globals/graphql.js to instead import from
"ts-invariant/process/index.js, so Node.js won't complain about the
directory import (even though ts-invariant/process/package.json exists).
  • Loading branch information
benjamn committed Jul 28, 2021
1 parent f5a7a58 commit 60e18e9
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions config/resolveModuleIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,44 @@ function isRelative(id: string) {
}

function normalizeSourceString(file: string, source?: Node | null) {
if (source && n.StringLiteral.check(source) && isRelative(source.value)) {
if (source && n.StringLiteral.check(source)) {
try {
source.value = normalizeId(source.value, file);
source.value = isRelative(source.value)
? normalizeId(source.value, file)
: normalizeNonRelativeId(source.value, file);
} catch (error) {
console.error(`Failed to resolve ${source.value} in ${file}`);
console.error(`Failed to resolve ${source.value} in ${file} with error ${error}`);
process.exit(1);
}
}
}

function normalizeNonRelativeId(id: string, file: string) {
const normal = normalizeId(id, file);
const normalParts = normal.split("/");
const sourceParts = id.split("/");
const nodeModulesIndex = normalParts.lastIndexOf("node_modules");
if (
nodeModulesIndex >= 0 &&
normalParts[nodeModulesIndex + 1] === sourceParts[0]
) {
const bareModuleIdentifier =
normalParts.slice(nodeModulesIndex + 1).join("/");
if (normal === normalizeId(bareModuleIdentifier, file)) {
return bareModuleIdentifier;
}
console.error(`Leaving ${id} import in ${file} unchanged because ${
bareModuleIdentifier
} does not resolve to the same module`);
}
return id;
}

function normalizeId(id: string, file: string) {
const basedir = path.dirname(file);
const absPath = resolve.sync(id, {
basedir,
extensions: [".mjs", ".js"],
packageFilter(pkg) {
return pkg.module ? {
...pkg,
Expand Down

0 comments on commit 60e18e9

Please sign in to comment.