-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
68 lines (63 loc) · 2.02 KB
/
rollup.config.mjs
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
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import terser from '@rollup/plugin-terser';
import tsConfigJson from './tsconfig.json' assert { type: 'json' };
const outputBaseDir = 'dist'
const outputBaseFileName = 'index'
const outputFormats = ['cjs', 'umd', 'es'];
const outputEnvironments = ['production', 'development']
const outputFormatsMap = Object.fromEntries(outputEnvironments.map(environment => {
return [environment, Object.fromEntries(outputFormats.map(format => {
const environmentSuffix = environment === 'production' ? '' : `.${environment}`
const outputFileName = `${outputBaseFileName}.${format}${environmentSuffix}.js`
return [format, `${outputBaseDir}/${outputFileName}`]
}))]
}))
const commonPlugins = [commonjs(), nodeResolve()]
const commonExternals = ['cross-fetch']
const commonName = 'fetchMagic'
const buildTsConfig = ({ format, sourceMap }) => {
const extraConfig =
format === 'umd'
? { compilerOptions: { target: 'es5', sourceMap } }
: { compilerOptions: { sourceMap } };
return typescript({ ...tsConfigJson, ...extraConfig });
};
const productionConfigs = outputFormats.map(format => ({
input: 'src/index.ts',
output: [
{
file: outputFormatsMap['production'][format],
format,
sourcemap: tsConfigJson.compilerOptions.sourceMap,
name: commonName,
compact: true,
},
],
external: commonExternals,
plugins: [
...commonPlugins,
buildTsConfig({
format,
sourceMap: tsConfigJson.compilerOptions.sourceMap,
}),
terser(),
],
}));
const developmentConfigs = outputFormats.map(format => ({
input: 'src/index.ts',
output: [
{
file: outputFormatsMap['development'][format],
format,
name: commonName,
},
],
external: commonExternals,
plugins: [...commonPlugins, buildTsConfig({ format, sourceMap: false })],
}));
export default [
...productionConfigs,
...developmentConfigs,
];