-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.mjs
200 lines (186 loc) · 4.91 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import typescript from "@rollup/plugin-typescript";
import resolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import copy from "rollup-plugin-copy";
import { promisify } from "util";
import { exec } from "child_process";
import packageJson from "./package.json" assert { type: "json" };
const execAsync = promisify(exec);
const basicPlugins = () => [
buildInfo(),
resolve(),
typescript({ sourceMap: true, inlineSources: true }),
];
const umdOutput = (path, umdName = undefined) => ({
file: `./dist/${path}.js`,
format: "umd",
name: umdName,
plugins: [
terser({
keep_classnames: true,
keep_fnames: true,
}),
],
sourcemap: true,
// sourcemapExcludeSources: true,
// sourcemapPathTransform: transformSourcemapPath(),
});
/** @type {() => import('rollup').RollupOptions[]} */
const rollupConfig = (args) => {
const id = args.id;
const buildFor = (x) => !id || id == x;
delete args.id;
return [
...["main", "bundle"].map(
(id) =>
buildFor(id) && {
input: "./src/main.ts",
output: umdOutput(id, "mcloud"),
external: id === "main" ? ["@yuuza/webfx"] : [],
plugins: [
...basicPlugins(),
textLoader(),
jsonLoader(),
copy({
targets: [
{
src: ["index.html", "resources/overlay.html"],
dest: "./dist/",
transform: (content, path) =>
content.toString().replace("{{BUILD_DATE}}", _buildDate),
},
{
src: ["resources/app_icon.svg", "resources/manifest.json"],
dest: "./dist/resources/",
},
],
}),
],
context: "window",
},
),
buildFor("sw") && {
input: "./src/ServiceWorker/sw.ts",
output: umdOutput("sw.bundle"),
plugins: basicPlugins(),
},
buildFor("electron") && {
input: "./src/Electron/main.ts",
output: {
file: "./dist/electron.main.cjs",
format: "commonjs",
plugins: [],
sourcemap: true,
},
external: ["electron"],
plugins: basicPlugins(),
},
buildFor("electron") && {
input: "./src/Electron/rendererPreload.ts",
output: {
file: "./dist/electron.preload.js",
format: "commonjs",
sourcemap: true,
},
external: ["electron"],
plugins: basicPlugins(),
},
buildFor("overlay") && {
input: "./src/Overlay/main.ts",
output: umdOutput("overlay.bundle"),
plugins: [...basicPlugins(), textLoader(), jsonLoader()],
},
].filter((x) => !!x);
};
let _buildInfo = null;
const _buildDate = new Date().toISOString();
async function getBuildInfo() {
return (
_buildInfo ||
(_buildInfo = JSON.stringify({
version: packageJson.version,
buildDate: _buildDate,
commits: await getCommits(),
}))
);
}
async function getCommits() {
var execResult = await execAsync('git log --pretty=format:"%h %cI %s" -n 1');
return execResult.stdout.split("\n").map((x) => {
var s = x.split(" ", 2);
return {
id: s[0],
date: s[1],
message: x.substr(s[0].length + 1 + s[1].length + 1),
};
});
}
function buildInfo() {
return {
name: "gen-build-info",
resolveId(source, importer) {
if (source === "./buildInfo") {
return source;
}
return null;
},
async load(id) {
if (id === "./buildInfo") {
const info = await getBuildInfo();
console.info("buildInfo: " + info);
return "export default " + info + ";";
}
return null;
},
};
}
function transformSourcemapPath() {
return (rel, path) => {
rel = rel.replace(/\\/g, "/");
if (rel.startsWith("node_modules")) {
var mat = rel.match(/node_modules\/((?:@[\w\-_]+\/)?[\w\-_]+)\/(.*)$/);
if (!mat) {
console.warn(["sourcemapPathTransform", rel]);
}
var version = require(mat[1] + "/package.json").version;
return `https://cdn.jsdelivr.net/npm/${mat[1]}@${version}/${mat[2]}`;
}
return `https://github.com/lideming/MusicCloud/raw/dev/${rel}`;
};
}
function textLoader() {
function match(id) {
return /\.(css|svg)$/.test(id);
}
return {
name: "my-text-loader",
transform(code, id) {
if (match(id)) {
return {
code: "export default " + JSON.stringify(code),
map: { mappings: "" },
};
}
},
};
}
function jsonLoader() {
return {
name: "json-loader",
transform(code, id) {
if (id.endsWith(".json")) {
return {
code:
"export default JSON.parse(" +
JSON.stringify(JSON.stringify(JSON.parse(code))) +
");",
map: { mappings: "" },
};
}
},
};
}
export default (args) => {
const r = rollupConfig(args);
return r;
};