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(dev): get deps from ESM packages without main #7272

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/get-dependencies-from-esm-without-main.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Update `getDependenciesToBundle` to handle ESM packages without main exports. Note that these packages must expose `package.json` in their `exports` field so that their path can be resolved.
32 changes: 32 additions & 0 deletions integration/compiler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test.describe("compiler", () => {
"esm-only-pkg",
"esm-only-single-export",
...getDependenciesToBundle("esm-only-exports-pkg"),
...getDependenciesToBundle("esm-only-nested-exports-pkg"),
],
};
`,
Expand Down Expand Up @@ -84,6 +85,13 @@ test.describe("compiler", () => {
return <div id="esm-only-exports-pkg">{esmOnlyPkg}</div>;
}
`,
"app/routes/esm-only-nested-exports-pkg.tsx": js`
import esmOnlyPkg from "esm-only-nested-exports-pkg/nested";

export default function EsmOnlyPkg() {
return <div id="esm-only-nested-exports-pkg">{esmOnlyPkg}</div>;
}
`,
"app/routes/esm-only-single-export.tsx": js`
import esmOnlyPkg from "esm-only-single-export";

Expand Down Expand Up @@ -129,6 +137,18 @@ test.describe("compiler", () => {
"node_modules/esm-only-exports-pkg/esm-only-exports-pkg.js": js`
export default "esm-only-exports-pkg";
`,
"node_modules/esm-only-nested-exports-pkg/package.json": json({
name: "esm-only-nested-exports-pkg",
version: "1.0.0",
type: "module",
exports: {
"./package.json": "./package.json",
"./nested": "./esm-only-nested-exports-pkg.js",
},
}),
"node_modules/esm-only-nested-exports-pkg/esm-only-nested-exports-pkg.js": js`
export default "esm-only-nested-exports-pkg";
`,
"node_modules/esm-only-single-export/package.json": json({
name: "esm-only-exports-pkg",
version: "1.0.0",
Expand Down Expand Up @@ -288,6 +308,18 @@ test.describe("compiler", () => {
);
});

test("allows consumption of ESM modules with only nested exports in CJS builds with `serverDependenciesToBundle` and `getDependenciesToBundle`", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
let res = await app.goto("/esm-only-nested-exports-pkg", true);
expect(res.status()).toBe(200); // server rendered fine
// rendered the page instead of the error boundary
expect(await app.getHtml("#esm-only-nested-exports-pkg")).toBe(
'<div id="esm-only-nested-exports-pkg">esm-only-nested-exports-pkg</div>'
);
});

test("allows consumption of packages with sub modules", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
let res = await app.goto("/package-with-submodule", true);
Expand Down
24 changes: 23 additions & 1 deletion packages/remix-dev/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,36 @@ export function getDependenciesToBundle(...pkg: string[]): string[] {
return Array.from(aggregatedDeps);
}

interface ErrorWithCode extends Error {
code: string;
}

function isErrorWithCode(error: unknown): error is ErrorWithCode {
return (
error instanceof Error &&
typeof (error as NodeJS.ErrnoException).code === "string"
);
}

function getPackageDependenciesRecursive(
pkg: string,
aggregatedDeps: Set<string>,
visitedPackages: Set<string>
): void {
visitedPackages.add(pkg);

let pkgPath = require.resolve(pkg);
let pkgPath: string;
try {
pkgPath = require.resolve(pkg);
} catch (err) {
if (isErrorWithCode(err) && err.code === "ERR_PACKAGE_PATH_NOT_EXPORTED") {
// Handle packages without main exports.
// They at least need to have package.json exported.
pkgPath = require.resolve(`${pkg}/package.json`);
} else {
throw err;
}
}
let lastIndexOfPackageName = pkgPath.lastIndexOf(pkg);
if (lastIndexOfPackageName !== -1) {
pkgPath = pkgPath.substring(0, lastIndexOfPackageName);
Expand Down