-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
92 lines (84 loc) · 2.43 KB
/
rollup.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
import path from 'path';
import json from '@rollup/plugin-json';
import cleanup from 'rollup-plugin-cleanup';
import replace from '@rollup/plugin-replace';
import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import { nodeResolve } from '@rollup/plugin-node-resolve';
const byBabel = false;
const outputConfigs = {
cjs: {
format: 'cjs',
file: path.resolve(__dirname, 'dist/virtual-esm.cjs.js'),
},
'esm-bundler': {
format: 'es',
file: path.resolve(__dirname, 'dist/virtual-esm.esm-bundler.js'),
},
umd: {
format: 'umd',
file: path.resolve(__dirname, 'dist/virtual-esm.umd.js'),
},
};
const pkg = require(path.resolve(__dirname, 'package.json'));
const packageConfigs = Object.keys(outputConfigs).map((format) =>
createConfig(format, outputConfigs[format]),
);
packageConfigs.push(
createConfig('cjs', {
format: outputConfigs.cjs.format,
file: path.resolve(__dirname, 'dist/virtual-esm.cjs.prod.js'),
}),
);
function createReplacePlugin(isProductionBuild, isUmdBuild, isBundlerESMBuild) {
return replace({
__VERSION__: `"${pkg.version}"`,
__BROWSER__: Boolean(isUmdBuild),
__DEV__: isBundlerESMBuild
? '(process.env.NODE_ENV !== "production")'
: !isProductionBuild,
});
}
function createConfig(format, output) {
let external = [];
let nodePlugins = [];
const isUmdBuild = /umd/.test(format);
const isBundlerESMBuild = /esm-bundler/.test(format);
const isProductionBuild = /\.prod\.js$/.test(output.file);
const input = byBabel
? path.resolve(__dirname, 'byBabel/index.js')
: path.resolve(__dirname, `src/index.ts`);
output.externalLiveBindings = false;
if (isUmdBuild) output.name = 'VirtualModule';
if (format !== 'cjs') {
nodePlugins = [
nodeResolve({ browser: isUmdBuild }),
commonjs({ sourceMap: false }),
];
}
if (!isUmdBuild) {
external = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
];
}
return {
input,
output,
external,
plugins: [
cleanup(),
json({
namedExports: false,
}),
typescript({
clean: true, // no cache
typescript: require('typescript'),
tsconfig: path.resolve(__dirname, './tsconfig.json'),
}),
createReplacePlugin(isProductionBuild, isUmdBuild, isBundlerESMBuild),
...nodePlugins,
],
};
}
export default packageConfigs;