-
Notifications
You must be signed in to change notification settings - Fork 7
/
rollup.config.mjs
117 lines (108 loc) · 2.58 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
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
109
110
111
112
113
114
115
116
117
import resolve from "@rollup/plugin-node-resolve";
import { terser } from "rollup-plugin-terser";
import html2 from "rollup-plugin-html2";
import serve from "rollup-plugin-serve";
import compiler from "@ampproject/rollup-plugin-closure-compiler";
import replace from "@rollup/plugin-replace";
import { Packer } from "roadroller";
// `npm run build` -> `production` is true
// `npm run dev` -> `production` is false
const production = !process.env.ROLLUP_WATCH;
const useClosureCompiler = process.env.CLOSURE_COMPILER;
// Should be safe enough; saves ~60 bytes
const transformConstToLet = {
renderChunk(chunk) {
return chunk.replace(/\bconst\b/g, "let");
},
};
/*
const patchMinified = {
renderChunk(chunk) {
let processedChunk = chunk.trim();
if (processedChunk.endsWith(";")) {
processedChunk = processedChunk.slice(0, -1);
}
return processedChunk;
},
};
*/
const roadroller = {
renderChunk(data) {
const inputs = [
{
data,
type: "js",
action: "eval",
},
];
const options = {
maxMemoryMB: 150,
};
const packer = new Packer(inputs, options);
// packer.optimize(2) for extra compression
packer.optimize();
const { firstLine, secondLine } = packer.makeDecoder();
return firstLine + secondLine;
},
};
export default {
input: "src/index.js",
output: {
file: "dist/bundle.js",
format: "cjs",
preferConst: true,
sourcemap: false,
},
plugins: [
html2({
template: "src/index.html",
}),
resolve(),
transformConstToLet,
replace({
preventAssignment: true,
values: {
"process.env.DEBUG": JSON.stringify(!production),
},
}),
production &&
!useClosureCompiler &&
terser({
ecma: 2019,
module: true,
toplevel: true,
compress: {
passes: 5,
keep_fargs: false,
unsafe: true,
unsafe_arrows: true,
unsafe_comps: true,
unsafe_math: true,
unsafe_methods: true,
unsafe_symbols: true,
toplevel: true,
booleans_as_integers: true,
},
format: {
wrap_func_args: false,
semicolons: true,
ecma: 2019,
ascii_only: true,
},
}),
production &&
useClosureCompiler &&
compiler({
compilation_level: "ADVANCED",
}),
//patchMinified,
process.env.ROADROLLER && roadroller,
!production &&
serve({
open: true,
contentBase: "dist",
host: "localhost",
port: 8080,
}),
],
};