This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
123 lines (103 loc) · 2.81 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
import fs from "fs";
import crypto from "crypto";
import svelte from "rollup-plugin-svelte";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import del from "rollup-plugin-delete";
import html, { makeHtmlAttributes } from "@rollup/plugin-html";
import livereload from "rollup-plugin-livereload";
import { terser } from "rollup-plugin-terser";
import { minify } from "html-minifier-terser";
const isProduction = !process.env.ROLLUP_WATCH;
// TODO: possibly inline everything?
const template = ({ isProduction }) => async ({
attributes,
files,
bundle,
publicPath,
title,
}) => {
const scripts = (files.js || [])
.map(({ fileName, code }) => {
const hash = () =>
crypto.createHash("sha512").update(code).digest("hex").slice(0, 25);
const newFileName = isProduction
? fileName.replace("[hash]", hash())
: fileName;
const file = bundle[fileName];
file.fileName = newFileName;
delete bundle[fileName];
bundle[newFileName] = file;
const attrs = makeHtmlAttributes(attributes.script);
return `<script defer src="${publicPath}${newFileName}"${attrs}></script>`;
})
.join("\n");
const links = (files.css || [])
.map(({ fileName }) => {
const attrs = makeHtmlAttributes(attributes.link);
return `<link href="${publicPath}${fileName}" rel="stylesheet"${attrs}>`;
})
.join("\n");
console.log({ title });
let templateString = fs
.readFileSync("./src/index.html")
.toString()
.replace("{{HTML_ATTRIBUTES}}", makeHtmlAttributes(attributes.html))
.replace("{{HTML_LINKS}}", links)
.replace("{{HTML_SCRIPTS}}", scripts);
if (isProduction) {
templateString = minify(templateString, {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
});
}
return templateString;
};
export default {
input: "src/main.js",
output: {
sourcemap: true,
format: "iife",
name: "app",
file: "bundle-[hash].js",
},
plugins: [
isProduction &&
del({ runOnce: false, targets: ["bundle-*", "index.html"] }),
svelte({
dev: !isProduction,
// css: (css) => {
// css.write("build/bundle.css");
// },
}),
resolve({
browser: true,
dedupe: ["svelte"],
}),
commonjs(),
!isProduction && serve(),
!isProduction && livereload("."),
html({
template: template({ isProduction }),
}),
isProduction && terser(),
],
watch: {
clearScreen: false,
},
};
function serve() {
let started = false;
return {
writeBundle() {
if (!started) {
started = true;
require("child_process").spawn("npm", ["run", "start", "--", "--dev"], {
stdio: ["ignore", "inherit", "inherit"],
shell: true,
});
}
},
};
}