Skip to content

Commit 5063ef5

Browse files
author
Taois
committed
feat: 研究了一下esbuild打包方案,最终失败告终
1 parent 6e03698 commit 5063ef5

File tree

7 files changed

+97
-29
lines changed

7 files changed

+97
-29
lines changed

dist/drpy-core-lite.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/drpy-core.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esbuild.config.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const esbuild = require('esbuild')
2+
const {NodeModulesPolyfillPlugin} = require('@esbuild-plugins/node-modules-polyfill')
3+
const path = require('path')
4+
5+
// 多入口配置
6+
const entryPoints = {
7+
'drpy-core': './src/drpy-core.js',
8+
'drpy-core-lite': './src/drpy-core-lite.js'
9+
}
10+
11+
// 共享配置
12+
const sharedConfig = {
13+
entryPoints,
14+
bundle: true,
15+
minify: true,
16+
sourcemap: false,
17+
target: ['es2020'],
18+
legalComments: 'none',
19+
charset: 'utf8',
20+
platform: 'browser',
21+
format: 'esm',
22+
// format: 'iife', // 使用立即执行函数
23+
// globalName: 'globalThis', // 设置全局命名空间
24+
outdir: 'dist',
25+
outExtension: {'.js': '.min.js'},
26+
keepNames: true, // 保留函数/类名
27+
alias: {
28+
'模板': path.resolve(__dirname, 'src/模板.js')
29+
},
30+
plugins: [
31+
// 处理 Node.js 模块 polyfill
32+
NodeModulesPolyfillPlugin()
33+
],
34+
loader: {
35+
'.js': 'js'
36+
},
37+
define: {
38+
'process.env.NODE_ENV': '"production"',
39+
'globalThis': 'globalThis',
40+
'window.globalThis': 'globalThis'
41+
},
42+
logOverride: {
43+
// 忽略这个特定警告
44+
'suspicious-boolean-not': 'silent'
45+
},
46+
// 关键修复:确保全局导出可用
47+
banner: {
48+
js: `const g = typeof window !== 'undefined' ? window :
49+
typeof global !== 'undefined' ? global : globalThis;`
50+
}
51+
}
52+
53+
// 执行构建
54+
esbuild.build(sharedConfig)
55+
.then(() => console.log('构建完成!'))
56+
.catch(() => process.exit(1))

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
"name": "drpy-webpack",
33
"version": "1.0.2",
44
"main": "webpack.config.js",
5-
65
"scripts": {
7-
"build": "webpack --config webpack.config.js"
6+
"build": "webpack --config webpack.config.js",
7+
"esbuild": "node esbuild.config.js"
88
},
9+
"type": "module",
910
"engines": {
1011
"node": ">21 <23"
1112
},
@@ -18,7 +19,9 @@
1819
"@babel/plugin-proposal-class-properties": "^7.18.6",
1920
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
2021
"@babel/preset-env": "^7.28.0",
22+
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
2123
"babel-loader": "^10.0.0",
24+
"esbuild": "^0.25.8",
2225
"script-loader": "^0.7.2",
2326
"terser-webpack-plugin": "^5.3.14",
2427
"webpack": "^5.100.2",

readme.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@ yarn add script-loader --dev
2727

2828
```
2929

30-
执行编译的时候需要去掉package.json里的 `"type": "module",`,执行drpy-test.js测试的时候需要加回去
30+
执行编译的时候需要去掉package.json里的 `"type": "module",`,执行drpy-test.js测试的时候需要加回去
31+
32+
### 新增esbuild打包
33+
34+
已知问题。打包后的东西全局变量不正常,没法用,先研究到这儿,后面有时间再说。
35+
唯一看中的优势就是这个打包工具比webpack来说速度贼快,速度超越 `vite` `rollup` 直逼 `rolldown`!
36+
37+
```shell
38+
yarn add esbuild @esbuild-plugins/node-modules-polyfill --dev
39+
```

src/drpy-core-lite.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ import './libs/xxhash-wasm.min.js';
1818

1919

2020
// 确保全局依赖可用
21-
22-
const gbkTool = globalThis.gbkTool;
23-
const CryptoJS = globalThis.CryptoJS;
24-
const JSEncrypt = globalThis.JSEncrypt;
25-
const NODERSA = globalThis.NODERSA; // lite版弃用
26-
const pako = globalThis.pako;
27-
const JSON5 = globalThis.JSON5;
28-
const JSONPath = globalThis.JSONPath;
29-
const jinja = globalThis.jinja;
30-
const WebAssembly = globalThis.WebAssembly;
31-
const TextEncoder = globalThis.TextEncoder;
32-
const TextDecoder = globalThis.TextDecoder;
21+
const g = globalThis;
22+
const gbkTool = g.gbkTool;
23+
const CryptoJS = g.CryptoJS;
24+
const JSEncrypt = g.JSEncrypt;
25+
const NODERSA = g.NODERSA; // lite版弃用
26+
const pako = g.pako;
27+
const JSON5 = g.JSON5;
28+
const JSONPath = g.JSONPath;
29+
const jinja = g.jinja;
30+
const WebAssembly = g.WebAssembly;
31+
const TextEncoder = g.TextEncoder;
32+
const TextDecoder = g.TextDecoder;
3333

3434
/*
3535
patch打补丁开始

src/drpy-core.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ import './libs/encoding.min.js'
1616
import './libs/xxhash-wasm.min.js';
1717

1818
// 确保全局依赖可用
19-
20-
const gbkTool = globalThis.gbkTool;
21-
const CryptoJS = globalThis.CryptoJS;
22-
const JSEncrypt = globalThis.JSEncrypt;
23-
const NODERSA = globalThis.NODERSA;
24-
const pako = globalThis.pako;
25-
const JSON5 = globalThis.JSON5;
26-
const JSONPath = globalThis.JSONPath;
27-
const jinja = globalThis.jinja;
28-
const WebAssembly = globalThis.WebAssembly;
29-
const TextEncoder = globalThis.TextEncoder;
30-
const TextDecoder = globalThis.TextDecoder;
19+
const g = globalThis;
20+
const gbkTool = g.gbkTool;
21+
const CryptoJS = g.CryptoJS;
22+
const JSEncrypt = g.JSEncrypt;
23+
const NODERSA = g.NODERSA;
24+
const pako = g.pako;
25+
const JSON5 = g.JSON5;
26+
const JSONPath = g.JSONPath;
27+
const jinja = g.jinja;
28+
const WebAssembly = g.WebAssembly;
29+
const TextEncoder = g.TextEncoder;
30+
const TextDecoder = g.TextDecoder;
3131

3232

3333
// const cheerio = {

0 commit comments

Comments
 (0)