-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsup.ui.config.ts
58 lines (54 loc) · 1.4 KB
/
tsup.ui.config.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
import type { PluginBuild } from 'esbuild';
import { defineConfig } from 'tsup';
export default defineConfig({
define: {
'process.env.NODE_ENV': '"production"',
},
entry: { 'ui/build': 'src/ui/WebpackRendererError.tsx' },
esbuildPlugins: [
importAsGlobals({
react: 'React',
'react-dom': 'ReactDOM',
'react-plugin': 'ReactPlugin',
}),
],
format: 'esm',
splitting: false,
});
// From https://github.com/evanw/esbuild/issues/337#issuecomment-954633403
function importAsGlobals(mapping: Record<string, string>) {
// https://stackoverflow.com/a/3561711/153718
const escRe = (s: string) => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const filter = new RegExp(
Object.keys(mapping)
.map((mod) => `^${escRe(mod)}$`)
.join('|')
);
return {
name: 'global-imports',
setup(build: PluginBuild) {
build.onResolve({ filter }, (args) => {
if (!mapping[args.path]) {
throw new Error('Unknown global: ' + args.path);
}
return {
path: args.path,
namespace: 'external-global',
};
});
build.onLoad(
{
filter,
namespace: 'external-global',
},
async (args) => {
const global = mapping[args.path];
return {
contents: `module.exports = ${global};`,
loader: 'js',
};
}
);
},
};
}