/
rollup.config.live.js
82 lines (79 loc) · 2.6 KB
/
rollup.config.live.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
/**
* Copyright © 2022 Tony Spegel
*/
import { generateSW } from 'rollup-plugin-workbox';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import path from 'path';
import replaceHtmlVars from 'rollup-plugin-replace-html-vars';
import summary from 'rollup-plugin-summary';
const serviceWorkerRegistration = `
<script type="module">
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker
.register('/sw.js')
.then((registration) => {
// Registration was successful
console.log(
'ServiceWorker registration successful with scope: ',
registration.scope
);
})
.catch((errorMessage) => {
// registration failed :(
console.log(
'ServiceWorker registration failed: ',
errorMessage
);
});
});
} else {
console.log('Service workers are not supported.');
}
</script>
`;
export default {
input: 'dist/index.js',
output: {
dir: 'dist/',
format: 'es',
sourcemap: false,
},
preserveEntrySignatures: 'strict',
plugins: [
// Replace token to register ServiceWorker
replaceHtmlVars({
files: 'dist/**/*.html',
from: '<!-- SW Registration -->',
to: serviceWorkerRegistration,
}),
nodeResolve(),
// Minify JS
terser({
ecma: 2020,
module: true,
warnings: true,
format: {
quote_style: 1,
},
}),
// Generate a ServiceWorker
generateSW({
globIgnores: ['polyfills/*.js', 'nomodule-*.js'],
navigateFallback: '/index.html',
// where to output the generated sw
swDest: path.join('dist', 'sw.js'),
// directory to match patterns against to be precached
globDirectory: path.join('dist'),
// cache any html js and css by default
globPatterns: ['**/*.{html,js,mjs,css,png,svg,woff2,webmanifest}'],
skipWaiting: true,
clientsClaim: true,
runtimeCaching: [
{ urlPattern: 'polyfills/*.js', handler: 'CacheFirst' },
],
}),
summary(),
],
};