diff --git a/.changeset/silent-trains-build.md b/.changeset/silent-trains-build.md new file mode 100644 index 00000000000..4237a246076 --- /dev/null +++ b/.changeset/silent-trains-build.md @@ -0,0 +1,5 @@ +--- +"@remix-run/dev": patch +--- + +Don't show ESM warnings when consumed via dynamic import. diff --git a/integration/esm-only-warning-test.ts b/integration/esm-only-warning-test.ts index ee3c150c245..f9803c79655 100644 --- a/integration/esm-only-warning-test.ts +++ b/integration/esm-only-warning-test.ts @@ -42,13 +42,17 @@ test.beforeAll(async () => { import b from "esm-only-exports"; import c from "esm-only-sub-exports"; import d from "esm-cjs-exports"; + import e from "cjs-dynamic-import"; - export function loader() { + export async function loader() { + let { default: f } = await import("esm-only-exports-b"); return json({ a: a(), b: b(), c: c(), d: d(), + e: e(), + f: f(), }); } @@ -79,6 +83,40 @@ test.beforeAll(async () => { "node_modules/esm-only-exports/index.js": js` export default () => "esm-only-no-exports"; `, + "node_modules/esm-only-exports-b/package.json": json({ + name: "esm-only-exports-b", + version: "1.0.0", + type: "module", + main: "index.js", + exports: { + ".": "./index.js", + "./package.json": "./package.json", + }, + }), + "node_modules/esm-only-exports-b/index.js": js` + export default () => "esm-only-no-exports-b"; + `, + "node_modules/esm-only-exports-c/package.json": json({ + name: "esm-only-exports-c", + version: "1.0.0", + type: "module", + main: "index.js", + exports: { + ".": "./index.js", + "./package.json": "./package.json", + }, + }), + "node_modules/esm-only-exports-c/index.js": js` + export default () => "esm-only-no-exports-c"; + `, + "node_modules/cjs-dynamic-import/package.json": json({ + name: "cjs-dynamic-import", + version: "1.0.0", + main: "index.js", + }), + "node_modules/cjs-dynamic-import/index.js": js` + module.exports = async () => "esm-only-no-exports-d" + (await import("esm-only-exports-c")).default(); + `, "node_modules/esm-only-sub-exports/package.json": json({ name: "esm-only-sub-exports", version: "1.0.0", @@ -143,6 +181,15 @@ test("logs warnings for ESM only packages", async () => { expect(buildOutput).toContain( "esm-only-exports is possibly an ESM only package" ); + expect(buildOutput).not.toContain( + "esm-only-exports-b is possibly an ESM only package" + ); + expect(buildOutput).not.toContain( + "esm-only-exports-c is possibly an ESM only package" + ); + expect(buildOutput).not.toContain( + "cjs-dynamic-import is possibly an ESM only package" + ); expect(buildOutput).toContain( "esm-only-sub-exports is possibly an ESM only package" ); diff --git a/packages/remix-dev/compiler/plugins/serverBareModulesPlugin.ts b/packages/remix-dev/compiler/plugins/serverBareModulesPlugin.ts index 0ace01c072f..4e9fcffe17f 100644 --- a/packages/remix-dev/compiler/plugins/serverBareModulesPlugin.ts +++ b/packages/remix-dev/compiler/plugins/serverBareModulesPlugin.ts @@ -36,7 +36,7 @@ export function serverBareModulesPlugin( return { name: "server-bare-modules", setup(build) { - build.onResolve({ filter: /.*/ }, ({ importer, path }) => { + build.onResolve({ filter: /.*/ }, ({ importer, kind, path }) => { // If it's not a bare module ID, bundle it. if (!isBareModuleId(resolvePath(path))) { return undefined; @@ -103,6 +103,7 @@ export function serverBareModulesPlugin( if ( onWarning && !isNodeBuiltIn(packageName) && + kind !== "dynamic-import" && (!remixConfig.serverBuildTarget || remixConfig.serverBuildTarget === "node-cjs") ) {