-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.mjs
62 lines (53 loc) · 1.73 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import fs from 'node:fs';
import { build } from 'esbuild';
import * as importMap from "esbuild-plugin-import-map";
import packageJson from './package.json' assert { type: 'json' };
importMap.load({
imports: {
'../lib/index.js': '../lib/esm/index.esm.js'
}
});
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/lib/index.ts'],
bundle: true,
minify: true,
treeShaking: true,
sourcemap: false
};
await build({
...shared,
outfile: 'bundled/lib/commonjs/index.js',
format: 'cjs',
external: getExternalDependencies()
});
await build({
...shared,
outfile: 'bundled/lib/esm/index.esm.js',
format: 'esm',
external: getExternalDependencies()
});
await build({
...shared,
entryPoints: ['source/bin/index.ts'],
outfile: 'bundled/bin/index.js',
format: 'esm',
external: getExternalDependencies(),
plugins: [importMap.plugin()],
define: {
'__VERSION__': `"${packageJson.version}"`
}
});
}
function generateCommonjsPackageJson() {
const packageJsonCommonJs = JSON.stringify({ ...packageJson, type: undefined }, null, 2);
fs.writeFileSync('./bundled/lib/commonjs/package.json', packageJsonCommonJs);
}
await buildModule();
generateCommonjsPackageJson();