-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.mjs
176 lines (143 loc) · 4.75 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
// @ts-check
import path from 'path';
import fs from 'fs';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
import minifyHTML from 'rollup-plugin-minify-html-literals';
import modulepreload from 'rollup-plugin-modulepreload';
import notify from 'rollup-plugin-notify';
import license from 'rollup-plugin-license';
import watchAssets from 'rollup-plugin-watch-assets';
import { minify } from 'html-minifier-terser';
import { visualizer } from 'rollup-plugin-visualizer';
import { copy } from '@web/rollup-plugin-copy';
import { generateSW } from 'rollup-plugin-workbox';
import { terser } from 'rollup-plugin-terser';
import { rollupPluginHTML as html } from '@web/rollup-plugin-html';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
function onwarn(warning, warn) {
if (warning.code === 'THIS_IS_UNDEFINED') return;
else warn(warning);
}
const isProduction = arg => arg.includes('production');
const PRODUCTION =
process.env.NODE_ENV === 'production' ||
process.env.NETLIFY_ENV === 'production' ||
process.env.PRODUCTION === 'true' ||
process.argv.some(isProduction);
const WATCH = process.argv.includes('-w') || process.argv.includes('--watch');
console.log('Production?', PRODUCTION);
console.log('Watch mode?', WATCH);
export default {
onwarn,
preserveEntrySignatures: false,
treeshake: !!PRODUCTION,
input: 'index.html',
output: {
dir: 'public',
format: 'es',
sourcemap: true,
},
plugins: [
{
// needed to specifically use the browser bundle for subscriptions-transport-ws
name: 'use-browser-for-subscriptions-transport-ws',
resolveId(id) {
if (id === 'subscriptions-transport-ws')
return path.resolve('node_modules/subscriptions-transport-ws/dist/client.js');
},
},
WATCH ? null : license({
thirdParty: {
includePrivate: true,
output: {
file: 'client/dependencies.json',
template(dependencies) {
return JSON.stringify(dependencies, null, 2);
},
},
},
}),
commonjs(),
html({
rootDir: path.join(process.cwd(), 'client'),
transformHtml: html => !PRODUCTION ? html : minify(html, {
collapseWhitespace: true,
removeComments: true,
removeScriptTypeAttributes: false,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyCSS: false,
minifyJS: true,
}),
}),
{
name: 'transform-preload-assets',
writeBundle: () => {
const INDEX = new URL('./public/index.html', import.meta.url);
const ASSETS = fs.readdirSync(new URL('./public/assets', import.meta.url));
const ASSETS_NO_HASHES = ASSETS.map(x => x.replace(/-\w+\./, '.'));
const getAsset = pre =>
ASSETS[ASSETS_NO_HASHES.findIndex(x => x === pre)] ||
pre;
const preloadReplacer = (_, g1) => `href="${getAsset(g1)}"`;
const srcReplacer = (_, g1) => `src="assets/${getAsset(g1)}"`;
const preTransform = fs.readFileSync(INDEX, 'utf8');
fs.writeFileSync(
INDEX,
preTransform
.replace(/href="(\w+)"/, preloadReplacer)
.replace(/src="((?:\w|\.)+)"/g, srcReplacer),
'utf8',
);
},
},
resolve(),
json(),
watchAssets({ assets: ['client/index.html', 'client/style.css'] }),
copy({ patterns: 'LICENSE.md' }),
replace({
preventAssignment: true,
PROTOCOL_SUFFIX: PRODUCTION ? 's' : '',
}),
WATCH ? generateSW({
swDest: 'public/sw.js',
globPatterns: ['nothing'],
globDirectory: 'public',
globIgnores: ['**/*'],
runtimeCaching: [
{
// You can use a RegExp as the pattern:
urlPattern: '*',
handler: 'NetworkOnly',
},
],
})
: generateSW(require('./workbox-config.cjs')),
// modulepreload({ index: 'public/index.html', prefix: 'module' }),
...(PRODUCTION && !WATCH ? [
// @ts-expect-error: ugh..
(typeof minifyHTML === 'function' ? minifyHTML : minifyHTML.default)({
failOnError: true,
options: {
shouldMinify(template) {
if (template.tag)
return template.tag.toLowerCase().includes('html');
else {
return template.parts.some(part => (
part.text.includes('<style') ||
part.text.includes('<dom-module')));
}
},
},
}),
terser({ mangle: false, format: { comments: false } }),
] : [
WATCH ? null : visualizer({ sourcemap: true }),
WATCH ? null : notify(),
]),
],
};