Skip to content

Commit

Permalink
chore(config): 将 babel 改为 esbuild (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
KRISACHAN authored Jun 22, 2023
1 parent 5089969 commit b4d76c6
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 950 deletions.
51 changes: 0 additions & 51 deletions .babelrc

This file was deleted.

78 changes: 13 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 项目信息

这是一个基于 `webpack@^5.27.2` + `typescript@^4.2.3` + `@babel/core@^7.2.2` + `jest@^26.6.3` + `eslint@^7.22.0` 的多页面脚手架。
这是一个基于 `webpack@^5.27.2` + `typescript@^4.2.3` + `esbuild-loader@^3.0.1,` + `jest@^26.6.3` + `eslint@^7.22.0` 的多页面脚手架。

本库支持增量更新,支持 `gzip` 打包,支持第三方资源别名引入,支持静态文件引入,支持使用环境变量。

Expand Down Expand Up @@ -55,7 +55,6 @@ yarn prepare
### **项目结构**

```txt
├─.babelrc // babel配置文件
├─.browserslistrc // 浏览器兼容配置
├─.cz-config.js // commitizen 配置
├─.editorconfig // 编辑器配置
Expand Down Expand Up @@ -340,72 +339,21 @@ precss({

#### 基础方案

ECMA 语法的基础方案为`@babel/preset-env`,主要配置如下:
ECMA 语法的基础方案为 `esbuild-loader`,主要配置如下:

```javascript
// ./.babelrc
"presets": [
// 文档:https://babeljs.io/docs/babel-preset-env
[
"@babel/preset-env",
{
// 配置:https://babeljs.io/docs/options#targets
"targets": {
"esmodules": true
},
// 配置:https://babeljs.io/docs/babel-preset-env#usebuiltins
"useBuiltIns": "usage",
// 配置:https://babeljs.io/docs/babel-preset-env#corejs
"corejs": {
"version": 3,
"proposals": true
}
}
],
[
"@babel/preset-typescript",
{
"optimizeConstEnums": true
}
]
],
// ./config/webpack.config.base.js
{
loader: 'esbuild-loader',
options: {
loader: 'tsx',
target: 'esnext',
tsconfig: './tsconfig.json'
},
},
```

`@babel/preset-env`文档如下:

<https://babeljs.io/docs/en/babel-preset-env>

#### 拓展语法

可按需增删需要的 ECMA 提案插件,主要配置如下:

```javascript
// ./.babelrc
"plugins": [
// 文档:https://babeljs.io/docs/babel-plugin-transform-runtime
"@babel/plugin-transform-runtime",
"@babel/plugin-transform-arrow-functions",
"@babel/plugin-proposal-optional-chaining",
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
],
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
[
"@babel/plugin-proposal-private-methods",
{
"loose": true
}
]
]
```
已用 `esbuild-loader` 代替了 `babel-loader`,在 2023 的今年,不再考虑打包到 es5 代码

### typescript

Expand Down Expand Up @@ -438,7 +386,7 @@ IS_MOBILE=false # 判断是否是移动端,如果是,则打开postcss-px-to-

**jest** 测试 **dom** 时,相关代码需要包裹在 **window.onload** 里,否则会报错。例如

```
```javascript
'use strict'
import '@/style/index.css'
import qrcodeImg from 'static/img/qrcode-all1.png'
Expand Down
2 changes: 2 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ module.exports = {
'refactor', // 重构(既不是增加feature),也不是修复bug
'pref', // 性能优化
'test', // 增加测试
'wip', // 半成品: 开发中
'build', // 打包
'ci', // 集成
'style', // 代码格式
'chore', // 构建: 变更构建流程或辅助工具
'revert', // 回退
],
],
Expand Down
7 changes: 4 additions & 3 deletions config/webpack.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ const baseConfig = {
// https://www.npmjs.com/package/thread-loader 将下方的 loader 放入 worker 池里。每个 worker 都是一个单独的有 600ms 限制的 node.js 进程。同时跨进程的数据交换也会被限制。
'thread-loader',
{
loader: 'babel-loader',
loader: 'esbuild-loader',
options: {
cacheDirectory: true,
cacheCompression: true,
loader: 'tsx',
target: 'es2015',
tsconfig: './tsconfig.json',
},
},
],
Expand Down
19 changes: 2 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@
"lodash-es": "^4.17.21"
},
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-proposal-decorators": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.6.2",
"@babel/plugin-proposal-optional-chaining": "^7.6.0",
"@babel/plugin-proposal-private-methods": "^7.8.3",
"@babel/plugin-transform-arrow-functions": "^7.10.1",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.6.2",
"@babel/preset-typescript": "^7.6.0",
"@babel/runtime": "^7.9.2",
"@babel/runtime-corejs2": "^7.9.2",
"@commitlint/cli": "^17.4.4",
"@commitlint/config-conventional": "^17.4.4",
"@types/eslint": "^7.2.1",
Expand All @@ -30,9 +18,6 @@
"@typescript-eslint/typescript-estree": "^4.19.0",
"acorn": "^7.1.0",
"autoprefixer": "^10.2.5",
"babel-jest": "^26.3.0",
"babel-loader": "^8.0.5",
"babel-polyfill": "^6.26.0",
"chalk": "^2.4.2",
"clean-webpack-plugin": "^1.0.0",
"commitizen": "^4.1.5",
Expand All @@ -45,6 +30,7 @@
"cz-customizable": "^6.3.0",
"dotenv": "^8.2.0",
"es6-promise": "^4.2.5",
"esbuild-loader": "^3.0.1",
"eslint": "^7.22.0",
"eslint-config-prettier": "^6.11.0",
"eslint-config-standard": "^14.1.1",
Expand Down Expand Up @@ -120,8 +106,7 @@
"stylelint",
"prettier",
"lint-staged",
"babel",
"less",
"esbuild",
"multiple pages"
],
"author": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"outDir": "./dist/",
"noImplicitAny": true,
"module": "ESNext",
"target": "ESNext",
"target": "ES2015",
"allowJs": true,
"alwaysStrict": true,
"watch": true,
Expand Down
Loading

0 comments on commit b4d76c6

Please sign in to comment.