From cfd907e843e7047d950ddacff91b54a2f47b7a39 Mon Sep 17 00:00:00 2001 From: Jacob Ebey Date: Fri, 29 Jul 2022 12:37:03 -0700 Subject: [PATCH 1/3] chore: don't show esm warning on dynamic imports --- integration/esm-only-warning-test.ts | 20 ++++++++++++++++++- .../plugins/serverBareModulesPlugin.ts | 3 ++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/integration/esm-only-warning-test.ts b/integration/esm-only-warning-test.ts index ee3c150c245..c69c9411650 100644 --- a/integration/esm-only-warning-test.ts +++ b/integration/esm-only-warning-test.ts @@ -43,12 +43,14 @@ test.beforeAll(async () => { import c from "esm-only-sub-exports"; import d from "esm-cjs-exports"; - export function loader() { + export async function loader() { + let { default: e } = await import("esm-only-exports-b"); return json({ a: a(), b: b(), c: c(), d: d(), + e: e(), }); } @@ -79,6 +81,19 @@ 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"; + `, "node_modules/esm-only-sub-exports/package.json": json({ name: "esm-only-sub-exports", version: "1.0.0", @@ -143,6 +158,9 @@ 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).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") ) { From 1ba2d8d2a4525d3895898d794e3c5989bfb81ec2 Mon Sep 17 00:00:00 2001 From: Jacob Ebey Date: Fri, 29 Jul 2022 12:38:31 -0700 Subject: [PATCH 2/3] add changeset --- .changeset/silent-trains-build.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/silent-trains-build.md 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. From aef5de3eb8a96e4cbea66586fe945eb4fad14016 Mon Sep 17 00:00:00 2001 From: Jacob Ebey Date: Fri, 29 Jul 2022 14:49:10 -0700 Subject: [PATCH 3/3] add test --- integration/esm-only-warning-test.ts | 33 ++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/integration/esm-only-warning-test.ts b/integration/esm-only-warning-test.ts index c69c9411650..f9803c79655 100644 --- a/integration/esm-only-warning-test.ts +++ b/integration/esm-only-warning-test.ts @@ -42,15 +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 async function loader() { - let { default: e } = await import("esm-only-exports-b"); + let { default: f } = await import("esm-only-exports-b"); return json({ a: a(), b: b(), c: c(), d: d(), e: e(), + f: f(), }); } @@ -92,7 +94,28 @@ test.beforeAll(async () => { }, }), "node_modules/esm-only-exports-b/index.js": js` - export default () => "esm-only-no-exports"; + 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", @@ -161,6 +184,12 @@ test("logs warnings for ESM only packages", async () => { 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" );