web-infra-dev / rspack

The fast Rust-based web bundler with webpack-compatible API 🦀️
https://rspack.dev
MIT License
8.15k stars 484 forks source link

Build error with `type:module` dependency package #5147

Closed musicq closed 6 months ago

musicq commented 6 months ago

System Info

  System:
    OS: macOS 14.2.1
    CPU: (12) arm64 Apple M2 Pro
    Memory: 235.14 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.0 - /usr/local/bin/node
    Yarn: 1.22.21 - ~/Library/pnpm/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.12.1 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 120.0.6099.129
    Safari: 17.2.1
  npmPackages:
    @rspack/cli: 0.4.0 => 0.4.0 
    @rspack/core: 0.4.0 => 0.4.0 
    @rspack/plugin-react-refresh: 0.4.0 => 0.4.0 

Details

Rspack will report error if I have some dependency packages with type: module in the package.json.

Errors be like

error[internal]: Failed to resolve ./control in javascript/esm|builtin:swc-loader??ruleSet[1].rules[2].use[0]!/Users/rp/node_modules/.pnpm/flowp@0.4.4/node_modules/flowp/dist/index.js
 = Did you mean './control.js'?
     BREAKING CHANGE: The request './control' failed to resolve only because it was resolved as fully specified
     (probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
     The extension in the request is mandatory for it to be fully specified.
     Add the extension to the request.

I tried the same code on rsbuild, it works.

Reproduce link

https://github.com/musicq/repr-rspack-type-module

Reproduce Steps

  1. pnpm i
  2. pnpm dev
hardfist commented 6 months ago

its same as webpack and not a bug,rsbuild works may because rsbuild add some extra default config for resolve options? cc @chenjiahan

chenjiahan commented 6 months ago

Try this:

module.exports = {
  rules: [
    {
      test: /\.m?js/,
      resolve: {
        fullySpecified: false
      }
    },
  ]
}
musicq commented 6 months ago

@chenjiahan it works!