-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild.js
69 lines (58 loc) · 2.12 KB
/
esbuild.js
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
import esbuild from 'esbuild';
import path from 'path';
import fs from 'fs';
import * as url from 'url';
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const tsConfig = JSON.parse(fs.readFileSync('./tsconfig.json').toString());
const dist = path.join(__dirname, tsConfig.compilerOptions.outDir);
if (!fs.existsSync(dist)) {
fs.mkdirSync(dist);
}
//create pkg file
fs.writeFileSync(
path.join(__dirname, 'src', 'pkg.ts'),
`export const pkg = { name: '${process.env.npm_package_name}', version: '${process.env.npm_package_version}' };\n`
);
let makeAllPackagesExternalPlugin = {
name: 'make-all-packages-external',
setup(build) {
let filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/; // Must not start with "/" or "./" or "../"
build.onResolve({ filter }, (args) => ({ path: args.path, external: true }));
}
};
const globalConfig = {
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
minify: false,
external: ['crowdsec-client-scenarios'],
plugins: [makeAllPackagesExternalPlugin]
};
esbuild
.build({
...globalConfig,
outdir: path.join(dist, 'esm'),
splitting: true,
format: 'esm',
outExtension: { '.js': '.mjs' },
target: ['esnext']
})
.catch(() => process.exit(1));
esbuild
.build({
...globalConfig,
outdir: path.join(dist, 'cjs'),
format: 'cjs',
outExtension: { '.js': '.cjs' },
platform: 'node',
target: ['node16']
})
.catch(() => process.exit(1));
// an entry file for cjs at the root of the bundle
fs.writeFileSync(path.join(dist, 'index.mjs'), "export * from './esm/index.mjs';");
// an entry file for esm at the root of the bundle
fs.writeFileSync(path.join(dist, 'index.cjs'), "module.exports = require('./cjs/index.cjs');");
fs.writeFileSync(path.join(dist, 'index.js'), "module.exports = require('./cjs/index.cjs');");
fs.writeFileSync(path.join(dist, 'index.d.ts'), "export * from './types/index.js';");
fs.writeFileSync(path.join(dist, 'index.d.cts'), "export * from './types/index.js';");