-
Notifications
You must be signed in to change notification settings - Fork 8
/
esbuild.config.cjs
150 lines (129 loc) · 4.67 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
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
145
146
147
148
149
150
const esbuild = require('esbuild');
const { umdWrapper } = require('esbuild-plugin-umd-wrapper');
const fs = require('fs/promises');
const path = require('path');
const postcss = require('postcss');
const cssnano = require('cssnano');
const cssnanoPresetLite = require('cssnano-preset-lite');
const htmlMinifier = require('html-minifier-terser');
const exportedNames = {
react: 'React',
'react-dom': 'ReactDOM',
'ag-charts-community': 'agCharts',
'ag-charts-enterprise': 'agCharts',
'ag-charts-react': 'AgChartsReact',
};
/** @type {import('esbuild').Plugin} */
const cssPlugin = {
name: 'css',
setup(build) {
build.onLoad({ filter: /\.css$/ }, async (args) => {
const { css } = await postcss([cssnano({ preset: cssnanoPresetLite })]).process(
await fs.readFile(args.path, 'utf8')
);
return { contents: css, loader: 'text' };
});
},
};
/** @type {import('esbuild').Plugin} */
const htmlPlugin = {
name: 'html',
setup(build) {
build.onLoad({ filter: /\.html$/ }, async (args) => {
const html = await htmlMinifier.minify(await fs.readFile(args.path, 'utf8'), {
collapseWhitespace: true,
});
return { contents: html, loader: 'text' };
});
},
};
/** @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) => {
if (result.errors?.length !== 0) return;
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 = [cssPlugin, htmlPlugin];
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;