-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathesbuild.config.cjs
119 lines (100 loc) · 3.64 KB
/
esbuild.config.cjs
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
const esbuild = require('esbuild');
const { umdWrapper } = require('esbuild-plugin-umd-wrapper');
const fs = require('fs/promises');
const path = require('path');
const exportedNames = {
react: 'React',
'react-dom': 'ReactDOM',
'ag-grid-community': 'agGrid',
'ag-grid-enterprise': 'agGrid',
'ag-grid-react': 'AgGridReact',
};
/** @type {import('esbuild').Plugin} */
const postBuildMinificationPlugin = {
name: 'minification-plugin',
setup(build) {
build.initialOptions.metafile = true;
/** @type {Map<string, AbortController>} */
const writeState = new Map();
/** @param {string} outputFile */
const minifyFile = async (outputFile) => {
try {
if (outputFile.endsWith('.map')) return;
writeState.get(outputFile)?.abort();
const abortController = new AbortController();
writeState.set(outputFile, abortController);
const { signal } = abortController;
const contents = await fs.readFile(path.resolve(outputFile), 'utf-8');
if (signal.aborted) return;
const minified = await esbuild.transform(contents, {
minify: true,
sourcemap: true,
});
if (signal.aborted) return;
const { name, ext } = path.parse(outputFile);
const minifiedFile = path.resolve(path.dirname(outputFile), `${name}.min${ext}`);
await Promise.all([
fs.writeFile(minifiedFile, minified.code, { signal }),
fs.writeFile(`${minifiedFile}.map`, minified.map, { signal }),
]);
} catch (e) {
if (e.name !== 'AbortError') throw e;
}
};
build.onEnd(async (result) => {
await Promise.all(Object.keys(result.metafile.outputs).map(minifyFile));
});
},
};
/** @type {import('esbuild').Plugin} */
const umdWrapperAdaptorPlugin = {
name: 'umd-wrapper-adaptor',
setup(build) {
const { initialOptions } = build;
// Creates UMD banner + footer config.
const exportedName = exportedNames[process.env.NX_TASK_TARGET_PROJECT];
const umdWrapperInstance = umdWrapper({ libraryName: exportedName });
umdWrapperInstance.setup(build);
// Correct global variable name references.
const { banner } = build.initialOptions;
for (const external of initialOptions.external ?? []) {
const globalName = exportedNames[external];
if (globalName) {
banner.js = banner.js.replaceAll(`g["${external}"]`, `g["${globalName}"]`);
}
}
// Add `require()` function which uses resolved module references.
const externalsMap =
initialOptions.external?.map((e, i) => {
return `if (name === '${e}') return __d${String.fromCharCode(97 + i)};`;
}) ?? [];
build.initialOptions.banner.js += `
if (typeof require === 'undefined') {
function require(name) {
${externalsMap.join('\n ')}
throw new Error('Unknown module: ' + name);
}
}
`;
},
};
const plugins = [];
let outExtension = {};
if (process.env.NX_TASK_TARGET_TARGET?.endsWith('umd')) {
plugins.push(umdWrapperAdaptorPlugin);
outExtension = {
'.cjs': '.js',
};
} else {
outExtension = {
'.cjs': '.cjs.js',
'.js': '.esm.mjs',
};
}
plugins.push(postBuildMinificationPlugin);
/** @type {import('esbuild').BuildOptions} */
const options = {
outExtension,
plugins,
};
module.exports = options;