Rspack provides native support for TypeScript, allowing developers to handle TypeScript files without additional configuration, greatly simplifying the development process. Here's a detailed explanation of Rspack's TypeScript support:
Native TypeScript Support
Rspack has built-in TypeScript support, which means:
-
No Additional Loader Needed:
- No need to install ts-loader or other TypeScript loaders
- Simply import
.tsand.tsxfiles directly - Automatically handles TypeScript compilation
-
Type Checking:
- Supports type checking (optional)
- Can configure whether to perform type checking
- Provides type error hints
-
Type Declaration Files:
- Supports
.d.tstype declaration files - Automatically resolves type declarations
- Provides complete type support
- Supports
Basic Configuration
Minimal Configuration
javascriptmodule.exports = { entry: './src/index.ts', module: { rules: [ { test: /\.ts$/, use: 'builtin:swc-loader', type: 'javascript/auto' } ] } }
Complete Configuration
javascriptmodule.exports = { entry: './src/index.tsx', module: { rules: [ { test: /\.(ts|tsx)$/, use: { loader: 'builtin:swc-loader', options: { jsc: { parser: { syntax: 'typescript', tsx: true }, transform: { react: { runtime: 'automatic' } } } } }, type: 'javascript/auto' } ] }, resolve: { extensions: ['.ts', '.tsx', '.js', '.jsx'] } }
SWC Loader
Rspack uses the built-in SWC Loader to handle TypeScript. SWC is an ultra-fast TypeScript/JavaScript compiler written in Rust:
SWC Advantages
-
Extremely Fast Compilation Speed:
- 20-70x faster than Babel
- 10-30x faster than tsc
- Significantly improves build speed
-
Complete TypeScript Support:
- Supports all TypeScript syntax
- Supports latest ECMAScript features
- Compatible with TypeScript configuration
-
Low Memory Usage:
- Uses less memory than Babel
- Suitable for large projects
- Better resource utilization
SWC Configuration Options
javascript{ loader: 'builtin:swc-loader', options: { jsc: { parser: { syntax: 'typescript', tsx: true, decorators: true, dynamicImport: true }, transform: { react: { runtime: 'automatic', importSource: '@emotion/react' }, optimizer: { globals: { vars: { 'process.env.NODE_ENV': 'production' } } } }, target: 'es2015', loose: false, externalHelpers: true }, env: { targets: 'defaults', coreJs: 3 }, sourceMaps: true, inlineSourcesContent: true } }
tsconfig.json Integration
Rspack can read and use tsconfig.json configuration:
json{ "compilerOptions": { "target": "ES2020", "module": "ESNext", "lib": ["ES2020", "DOM", "DOM.Iterable"], "jsx": "react-jsx", "strict": true, "moduleResolution": "node", "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "isolatedModules": true, "noEmit": true }, "include": ["src"], "exclude": ["node_modules"] }
Type Checking
Type Checking Configuration
Rspack supports two type checking methods:
-
Build-time Type Checking:
javascriptmodule.exports = { builtins: { pluginImport: [ { libraryName: 'lodash', libraryDirectory: '', camel2DashComponentName: false } ] }, plugins: [ new rspack.ForkTsCheckerWebpackPlugin({ typescript: { configFile: './tsconfig.json', memoryLimit: 4096 } }) ] } -
Independent Type Checking:
- Run type checking separately using
tsc --noEmit - Integrate into CI/CD pipeline
- Separate type checking from build process
- Run type checking separately using
Type Checking Best Practices
-
Development Environment:
- Can disable type checking during development to improve speed
- Use editor's type checking features
- Perform quick type checking on save
-
Build Environment:
- Enable complete type checking in production builds
- Ensure code quality
- Block deployment of code with type errors
-
CI/CD:
- Run type checking in continuous integration
- Use as code quality gate
- Combine with other checking tools
React + TypeScript
Rspack provides complete support for React + TypeScript:
javascriptmodule.exports = { module: { rules: [ { test: /\.(ts|tsx)$/, use: { loader: 'builtin:swc-loader', options: { jsc: { parser: { syntax: 'typescript', tsx: true }, transform: { react: { runtime: 'automatic', importSource: '@emotion/react' } } } } }, type: 'javascript/auto' } ] }, resolve: { extensions: ['.ts', '.tsx', '.js', '.jsx'] } }
Performance Optimization
-
Incremental Compilation:
- Leverage Rspack's incremental build capabilities
- Only recompile changed TypeScript files
- Greatly improve development experience
-
Type Caching:
- Cache type checking results
- Avoid redundant type checking
- Improve build speed
-
Parallel Processing:
- Process multiple TypeScript files in parallel
- Fully utilize multi-core CPU
- Shorten build time
Common Issues
-
Type Declaration Files Not Found:
- Ensure
@typespackages are installed - Check tsconfig.json configuration
- Verify type declaration file paths
- Ensure
-
Type Checking Errors:
- Check tsconfig.json configuration
- Ensure type definitions are correct
- Use
// @ts-ignoreor// @ts-expect-errorto temporarily ignore
-
Slow Compilation Speed:
- Ensure SWC Loader is being used
- Enable incremental builds
- Optimize tsconfig.json configuration
Rspack's TypeScript support provides developers with a fast and efficient development experience. Through reasonable configuration and optimization, you can fully leverage TypeScript's type safety advantages while maintaining extremely fast build speeds.