-
Notifications
You must be signed in to change notification settings - Fork 7
/
esbuild.base.ts
144 lines (137 loc) · 5.06 KB
/
esbuild.base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const esbuild = require('esbuild');
const fs = require('fs');
const ts = require('typescript');
const { lessLoader } = require('esbuild-plugin-less');
const {
getCompilerOptions,
} = require('./scripts/esbuild/getCompilerOptions.js');
const { resolveTSConfig } = require('./scripts/esbuild/resolveTSConfig.js');
const builder = async (type, entryPoints, outbase, outdir) => {
esbuild
.build({
entryPoints, // 你的入口文件
outdir,
bundle: false, // 是否打包
minify: false, // 是否压缩
outbase,
format: type,
platform: 'node', // 目标平台: 'node' 或 'browser'
target: 'es2015',
metafile: true,
plugins: [
lessLoader(),
{
name: 'dts-plugin',
async setup(build) {
const { config } = resolveTSConfig({});
const willBundleDeclarations = false;
const compilerOptions = getCompilerOptions({
tsconfig: config,
pluginOptions: { tsconfig: { outdir } },
esbuildOptions: build.initialOptions,
willBundleDeclarations,
});
const compilerHost = compilerOptions.incremental
? ts.createIncrementalCompilerHost(compilerOptions)
: ts.createCompilerHost(compilerOptions);
const inputFiles = [];
build.onLoad({ filter: /(\.tsx|\.ts)$/ }, async (args) => {
inputFiles.push(args.path);
compilerHost.getSourceFile(
args.path,
compilerOptions.target ?? ts.ScriptTarget.Latest,
true,
);
});
build.onEnd(() => {
let compilerProgram;
if (compilerOptions.incremental) {
compilerProgram = ts.createIncrementalProgram({
options: compilerOptions,
host: compilerHost,
rootNames: inputFiles,
});
} else {
compilerProgram = ts.createProgram(
inputFiles,
compilerOptions,
compilerHost,
);
}
compilerProgram.emit();
});
},
},
{
name: 'transform-import-less-to-css',
async setup(build) {
const distFile = [];
const esFile = [];
build.onLoad({ filter: /(\.tsx)$/ }, async (args) => {
const distPath = args.path
?.replace('src', 'dist')
.replace('tsx', 'js');
const esPath = args.path
?.replace('src', 'es')
.replace('tsx', 'js');
distFile.push(distPath);
esFile.push(esPath);
});
build.onEnd(async (buildResult) => {
if (buildResult.errors?.length) {
return null;
}
const { outputs } = buildResult?.metafile || {};
const assets = Object.keys(outputs);
assets.forEach((asset) => {
if (asset.endsWith('.js')) {
const assetPath = `${process.cwd()}/${asset}`;
fs.readFile(assetPath, 'utf8', (readErr, data) => {
if (readErr) console.error(`构建失败: ${readErr}`);
if (!data) return;
if (
data.match(
/(import_react\.default\.memo|import_react\.default\.forwardRef|React\.forwardRef|React\.memo|\.less)/gm,
)
) {
/**
* 1、将js中less改css引入
* 2、处理esbuild构建在forwardRef、memo场景不会注入PURE
* https://github.com/evanw/esbuild/issues/3492
*/
const result = data
.replace('.less', '.css')
.replace(
/(import_react\.default\.memo|import_react\.default\.forwardRef|React\.forwardRef|React\.memo)/gm,
`/* @__PURE__ */ $1`,
);
fs.writeFile(assetPath, result, (writeErr) => {
if (writeErr) console.error(`构建失败: ${writeErr}`);
});
}
});
if (
asset.endsWith('.types.js') &&
assetPath.includes('/es/')
) {
fs.writeFile(assetPath, 'export {};', (writeErr) => {
if (writeErr) console.error(`构建失败: ${writeErr}`);
});
}
}
});
});
},
},
],
})
.catch(() => process.exit(1));
};
const esBuilder = (entryPoints) => {
const outbase = 'src';
const esmOutdir = 'es';
const cjsOutdir = 'dist';
builder('esm', entryPoints, outbase, esmOutdir);
builder('cjs', entryPoints, outbase, cjsOutdir);
};
module.exports = esBuilder;