-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
168 lines (146 loc) · 4.65 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
164
165
166
167
168
import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import css from 'rollup-plugin-css-only';
import { string } from "rollup-plugin-string";
import strip from '@rollup/plugin-strip';
import json from '@rollup/plugin-json';
const { generateSW } = require('rollup-plugin-workbox'); // https://www.npmjs.com/package/rollup-plugin-workbox
import svelteSVG from "rollup-plugin-svelte-svg";
const production = !process.env.ROLLUP_WATCH;
function serve() {
let server;
function toExit() {
if (server) server.kill(0);
}
return {
writeBundle() {
if (server) return;
server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
process.on('SIGTERM', toExit);
process.on('exit', toExit);
}
};
}
// disabling service workers saves build time in development
// use only production builds in production - else you will ship app without updated service worker and cache won't be busted
// If you need to remove service worker in local environment:
// unregister service worker in devtools -> application -> service workers
// after disabling, hard refresh the page in browser (ctrl+f5)
// check if service worker wasn't reinstalled
let USE_SERVICE_WORKER
if (production) {
USE_SERVICE_WORKER = true
}
else {
USE_SERVICE_WORKER = false
}
// if you need to debug service worker, don't forget to enable them in main.ts too
export default {
input: 'src/main.ts',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
svelte({
preprocess: sveltePreprocess(),
compilerOptions: {
// enable run-time checks when not in production
dev: !production
}
}),
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: 'bundle.css' }),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
typescript({
sourceMap: true, //the project is opensource anyway. No reason not to generate source maps
inlineSources: !production
}),
string({ // https://www.npmjs.com/package/rollup-plugin-string
// Required to be specified
include: "**/*.md",
// Undefined by default
exclude: ["**/index.html"]
}),
// plugin to import json files in js
json(),
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser(),
// strip console logs in production to increase performance
// console errors are not stripped, because they are not as frequent and may be needed
production && strip({
// documentation: https://github.com/rollup/plugins/tree/master/packages/strip
include: [ // include - incompatibility fix for svelte https://github.com/rollup/plugins/issues/42
'**/*.js',
'**/*.ts',
'**/*.svelte',
],
functions: [
'console.log',
],
}),
USE_SERVICE_WORKER && generateSW({
globPatterns: ["**/*.{jpg,png,html,js,css,json}"],
// swSrc: 'src/sw.js',
swDest: 'public/sw.js',
globDirectory: 'public/',
// handler: 'NetworkFirst',
skipWaiting: true, // https://stackoverflow.com/a/59168696/14409632
mode: 'development'
},
function render({ swDest, count, size }) {
console.log(
"service worker generated",
'📦', swDest,
'#', count,
'🐘', size,
);
}
),
// Watch if service worker was changed, then reload the page
// Service worker contains all changed files, so only changed files will be redownloaded
// you need to use Update on reload in devtools -> Application -> service workers -> Update on reload (keep cheched)
USE_SERVICE_WORKER && !production && livereload({
// more options https://www.npmjs.com/package/livereload -> Server API
delay: 0,
watch: 'public/sw.js',
applyCSSLive: false,
applyImgLive: false
}),
// if we don't use service workers
!USE_SERVICE_WORKER && !production && livereload({
delay: 0,
watch: 'public',
applyCSSLive: false,
applyImgLive: false
}),
svelteSVG()
],
watch: {
clearScreen: false
},
};