-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtsup.base.config.ts
More file actions
117 lines (110 loc) · 4.31 KB
/
tsup.base.config.ts
File metadata and controls
117 lines (110 loc) · 4.31 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
import { type Options } from 'tsup';
import { babelPlugin, defaultPredicate, type Predicate } from './esbuildBabelPluginIstanbul';
import { cssPlugin } from './esbuildPlugins.mjs';
type Target = Exclude<Options['target'], Array<unknown> | undefined>;
const env = process.env.NODE_ENV || 'development';
const { npm_package_version } = process.env;
const istanbulPredicate: Predicate = args => defaultPredicate(args) && !/\.worker\.[cm]?[jt]s$/u.test(args.path);
type EsbuildPlugin = NonNullable<Options['plugins']>[number];
const disablePlugin = (pluginName: string): EsbuildPlugin => ({
name: `disable-plugin-${pluginName}`,
esbuildOptions: options => {
const plugin = options.plugins?.find(({ name }) => name === pluginName);
if (plugin) {
plugin.setup = () => Promise.resolve();
}
}
});
function applyConfig(
overrideOptions: (
options: Omit<Options, 'entry'> & {
define: Record<string, string>;
esbuildPlugins: EsbuildPlugin[];
target: Target[];
}
) => Options & {
define: Record<string, string>;
esbuildPlugins: EsbuildPlugin[];
target: Target[];
}
): Options & {
define: Record<string, string>;
esbuildPlugins: EsbuildPlugin[];
target: Target[];
} {
return overrideOptions({
define: {
// TSD does not support define, thus we need to use `globalThis.*` instead.
'globalThis.WEB_CHAT_BUILD_INFO_BUILD_TOOL': '"tsup"',
'globalThis.WEB_CHAT_BUILD_INFO_MODULE_FORMAT': '"unknown"',
'globalThis.WEB_CHAT_BUILD_INFO_VERSION': JSON.stringify(process.env.npm_package_version || '0.0.0-unknown')
},
dts: true,
env: {
node_env: env,
NODE_ENV: env,
...(npm_package_version ? { npm_package_version } : {})
},
esbuildOptions: options => {
// esbuild don't touch AMD but it also don't remove AMD glue code.
// Some of our packages prefers AMD over CJS via UMD and it also use anonymous modules.
// This combination conflict with RequireJS if it present in the system.
// We are removing AMD glue code manually, just like how Rollup does.
// Read more at https://github.com/evanw/esbuild/issues/1348.
// Also https://github.com/rollup/plugins/blob/e1a5ef99f1578eb38a8c87563cb9651db228f3bd/packages/commonjs/src/transform-commonjs.js#L328.
// Test case at /__tests__/html2/hosting/requirejs.html.
options.define = options.define || {};
options.define.define = 'undefined';
options.legalComments = 'linked';
},
esbuildPlugins:
env === 'test'
? [
babelPlugin({
// ESBuild use Go regular expressions and does not understand Unicode flag.
// eslint-disable-next-line require-unicode-regexp
filter: /\.[cm]?js$/,
loader: 'jsx',
name: 'babel-plugin-istanbul:js',
predicate: istanbulPredicate
}),
babelPlugin({
// ESBuild use Go regular expressions and does not understand Unicode flag.
// eslint-disable-next-line require-unicode-regexp
filter: /\.jsx$/,
loader: 'jsx',
name: 'babel-plugin-istanbul:jsx',
predicate: istanbulPredicate
}),
babelPlugin({
// ESBuild use Go regular expressions and does not understand Unicode flag.
// eslint-disable-next-line require-unicode-regexp
filter: /\.[cm]?ts$/,
loader: 'ts',
name: 'babel-plugin-istanbul:ts',
predicate: istanbulPredicate
}),
babelPlugin({
// ESBuild use Go regular expressions and does not understand Unicode flag.
// eslint-disable-next-line require-unicode-regexp
filter: /\.tsx$/,
loader: 'tsx',
name: 'babel-plugin-istanbul:tsx',
predicate: istanbulPredicate
}),
cssPlugin
]
: [cssPlugin],
format: 'esm',
ignoreWatch: './dist/',
loader: { '.js': 'jsx' },
metafile: true,
minify: env === 'production' || env === 'test',
platform: 'browser',
plugins: [disablePlugin('postcss'), disablePlugin('svelte')],
sourcemap: true,
splitting: true,
target: ['chrome100', 'firefox100', 'safari15'] satisfies Target[]
});
}
export { applyConfig };