-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.mjs
43 lines (36 loc) · 1.26 KB
/
build.mjs
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
import fs from 'node:fs';
import { build } from 'esbuild';
import packageJson from './package.json' assert { type: 'json' };
function getExternalDependencies(allow = []) {
const deps = packageJson.dependencies ? Object.keys(packageJson.dependencies).filter(dep => !allow.includes(dep)) : [];
const peerDeps = packageJson.peerDependencies ? Object.keys(packageJson.peerDependencies).filter(dep => !allow.includes(dep)) : [];
return [...deps, ...peerDeps];
}
async function buildModule() {
const shared = {
platform: 'node',
entryPoints: ['source/index.ts'],
bundle: true,
minify: true,
treeShaking: true,
sourcemap: true
};
await build({
...shared,
outfile: 'bundled/commonjs/index.js',
format: 'cjs',
external: getExternalDependencies(['chalk'])
});
await build({
...shared,
outfile: 'bundled/esm/index.esm.js',
format: 'esm',
external: getExternalDependencies([])
});
}
function generateCommonjsPackageJson() {
const packageJsonCommonJs = JSON.stringify({ ...packageJson, type: undefined }, null, 2);
fs.writeFileSync('./bundled/commonjs/package.json', packageJsonCommonJs);
}
await buildModule();
generateCommonjsPackageJson();