Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: RSPack can't resolve the TypeScript alias #8123

Closed
Verthon opened this issue Oct 15, 2024 · 2 comments
Closed

[Bug]: RSPack can't resolve the TypeScript alias #8123

Verthon opened this issue Oct 15, 2024 · 2 comments
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.

Comments

@Verthon
Copy link

Verthon commented Oct 15, 2024

System Info

System:
OS: macOS 14.6.1
CPU: (10) arm64 Apple M1 Max
Memory: 206.36 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.19.0 - ~/.nvm/versions/node/v18.19.0/bin/node
Yarn: 1.22.21 - ~/.nvm/versions/node/v18.19.0/bin/yarn
npm: 10.2.3 - ~/.nvm/versions/node/v18.19.0/bin/npm
pnpm: 9.12.1 - ~/.nvm/versions/node/v18.19.0/bin/pnpm
Watchman: 2024.10.07.00 - /opt/homebrew/bin/watchman
Browsers:
Chrome: 129.0.6668.100
Safari: 17.6
npmPackages:
@rspack/cli: ^1.0.0 => 1.0.0
@rspack/core: ^1.0.0 => 1.0.0

Details

Hello I'm using following setup

  • "typescript": "^5.6.3"
  • tsconfig with following setup
{
  "compilerOptions": {
    /* Base Options: */
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "es2022",
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,
    "verbatimModuleSyntax": true,

    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,

    /* If NOT transpiling with TypeScript: */
    "module": "preserve",
    "noEmit": true,

    /* If your code runs in the DOM: */
    "lib": ["es2022", "dom", "dom.iterable"],

    /* Aliases */
    "baseUrl": ".",
    "paths": {
      "src/*": ["src/*"]
    },
  }
}
  • rspack.config.mjs with following setup
import path from "path";
import { fileURLToPath } from "url";
import HtmlWebpackPlugin from "html-webpack-plugin";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const isRunningWebpack = !!process.env.WEBPACK;
const isRunningRspack = !!process.env.RSPACK;
if (!isRunningRspack && !isRunningWebpack) {
	throw new Error("Unknown bundler");
}

// "isolatedModules": true, in tsconfig.json ✅
// pnpm run type-check passes ✅

/**
 * @type {import('webpack').Configuration | import('@rspack/cli').Configuration}
 */
const config = {
	mode: "development",
	devtool: false,
	entry: {
		main: "./src/test.ts",
	},
	resolve: {
		// 1. tsconfig object fails with `Module not found: Can't resolve 'src/calc' in os-path-to-this-file`
		tsConfig: {
			configFile: path.resolve(__dirname, "./tsconfig.json"),
			references: "auto",
		},
		// 2. tsconfig inline fails with `Module not found: Can't resolve 'src/calc' in os-path-to-this-file`
		// tsConfig: path.resolve(__dirname, './tsconfig.json'),
		// 3. alias doesn't work also
		// alias: {
		//   src: path.resolve(__dirname, './src'),
		// },
	},
	plugins: [new HtmlWebpackPlugin()],
	output: {
		clean: true,
		path: isRunningWebpack
			? path.resolve(__dirname, "webpack-dist")
			: path.resolve(__dirname, "rspack-dist"),
		filename: "[name].js",
	},
	experiments: {
		css: true,
	},
	module: {
		rules: [
			{
				test: /\.svg$/,
				type: "asset",
			},
			{
				test: /\.ts$/,
				exclude: [/node_modules/],
				loader: "builtin:swc-loader",
				options: {
					jsc: {
						parser: {
							syntax: "typescript",
						},
					},
				},
				type: "javascript/auto",
			},
		],
	},
};

export default config;

Reproduce link

https://github.com/Verthon/typescript-aliases-issue

Reproduce Steps

  1. Run pnpm i
  2. Run pnpm type-check - it should pass without any issue
  3. Run pnpm build - should fail with
> rspack-repro@1.0.0 build:rspack /Users/a/b/typescript-aliases-issue
> RSPACK=1 pnpm rspack -c ./rspack.config.mjs

ERROR in × Module not found: Can't resolve './src/test.ts' in '/Users/a/b/typescript-aliases-issue'
@Verthon Verthon added bug Something isn't working pending triage The issue/PR is currently untouched. labels Oct 15, 2024
@inottn
Copy link
Contributor

inottn commented Oct 15, 2024

You need to configure resolve.extensions.

@Verthon
Copy link
Author

Verthon commented Oct 16, 2024

You need to configure resolve.extensions.

Thank you that was it, after adding extensions: ['...', '.ts', '.tsx', '.js', '.jsx', '.json'], // "..." means to extend from the default extensions
Everything works as expected.

@Verthon Verthon closed this as completed Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.
Projects
None yet
Development

No branches or pull requests

2 participants