forked from ag-grid/ag-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.config.cjs
96 lines (81 loc) · 2.91 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
const esbuild = require('esbuild');
const { umdWrapper } = require('esbuild-plugin-umd-wrapper');
const fs = require('fs/promises');
const path = require('path');
/** @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));
});
},
};
const exportedNames = {
'ag-grid-community': 'agGrid',
'ag-grid-enterprise': 'agGrid',
'ag-grid-charts-enterprise': 'agGrid',
'ag-grid-react': 'AgGridReact',
};
const exportedName = exportedNames[process.env.NX_TASK_TARGET_PROJECT];
const plugins = [];
let outExtension = {};
if (process.env.NX_TASK_TARGET_TARGET?.endsWith('umd')) {
plugins.push(umdWrapper({ libraryName: exportedName }));
outExtension = {
'.cjs': '.js',
};
} else {
outExtension = {
'.cjs': '.cjs.js',
'.js': '.esm.mjs',
};
}
let alias = {};
if (
process.env.NX_TASK_TARGET_PROJECT === 'ag-grid-enterprise' ||
process.env.NX_TASK_TARGET_PROJECT === 'ag-grid-charts-enterprise'
) {
alias = {
'@ag-grid-community/core': 'ag-grid-community',
'@ag-grid-community/client-side-row-model': 'ag-grid-community',
'@ag-grid-community/csv-export': 'ag-grid-community',
'@ag-grid-community/infinite-row-model': 'ag-grid-community',
};
}
plugins.push(postBuildMinificationPlugin);
/** @type {import('esbuild').BuildOptions} */
const options = {
outExtension,
plugins,
alias,
};
module.exports = options;