-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.js
163 lines (150 loc) · 4.33 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import postcss from 'rollup-plugin-postcss'
import tailwindcss from 'tailwindcss'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import terser from '@rollup/plugin-terser'
import fs from 'fs/promises'
import mustache from 'mustache'
import htmlMinifier from 'html-minifier'
import { brotliCompressSync, gzipSync } from 'zlib'
import esbuild from 'rollup-plugin-esbuild'
const production = !process.env.ROLLUP_WATCH || process.env.NODE_ENV == 'production';
if (production) {
process.env.NODE_ENV = 'production';
}
function html() {
return {
name: 'html',
async generateBundle(_, bundle) {
const script = bundle['bundle.js'];
const css = bundle['bundle.css'];
const template = await fs.readFile('src/index.html.mustache', 'utf8');
const html = mustache.render(template, {
script: script.code,
style: css.source
});
const minifiedHtml = !production ? html : htmlMinifier.minify(html, {
collapseBooleanAttributes: true,
collapseWhitespace: true,
decodeEntities: true,
html5: true,
processConditionalComments: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
trimCustomFragments: true,
useShortDoctype: true,
});
this.emitFile({
type: 'asset',
fileName: 'index.html',
source: minifiedHtml
});
delete bundle['bundle.js'];
delete bundle['bundle.css'];
console.log('index.html size', minifiedHtml.length, 'brotli', brotliCompressSync(Buffer.from(minifiedHtml)).length, 'gzip', gzipSync(Buffer.from(minifiedHtml)).length);
}
};
}
function favicon() {
return {
name: 'favicon',
async generateBundle() {
this.emitFile({
type: 'asset',
fileName: 'favicon.ico',
source: await fs.readFile('src/favicon.ico')
});
}
}
}
function headers(content) {
return {
name: 'headers',
async generateBundle() {
this.emitFile({
type: 'asset',
fileName: '_headers',
source: content
});
}
}
}
function routes(include) {
return {
name: 'routes',
async generateBundle() {
this.emitFile({
type: 'asset',
fileName: '_routes.json',
source: JSON.stringify({
version: 1,
include,
exclude: []
}, null, 2)
});
}
}
}
export default [{
input: 'src/main.tsx',
output: {
file: '.output/bundle.js',
format: 'es',
sourcemap: production ? false : 'inline',
indent: false
},
plugins: [
resolve(),
commonjs(),
esbuild(),
postcss({
plugins: [
tailwindcss
],
extract: true,
minimize: {
preset: ['default', {
discardComments: {
removeAll: true,
},
}]
},
sourceMap: production ? false : 'inline'
}),
favicon(),
html(),
production && terser({
compress: {
passes: 5,
unsafe_methods: true
},
mangle: {
properties: {
reserved: ['class']
}
},
ecma: 2019
}),
headers(`/
Cache-Control: max-age=3600, stale-while-revalidate=82800, stale-if-error=31536000
/favicon.ico
Cache-Control: max-age=604800, stale-if-error=31536000
`),
routes(['/api', '/left', '/right'])
]
}, {
input: 'src/api.ts',
output: {
file: '.output/_worker.js',
format: 'esm'
},
plugins: [
resolve(),
commonjs(),
esbuild()
]
}];