-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
108 lines (104 loc) · 3.23 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import jsx from 'acorn-jsx';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import commonjs from '@rollup/plugin-commonjs';
import babel from '@rollup/plugin-babel';
import { DEFAULT_EXTENSIONS } from '@babel/core';
import { terser } from 'rollup-plugin-terser';
import sizes from 'rollup-plugin-sizes';
import visualizer from 'rollup-plugin-visualizer';
import glob from 'fast-glob';
import path from 'path';
const mode =
process.env.NODE_ENV === 'production' ? 'production' : 'development';
const dev = mode === 'development';
// necessary to make multiple entries in different directory levels work.
// instead of an array of filenames, rollup can take a map where the key
// is the destination filename which is appended to the output directory
// and the value is the source filename
/*
{
'templates/default': 'src/js/templates/default.js'
}
*/
function generateInputMap(filenames, base) {
const inputMap = {};
for (let filename of filenames) {
const relativeFile = path.relative(base, filename);
const parsed = path.parse(relativeFile);
const name = path.join(parsed.dir, parsed.name);
inputMap[name] = filename;
}
return inputMap;
}
// configVisualize is set by --configVisualize in the rollup cli.
// see the build:js:visualize npm script
export default async ({ configVisualize }) => {
const inputs = ['src/js/main.js'].concat(await glob('src/js/templates/**/*.{js, jsx}'));
return {
preserveEntrySignatures: 'strict',
input: generateInputMap(inputs, 'src/js'),
output: [
{
dir: 'public/assets/js/',
format: 'es',
sourcemap: true,
},
],
// necessary to make the commonjs plugin understand jsx
acornInjectPlugins: [jsx()],
plugins: [
replace({
preventAssignment: true,
values: {
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode),
},
}),
resolve({
browser: true,
}),
commonjs({ extensions: ['.js', '.ts', '.jsx', '.tsx'] }),
babel({
exclude: 'node_modules/**',
babelHelpers: 'bundled',
extensions: [...DEFAULT_EXTENSIONS, '.ts', '.tsx'],
}),
!dev &&
terser({
module: true,
}),
sizes(),
configVisualize &&
visualizer({
sourcemap: true,
open: true,
}),
],
treeshake: { moduleSideEffects: true },
watch: {
clearScreen: false,
exclude: ['node_modules/**'],
},
// https://github.com/rollup/rollup/issues/1089#issuecomment-635564942
onwarn: (warning, rollupWarn) => {
const ignoredWarnings = [
{
ignoredCode: 'CIRCULAR_DEPENDENCY',
ignoredPath: 'node_modules/chevrotain/',
},
{
ignoredCode: 'EVAL',
ignoredPath: 'node_modules/@chevrotain/utils/lib/src/',
}
];
// only show warning when code and path don't match
// anything in above list of ignored warnings
if (!ignoredWarnings.some(({ ignoredCode, ignoredPath }) => (
warning.code === ignoredCode && (!warning.importer || warning.importer.includes(path.normalize(ignoredPath)))))
) {
rollupWarn(warning)
}
},
};
};