web-infra-dev / rspack

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

Module not found: Can't resolve "...xxxx./xxx.js" #7145

Closed cjnoname closed 1 month ago

cjnoname commented 1 month ago

System Info

System: OS: macOS 14.5 CPU: (10) arm64 Apple M1 Max Memory: 1.97 GB / 64.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node npm: 10.8.1 - ~/.nvm/versions/node/v20.14.0/bin/npm Browsers: Chrome: 126.0.6478.127 Safari: 17.5 npmPackages: @rspack/cli: 1.0.0-alpha.3 => 1.0.0-alpha.3 @rspack/core: 1.0.0-alpha.3 => 1.0.0-alpha.3

Details

Hi,

We are in the process of migrating to RsPack. Our setup includes a Node.js project using TypeScript and ESModules.

`

import { resolve, dirname } from "path"; import { fileURLToPath } from "url"; // import { sentryWebpackPlugin } from "@sentry/webpack-plugin";

const filename = fileURLToPath(import.meta.url); const dirname = dirname(__filename);

export default { mode: "production", experiments: { outputModule: true }, output: { chunkFormat: "module", chunkLoading: "import", library: { type: "module" } }, entry: "src/index.ts", externals: ["aws-sdk"], target: "node", // devtool: slsw.lib.webpack.isLocal ? "source-map" : undefined, resolve: { extensions: [".ts", ".js"], alias: { src: resolve(__dirname, "src") }, tsConfig: { configFile: "./tsconfig.json" }, symlinks: false }, module: { rules: [ { test: /.ts$/, exclude: [/node_modules/], loader: "builtin:swc-loader", options: { module: { type: "es6", strict: true, noInterop: false }, jsc: { parser: { syntax: "typescript", decorators: true, tsx: false }, transform: { legacyDecorator: true, decoratorMetadata: true // decoratorVersion: "2022-03" } } } } ] }, plugins: [] }; `

With the configuration above, we encountered several "Module not found: Can't resolve '...xxxx./xxx.js'" errors.

As far as we know, we should define the import as: import {xxx} from "./xxxx/xxxx.js" in TypeScript ESM.

Can anyone help?

Thanks.

Reproduce link

https://github.com/cjnoname/example-esm

Reproduce Steps

Please use the link above to reproduce this issue

colinaaa commented 1 month ago

Would you please try to add resolve.extensionAlias to your rspack.config.js? E.g.:

// rspack.config.js
export default {
  resolve: {
    extensionAlias: {
      '.js': ['.ts', '.js'],
    },
  },
};
cjnoname commented 1 month ago

Would you please try to add resolve.extensionAlias to your rspack.config.js? E.g.:

// rspack.config.js
export default {
  resolve: {
    extensionAlias: {
      '.js': ['.ts', '.js'],
    },
  },
};

It give another sets of errors

ERROR in ./node_modules/@techteamer/ocsp/lib/ocsp/utils.js × All of the aliased extension are not found

ERROR in ./node_modules/asn1.js-rfc2560/index.js × All of the aliased extension are not found

ERROR in ./node_modules/asn1.js-rfc5280/index.js × All of the aliased extension are not found

ERROR in ./node_modules/cli-highlight/dist/index.js × All of the aliased extension are not found

ERROR in ./node_modules/proxy-addr/index.js × All of the aliased extension are not found

ERROR in ./node_modules/snowflake-sdk/lib/agent/cert_util.js × All of the aliased extension are not found

ERROR in ./node_modules/snowflake-sdk/lib/connection/result/column.js × All of the aliased extension are not found

ERROR in ./node_modules/typeorm/platform/PlatformTools.js × All of the aliased extension are not found

ERROR in ./node_modules/typeorm/util/StringUtils.js × All of the aliased extension are not found

cjnoname commented 1 month ago

Would you please try to add resolve.extensionAlias to your rspack.config.js? E.g.:

// rspack.config.js
export default {
  resolve: {
    extensionAlias: {
      '.js': ['.ts', '.js'],
    },
  },
};

I have uploaded my repo to

https://github.com/cjnoname/example-esm

Please take a look, thanks