This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
165 lines (144 loc) · 3.76 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
import commonjs from "@rollup/plugin-commonjs";
import livereload from "rollup-plugin-livereload";
import resolve from "@rollup/plugin-node-resolve";
import svelte from "rollup-plugin-svelte";
import sveltePreprocess from "svelte-preprocess";
import typescript from "@rollup/plugin-typescript";
import { terser } from "rollup-plugin-terser";
import json from "@rollup/plugin-json";
import * as child_process from "child_process";
import * as fs from "fs";
import * as path from "path";
import replace from "@rollup/plugin-replace";
import { config as configDotenv } from "dotenv";
//////////////////////////////// Environment /////////////////////////////////
const production = !process.env.ROLLUP_WATCH;
const watchMode = !production;
/////////////////////////////// Configuration ////////////////////////////////
configDotenv();
const appConfiguration = {
input: "src/app/main.ts",
output: {
sourcemap: watchMode === true,
format: "iife",
name: "app",
file: "public/app.js",
},
plugins: [
replace({
CLIENT_ID: JSON.stringify(process.env.CLIENT_ID),
API_KEY: JSON.stringify(process.env.API_KEY),
DEVELOPMENT: watchMode === true,
}),
svelte({
preprocess: sveltePreprocess(),
emitCss: false,
}),
resolve({
dedupe: ["svelte"],
}),
commonjs(),
typescript({
tsconfig: "src/app/tsconfig.json",
sourceMap: watchMode === true,
}),
json({
compact: true,
}),
// Start the server when run as:
// rollup -c -w
// (server starts after bundle has been generated for the first time).
watchMode && serve(),
// Refreshes the browser when anything in "public" has changed:
watchMode && livereload("public"),
// Minify in production
production && terser(),
],
watch: {
clearScreen: false,
},
};
const workerConfiguration = {
input: "src/worker/main.ts",
output: {
sourcemap: true,
format: "iife",
name: "worker",
file: "public/worker.js",
},
plugins: [
typescript({
tsconfig: "src/worker/tsconfig.json",
}),
resolve({
browser: true,
dedupe: ["jszip"],
}),
commonjs(),
json({
compact: true,
}),
copyJSZipBundle(),
// Minify in production
production && terser(),
],
onwarn(warning, defaultHandler) {
// Ignore a warning about exceljs (false positive)
const str = warning.toString();
if (
/Use of eval is strongly discouraged/.test(str) &&
/exceljs[.]min[.]js/.test(warning.id)
) {
return;
}
defaultHandler(warning);
},
};
export default [workerConfiguration, appConfiguration];
/**
* Livereloading server; uses `sirv-cli`
*/
function serve() {
let server;
function stopServer() {
if (server) server.kill(0);
}
return {
// Start the server on the first write to the bundle
writeBundle() {
if (server) return;
// runs sirv --dev (ignores cache headers)
server = child_process.spawn("yarn", ["run", "start", "--dev"], {
stdio: [
/*stdin */ "ignore",
/* stdout */ "inherit",
/* stderr */ "inherit",
],
shell: true,
});
process.on("SIGTERM", stopServer);
process.on("exit", stopServer);
},
};
}
/**
* Rollup plugin that copies jszip.min.js from node_modules to be a SIBLING of
* the desired bundle.
*/
function copyJSZipBundle() {
return {
name: "copy-jszip-bundle",
generateBundle(options) {
const parentDir = path.dirname(path.resolve(options.file));
const destination = path.join(parentDir, "jszip.min.js");
const source = path.join(
__dirname,
"node_modules",
"jszip",
"dist",
"jszip.min.js"
);
fs.copyFileSync(source, destination);
},
};
}