web-infra-dev / rslib

The Rsbuild powered package build tool.
MIT License
361 stars 17 forks source link

[Bug]: Dynamic import in CJS output is broken #287

Open chenjiahan opened 6 days ago

chenjiahan commented 6 days ago

Version

Rslib 0.0.11

Details

Dynamic import in CJS output is broken.

There are some conditions:

For example, the input code is:

export async function main() {
  const { TsconfigPathsPlugin } = await import('tsconfig-paths-webpack-plugin');

  console.log(TsconfigPathsPlugin);
}

main();

Run the output code, and console.log(TsconfigPathsPlugin); get undefined.

Reproduce link

https://github.com/chenjiahan/rslib-repro-dynamic-import-cjs-interop

Reproduce Steps

Image

Image

Image

fi3ework commented 6 days ago

Duplicated with https://github.com/web-infra-dev/rslib/issues/121.

Current workaround

  output: {
    target: 'node',
+    externals: {
+      'tsconfig-paths-webpack-plugin': 'import tsconfig-paths-webpack-plugin',
+    },
  },
fi3ework commented 6 days ago

It will be resolved when we implement external map for webpack and Rspack, we're already working on this.