-
Notifications
You must be signed in to change notification settings - Fork 318
/
rollup.common.js
106 lines (95 loc) · 2.57 KB
/
rollup.common.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
99
100
101
102
103
104
105
106
import resolve from '@rollup/plugin-node-resolve';
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import { visualizer } from 'rollup-plugin-visualizer';
import multiInput from 'rollup-plugin-multi-input';
import path from 'path';
const ESM_OUTPUT_DIR = './target/esm';
const extensions = ['.js', '.ts'];
// External dependencies should also exist in the package.json dependencies
// Users of the ESM module will install their own copy
const external = [
'@okta/okta-auth-js',
'@sindresorhus/to-milliseconds',
'clipboard',
'cross-fetch',
'jquery',
'parse-ms',
'q',
'u2f-api-polyfill'
];
let entries = {
'default': 'src/exports/default.ts',
'classic': 'src/exports/classic.ts',
'oie': 'src/exports/oie.ts',
};
let preserveModules = true;
const combinedOutputDir = true; // all entries share an output dir
function getOuptutDir(entryName) {
return combinedOutputDir ? `${ESM_OUTPUT_DIR}` : `${ESM_OUTPUT_DIR}/${entryName}`;
}
// if ENTRY env var is passed, filter the entries to include only the named ENTRY
if (process.env.ENTRY) {
entries = {
[process.env.ENTRY]: entries[process.env.ENTRY]
};
}
const output = {
dir: `${ESM_OUTPUT_DIR}`,
format: 'es',
sourcemap: true,
preserveModules
};
const getPlugins = (entryName) => {
let plugins = [
babel({
babelHelpers: 'bundled',
extensions
}),
commonjs(),
json(),
resolve({
extensions,
browser: true
}),
multiInput({
relative: 'src/',
})
];
// if ANALZYE env var is passed, output analyzer html
if (process.env.ANALYZE) {
plugins = plugins.concat([
visualizer({
sourcemap: true,
projectRoot: path.join(__dirname, './src'),
filename: `${ESM_OUTPUT_DIR}/${entryName}.analzyer.html`,
template: 'treemap' // sunburst | treemap | network
}),
]);
}
return plugins;
};
// Optimization: can we run once?
if (combinedOutputDir && !process.env.ANALYZE) {
const combined = Object.values(entries);
entries = { combined };
}
export default function buildRollupConfig(callbackFn) {
return Object.keys(entries).reduce((res, entryName) => {
const entryValue = entries[entryName];
let config = {
input: Array.isArray(entryValue) ? entryValue : [entryValue],
external,
plugins: getPlugins(entryName),
output: [
{
...output,
dir: getOuptutDir(entryName)
}
]
};
config = callbackFn ? callbackFn(config): config;
return res.concat([config]);
}, []);
}