-
Notifications
You must be signed in to change notification settings - Fork 33
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
swc support #167
swc support #167
Conversation
应该会有一些代价吧?比如印象中 swc 对 babel plugin 支持得一般,我们现在在用的 babel plugin 受影响大吗?以及 ts 的使用是不是有影响 |
目前拿 这个后面我还需要看看怎么处理好;更多的场景还没仔细测 |
你说的是要靠测试发现的细微的行为差异;有那种明面上就存在的问题吗,比如某个我们在用的 babel plugin swc 不支持?或者某个我们在用的 ts-loader / ts-compiler 配置 swc 没有提供对应的能力? |
了解,这些我一一确认下 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看了下实现,整体做法我没问题~
这个 PR 看上去最麻烦的还是确认行为差异 🤣
是的,做之前我粗略看了下我们使用到的 使用方在 |
是指 现在会用自定义 babel 配置的项目应该很少(这个配置甚至没有写在对外文档里,算是个后门..),所以影响不大;退回 babel 的时候我们也可以打个 warning 啥的,推动项目跟我们一起处理下 |
P.S. 现在再看,当初把 |
是的
哈哈哈,我没想到还能这么搞 😆 |
测试发现用 另在 |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
另外,build-config.md
里是不是要加上 swc
相关的文档?PR 中的“开启 swc 注意点”也补充过去?
decorators: true | ||
}, | ||
transform: { | ||
legacyDecorator: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对于 TS 项目,这里的一些配置是不是需要根据 tsconfig 的内容来决定?
另外我看 PR 说明里提到
- swc 不会读取项目中的
tsconfig.json
配置
这个听着不太科学啊,尤其是像 baseUrl
这样的信息,如果不去读,这里能正确地把项目编出来吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现在能正常编是因为 swc
自己维护了 baseUrl
配置
https://swc.rs/docs/configuration/compilation#jscbaseurl
那 swc
的部分配置(如 baseUrl
/ paths
等 tsconfig
也有的字段)我从 tsconfig
文件来读取?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对的,除了 builder 希望写死的配置,其他都应该尽量从项目的 tsconfig 文件来会合理一点,因为我们的项目确实会用 tsconfig 文件来配置构建行为
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已补充~ 1679c2b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
更正下,我上面对 swc
依赖 tsconfig.json
的 baseUrl
配置理解有误;跟 webpack
结合使用时,它的模块查找仍是使用 webpack 的 resolve.modules
baseUrl
生效场景是完全使用 swcpack
打包时的场景,用法上也挺坑的 swc-project/swc#2725 ,整体觉得 swc
还不太完善
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
另外 #167 (review) 这里提到的 build-config.md
还需要处理下
src/webpack/swc.ts
Outdated
import { abs } from '../utils/paths' | ||
|
||
/** read and parse tsconfig.json */ | ||
function readTsConfig() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIP: 严谨点说,这个函数返回的是 ts config 中的 compilerOptions 而不是 ts config?
src/webpack/swc.ts
Outdated
const compilerOptions = readTsConfig() | ||
|
||
if (compilerOptions != null) { | ||
return merge(swcOptions, convertTsConfig(compilerOptions)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lodash merge 的行为应该不是这里想要的?比如
merge(
{ paths: ['a', 'b'] },
{ paths: ['c'] }
)
的结果是
{ paths: ['c', 'b'] }
我理解
{ paths: ['c'] }
才是预期的?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, a4a0815
function customizer(srcValue, targetValue) {
if (_.isArray(targetValue)) {
return targetValue
}
}
_.mergeWith(
{
arr: [1, 2, 4],
paths: ['a', 'b'],
obj: { name: 'obj1', origin: 222 }
},
{
paths: ['c'],
obj: { name: 'obj2', value: 123 }
},
customizer
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🕍
使用时建议先仅在开发环境或者在内部 admin 系统开启
支持 swc 提升打包速度
开启后
swc-loader
将替换babel-loader
、ts-loader
,开启方式如下:开启 swc 注意点
transpileOnlyWhenDev
开关无效,如果需要在打包中校验类型可在打包构建步骤中加入tsc --noEmit
来做类型检查; TypeScript type checker swc-project/swc#571useProjectTypeScript
开关无效tsconfig.json
配置babelOptions
,swc 将不会开启JSX
中不支持spread operator
写法,ts-loader
是支持的,但 babel 也不支持 JSX parsing panics when using the spread operator swc-project/swc#2037browserslist
在 swc 中的行为相较 babel 有所差异,以配置的extends @qiniu/build-config/portal
为例,使用 swc 后 bundle 中会包含let
const
async
await
箭头函数
语法 preset-env: Switch tobrowserslist-rs
swc-project/swc#2781spread operator 语法更改方式
可以改成
测试
以
portal-fusion
打包测试为例,开启后整体打包耗时下降30%
左右