-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrslib.config.ts
More file actions
117 lines (112 loc) · 2.76 KB
/
rslib.config.ts
File metadata and controls
117 lines (112 loc) · 2.76 KB
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
import path from 'node:path';
import { performance } from 'node:perf_hooks';
import { type RsbuildPlugin, logger } from '@rsbuild/core';
import { defineConfig } from '@rslib/core';
import { minify } from '@swc/core';
import pkgJson from './package.json';
/**
* Compile runtime code to ES5
*/
const pluginGenerateMinified: (filename: string) => RsbuildPlugin = (
filename: string,
) => ({
name: 'rsbuild-plugin-compile-runtime',
setup(api) {
/**
* transform `src/runtime/${filename}.ts`
* to `dist/runtime/${filename}.js` and `dist/runtime/${filename}.min.js`
*/
async function minifyRuntimeFile(distCode: string) {
const startTime = performance.now();
const { code: minifiedRuntimeCode } = await minify(distCode, {
ecma: 5,
// allows SWC to mangle function names
module: true,
compress: {
passes: 5,
unsafe: true,
},
});
logger.success(
`minify ${filename} retry runtime code in ${(
performance.now() - startTime
).toFixed(1)} ms`,
);
return minifiedRuntimeCode;
}
api.processAssets(
{ stage: 'optimize-transfer' },
async ({ assets, compilation, compiler }) => {
const minifiedChunkFilePath = path.join(
'runtime',
`${filename}.min.js`,
);
await Promise.all(
Object.entries(assets).map(async ([_, assetSource]) => {
const code = assetSource.source().toString();
const minifiedCode = await minifyRuntimeFile(code);
compilation.emitAsset(
minifiedChunkFilePath,
new compiler.webpack.sources.RawSource(minifiedCode),
);
}),
);
},
);
},
});
export default defineConfig({
lib: [
{
syntax: 'es2021',
dts: {
bundle: true,
},
source: {
entry: {
index: 'src/index.ts',
},
},
},
{
format: 'cjs',
syntax: 'es2021',
source: {
entry: {
index: 'src/index.ts',
},
},
},
{
format: 'iife',
syntax: 'es5',
source: {
entry: {
'runtime/initialChunkRetry': 'src/runtime/initialChunkRetry.ts',
},
},
output: {
target: 'web',
},
plugins: [pluginGenerateMinified('initialChunkRetry')],
},
{
format: 'iife',
syntax: 'es5',
source: {
entry: {
'runtime/asyncChunkRetry': 'src/runtime/asyncChunkRetry.ts',
},
},
output: {
target: 'web',
},
plugins: [pluginGenerateMinified('asyncChunkRetry')],
},
],
source: {
define: {
PLUGIN_VERSION: JSON.stringify(pkgJson.version.replace(/\./g, '-')),
},
},
});