Open Fitz6 opened 2 months ago
👍🏻 I used Rsdoctor loader analysis to debug this issue and it is awesome !!!
It was observed that the swc-loader with transform deleted the unused import *
. The treeshaking feature of Rspack/webpack will help you analyze and retain sideEffects statements if there is no swc-loader.
If you use swc-loader or babel-loader with webpack/Rspack, you will encounter the same issue.
I haven't found an option to keep them at present
If you use swc-loader or babel-loader with webpack/Rspack, you will encounter the same issue.
webpack + babel-loader works fine. https://stackblitz.com/edit/webpack-webpack-js-org-oudeaz
webpack + swc-loader works the same.
https://stackblitz.com/edit/webpack-webpack-js-org-y3dq3h
If you use swc-loader or babel-loader with webpack/Rspack, you will encounter the same issue.
Sorry, some information was ignored, based on "preset-typescript"
If you use swc-loader or babel-loader with webpack/Rspack, you will encounter the same issue.
Sorry, some information was ignored, based on "preset-typescript"
babel-loader
with @babel/preset-typescript
works fine as well.
I tested the effects of using Webpack with several different loaders.
// test.js
export const doSomething = () => {
console.log('It works.')
}
window.doSomething = doSomething
// index.js with namespace import
import * as something from './test.js'
window.doSomething()
// index.ts with namespace import
import * as something from './test.js'
(window as any).doSomething()
Results: | Loader | Options | Entry | Result |
---|---|---|---|---|
babel-loader | presets: ['@babel/preset-typescript'] | index.js | ✔️ | |
babel-loader | presets: ['@babel/preset-typescript'] | index.ts | ❌ | |
swc-loader | jsc: { parser: { syntax: 'ecmascript' } } | index.js | ✔️ | |
swc-loader | jsc: { parser: { syntax: 'typescript' } } | index.js | ❌ | |
swc-loader | jsc: { parser: { syntax: 'typescript' } } | index.ts | ❌ | |
esbuild-loader | target: 'es2015' | index.js | ✔️ | |
esbuild-loader | target: 'es2015' | index.ts | ❌ |
Based on the tests above, it can be observed that when transforming .ts
files, all loaders remove the side effect function when using namespace import. When transforming .js
files, they all work fine.
However, SWC only works correctly when the jsc.parser.syntax
is set to ecmascript
, but it fails to handle TypeScript syntax. Setting jsc.parser.syntax
to typescript
results in the inability to preserve the side effect properly.
Unfortunately, in real-world projects, it is common to encounter a mix of TypeScript and JavaScript. It seems that SWC lacks the flexibility required for these tasks. Maybe I should create an issue for SWC. 😑
Version
Details
Reproduce link
https://rsbuild.dev/guide/start/quick-start
Reproduce Steps
rsbuild dev