-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
bundle.ts
executable file
·145 lines (127 loc) · 4.33 KB
/
bundle.ts
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
#!/usr/bin/env ../../node_modules/.bin/ts-node
import fs from 'fs-extra';
import path, { dirname, join, relative } from 'path';
import { build } from 'tsup';
import aliasPlugin from 'esbuild-plugin-alias';
import dedent from 'ts-dedent';
import slash from 'slash';
import { exec } from '../utils/exec';
const hasFlag = (flags: string[], name: string) => !!flags.find((s) => s.startsWith(`--${name}`));
const run = async ({ cwd, flags }: { cwd: string; flags: string[] }) => {
const {
name,
dependencies,
peerDependencies,
bundler: { entries = [], untypedEntries = [], platform, pre, post },
} = await fs.readJson(join(cwd, 'package.json'));
if (pre) {
await exec(`node -r ${__dirname}/../node_modules/esbuild-register/register.js ${pre}`, { cwd });
}
const reset = hasFlag(flags, 'reset');
const watch = hasFlag(flags, 'watch');
const optimized = hasFlag(flags, 'optimized');
if (reset) {
await fs.emptyDir(join(process.cwd(), 'dist'));
}
const tsConfigPath = join(cwd, 'tsconfig.json');
const tsConfigExists = await fs.pathExists(tsConfigPath);
await Promise.all([
// SHIM DTS FILES (only for development)
...(optimized
? []
: entries.map(async (file: string) => {
const { name: entryName, dir } = path.parse(file);
const pathName = join(process.cwd(), dir.replace('./src', 'dist'), `${entryName}.d.ts`);
const srcName = join(process.cwd(), file);
const rel = relative(dirname(pathName), dirname(srcName))
.split(path.sep)
.join(path.posix.sep);
await fs.ensureFile(pathName);
await fs.writeFile(
pathName,
dedent`
// dev-mode
export * from '${rel}/${entryName}';
`
);
})),
// BROWSER EMS
build({
silent: true,
entry: [...entries, ...untypedEntries].map((e: string) => slash(join(cwd, e))),
watch,
...(tsConfigExists ? { tsconfig: tsConfigPath } : {}),
outDir: join(process.cwd(), 'dist'),
format: ['esm'],
target: 'chrome100',
clean: !watch,
platform: platform || 'browser',
esbuildPlugins: [
aliasPlugin({
process: path.resolve('../node_modules/process/browser.js'),
util: path.resolve('../node_modules/util/util.js'),
}),
],
external: [name, ...Object.keys(dependencies || {}), ...Object.keys(peerDependencies || {})],
dts:
optimized && tsConfigExists
? {
entry: entries,
resolve: true,
}
: false,
esbuildOptions: (c) => {
/* eslint-disable no-param-reassign */
c.conditions = ['module'];
c.logLevel = 'error';
c.platform = platform || 'browser';
c.legalComments = 'none';
c.minifyWhitespace = optimized;
c.minifyIdentifiers = false;
c.minifySyntax = optimized;
/* eslint-enable no-param-reassign */
},
}),
// NODE CJS
build({
silent: true,
entry: [...entries, ...untypedEntries].map((e: string) => slash(join(cwd, e))),
watch,
outDir: join(process.cwd(), 'dist'),
...(tsConfigExists ? { tsconfig: tsConfigPath } : {}),
format: ['cjs'],
target: 'node16',
platform: 'node',
clean: !watch,
external: [name, ...Object.keys(dependencies || {}), ...Object.keys(peerDependencies || {})],
esbuildOptions: (c) => {
/* eslint-disable no-param-reassign */
c.logLevel = 'error';
c.platform = 'node';
c.legalComments = 'none';
c.minifyWhitespace = optimized;
c.minifyIdentifiers = optimized;
c.minifySyntax = optimized;
/* eslint-enable no-param-reassign */
},
}),
]);
if (post) {
await exec(
`node -r ${__dirname}/../node_modules/esbuild-register/register.js ${post}`,
{ cwd },
{ debug: true }
);
}
};
const flags = process.argv.slice(2);
const cwd = process.cwd();
run({ cwd, flags }).catch((err: unknown) => {
// We can't let the stack try to print, it crashes in a way that sets the exit code to 0.
// Seems to have something to do with running JSON.parse() on binary / base64 encoded sourcemaps
// in @cspotcode/source-map-support
if (err instanceof Error) {
console.error(err.stack);
}
process.exit(1);
});