-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathesbuild.mjs
More file actions
124 lines (110 loc) · 3.7 KB
/
esbuild.mjs
File metadata and controls
124 lines (110 loc) · 3.7 KB
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
import { readFileSync } from 'node:fs';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';
const __dirname = dirname(fileURLToPath(import.meta.url));
const packagePath = process.cwd();
const files = process.argv.slice(2);
if (files.length === 0) {
throw new Error('must pass filename of entrypoints');
}
const [tsconfig, packageJson] = [
readFileSync(`${__dirname}/tsconfig.json`, 'utf8'),
readFileSync(`${packagePath}/package.json`, 'utf8'),
].map(JSON.parse);
const external = {
'@jridgewell/gen-mapping': 'genMapping',
'@jridgewell/remapping': 'remapping',
'@jridgewell/source-map': 'sourceMap',
'@jridgewell/sourcemap-codec': 'sourcemapCodec',
'@jridgewell/trace-mapping': 'traceMapping',
'@jridgewell/resolve-uri': 'resolveURI',
};
const externalSpec = /^[^./]/;
/** @type {esbuild.Plugin} */
const externalize = {
name: 'externalize',
setup(build) {
build.onResolve({ filter: externalSpec }, ({ path }) => {
if (!external[path]) {
throw new Error(`unregistered external module "${path}"`);
}
return { path, external: true };
});
},
};
/** @type {esbuild.Plugin} */
const umd = {
name: 'umd',
setup(build) {
const dependencies = Object.keys(packageJson.dependencies || {});
const browserDeps = dependencies.map((d) => `global.${external[d]}`);
const requireDeps = dependencies.map((d) => `require('${d}')`);
const amdDeps = dependencies.map((d) => `'${d}'`);
const locals = dependencies.map((d) => `require_${external[d]}`);
const browserGlobal = external[packageJson.name];
// Babel still supports Node v6, which doesn't support trailing commas, so we prepend an empty
// item to have it insert a comma after the last item in the static syntax list.
browserDeps.unshift('');
requireDeps.unshift('');
amdDeps.unshift('');
locals.unshift('');
build.initialOptions.banner = {
js: `
(function (global, factory) {
if (typeof exports === 'object' && typeof module !== 'undefined') {
factory(module${requireDeps.join(', ')});
module.exports = def(module);
} else if (typeof define === 'function' && define.amd) {
define(['module'${amdDeps.join(', ')}], function(mod) {
factory.apply(this, arguments);
mod.exports = def(mod);
});
} else {
const mod = { exports: {} };
factory(mod${browserDeps.join(', ')});
global = typeof globalThis !== 'undefined' ? globalThis : global || self;
global.${browserGlobal} = def(mod);
}
function def(m) { return 'default' in m.exports ? m.exports.default : m.exports; }
})(this, (function (module${locals.join(', ')}) {
`.trim(),
};
build.initialOptions.footer = {
js: '}));',
};
build.onResolve({ filter: externalSpec }, ({ path }) => {
if (!external[path]) {
throw new Error(`unregistered external module "${path}"`);
}
return { path, namespace: 'umd' };
});
build.onLoad({ filter: /.*/, namespace: 'umd' }, ({ path }) => {
return {
contents: `module.exports = require_${external[path]}`,
};
});
},
};
async function build(esm) {
const build = await esbuild.build({
entryPoints: files.map((f) => `src/${f}`),
outdir: 'dist',
bundle: true,
sourcemap: 'linked',
sourcesContent: false,
format: esm ? 'esm' : 'cjs',
plugins: esm ? [externalize] : [umd],
outExtension: esm ? { '.js': '.mjs' } : { '.js': '.umd.js' },
target: tsconfig.compilerOptions.target,
});
if (build.errors.length > 0) {
for (const message of build.errors) {
console.error(message);
}
process.exit(1);
}
console.log(`Compiled ${esm ? 'esm' : 'cjs'}`);
}
build(true);
build(false);