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(remix-dev/compiler): make getDependenciesToBundle more resilient to misconfigured packages.jsons #2593

Merged
merged 1 commit into from
Apr 7, 2022
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
33 changes: 32 additions & 1 deletion integration/compiler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,20 @@ describe("compiler", () => {
return <div id="esm-only-pkg">{esmOnlyPkg}</div>;
}
`,
"app/routes/esm-only-exports-pkg.jsx": js`
import esmOnlyPkg from "esm-only-exports-pkg";

export default function EsmOnlyPkg() {
return <div id="esm-only-exports-pkg">{esmOnlyPkg}</div>;
}
`,
"remix.config.js": js`
let { getDependenciesToBundle } = require("@remix-run/dev");
module.exports = {
serverDependenciesToBundle: ["esm-only-pkg"],
serverDependenciesToBundle: [
"esm-only-pkg",
...getDependenciesToBundle("esm-only-exports-pkg")
],
};
`,
"node_modules/esm-only-pkg/package.json": `{
Expand All @@ -69,6 +80,17 @@ describe("compiler", () => {
"node_modules/esm-only-pkg/esm-only-pkg.js": js`
export default "esm-only-pkg";
`,
"node_modules/esm-only-exports-pkg/package.json": `{
"name": "esm-only-exports-pkg",
"version": "1.0.0",
"type": "module",
"exports": {
".": "./esm-only-exports-pkg.js"
}
}`,
"node_modules/esm-only-exports-pkg/esm-only-exports-pkg.js": js`
export default "esm-only-exports-pkg";
`,
},
});

Expand Down Expand Up @@ -139,4 +161,13 @@ describe("compiler", () => {
`"<div id=\\"esm-only-pkg\\">esm-only-pkg</div>"`
);
});

it("allows consumption of ESM modules with exports in CJS builds with `serverDependenciesToBundle` and `getDependenciesToBundle`", async () => {
let res = await app.goto("/esm-only-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-exports-pkg")).toMatchInlineSnapshot(
`"<div id=\\"esm-only-exports-pkg\\">esm-only-exports-pkg</div>"`
);
});
});
7 changes: 6 additions & 1 deletion packages/remix-dev/compiler/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ function getPackageDependenciesRecursive(
): void {
visitedPackages.add(pkg);

let pkgJson = require.resolve(`${pkg}/package.json`);
let pkgPath = require.resolve(pkg);
let lastIndexOfPackageName = pkgPath.lastIndexOf(pkg);
if (lastIndexOfPackageName !== -1) {
pkgPath = pkgPath.substring(0, lastIndexOfPackageName);
}
let pkgJson = path.join(pkgPath, "package.json");
if (!fs.existsSync(pkgJson)) {
console.log(pkgJson, `does not exist`);
return;
Expand Down
4 changes: 3 additions & 1 deletion packages/remix-dev/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ export async function readConfig(
try {
appConfig = require(configFile);
} catch (error) {
throw new Error(`Error loading Remix config in ${configFile}`);
throw new Error(
`Error loading Remix config in ${configFile}\n${String(error)}`
);
}

let customServerEntryPoint = appConfig.server;
Expand Down