forked from volcengine/ve-tos-js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsdx.config.js
98 lines (88 loc) · 2.97 KB
/
tsdx.config.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
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
const path = require('path');
const alias = require('@rollup/plugin-alias');
const replace = require('@rollup/plugin-replace');
const builtins = require('rollup-plugin-node-builtins');
const globals = require('rollup-plugin-node-globals');
const commonjs = require('@rollup/plugin-commonjs');
const nodeResolve = require('@rollup/plugin-node-resolve');
const { babelPluginTsdx } = require('tsdx2/dist/babelPluginTsdx');
const { DEFAULT_EXTENSIONS: DEFAULT_BABEL_EXTENSIONS } = require('@babel/core');
function p(relativeSrcPath) {
return path.resolve(__dirname, 'src', relativeSrcPath);
}
module.exports = {
// This function will run for each entry/format/env combination
rollup(config, opts) {
if (!config.treeshake) {
config.treeshake = {};
}
config.treeshake.pureExternalModules = true;
{
// overwrite replace options to prevent warning message
const replacePluginIdx = config.plugins.findIndex(
it => it.name === 'replace'
);
config.plugins[replacePluginIdx] = replace({
'process.env.NODE_ENV': JSON.stringify(opts.env),
preventAssignment: true,
});
}
{
// overwrite babel for customizing node version
// copy from:
// https://github.com/formium/tsdx/blob/462af2d002987f985695b98400e0344b8f2754b7/src/createRollupConfig.ts#L187-L197
const MIN_NODE_VERSION = '10';
const babelPluginIdx = config.plugins.findIndex(
it => it.name === 'babel'
);
config.plugins[babelPluginIdx] = babelPluginTsdx({
exclude: 'node_modules/**',
extensions: [...DEFAULT_BABEL_EXTENSIONS, 'ts', 'tsx'],
passPerPreset: true,
custom: {
targets:
opts.target === 'node' ? { node: MIN_NODE_VERSION } : undefined,
extractErrors: opts.extractErrors,
format: opts.format,
},
babelHelpers: 'bundled',
});
}
config.plugins.push(
replace({
'process.env.TARGET_ENVIRONMENT': `'${opts.target}'`,
preventAssignment: true,
})
);
config.plugins.push(
replace({
'process.env.BUILD_FORMAT': `'${opts.format}'`,
preventAssignment: true,
})
);
if (opts.target === 'browser') {
config.input = p('browser-index.ts');
{
const resolverPluginIdx = config.plugins.findIndex(
it => it.name === 'node-resolve'
);
config.plugins[resolverPluginIdx] = nodeResolve.default({
browser: true,
mainFields: ['module', 'main'],
extensions: [...nodeResolve.DEFAULTS.extensions, '.jsx'],
});
}
if (opts.format === 'umd') {
const nodejsPath = path.resolve(__dirname, './src/nodejs');
config.external = (...args) => {
const realPath = path.resolve(args[1], args[0]);
if (realPath.includes(nodejsPath)) {
return true;
}
return false;
};
}
}
return config; // always return a config.
},
};