-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
85 lines (74 loc) · 2.85 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
import pluginTypeScript from "@rollup/plugin-typescript";
import pluginNodeResolve from "@rollup/plugin-node-resolve";
import pluginJson from "@rollup/plugin-json";
import pluginHtml from "rollup-plugin-html";
import pluginCss from "rollup-plugin-import-css";
import pluginExecute from "rollup-plugin-execute";
import typescript from "typescript";
import pkg from "./package.json" assert { type: "json" };
import requireJson from "./assets/require.json" assert { type: "json" };
const globalPkgs = requireJson.reduce((acc, pkg) => {
acc[pkg.pkgName] = pkg.global;
return acc;
}, {});
const externalPkgs = requireJson.map(pkg => pkg.pkgName);
const outputDir = "dist";
const outputFile = getOutputFileName();
/** @param {string} [suffix] */
function getOutputFileName(suffix) {
return `${pkg.userscriptName}${suffix ?? ""}.user.js`;
}
export default (/**@type {import("./src/types").RollupArgs}*/ args) => (async () => {
/** @type {import("./src/types").RollupArgs} */
const passCliArgs = {
mode: args["config-mode"] ?? (process.env.NODE_ENV === "production" ? "production" : "development"),
host: args["config-host"] ?? "github",
assetSource: args["config-assetSource"] ?? "github",
suffix: args["config-suffix"] ?? "",
};
const passCliArgsStr = Object.entries(passCliArgs).map(([key, value]) => `--${key}=${value}`).join(" ");
const { mode, suffix } = passCliArgs;
const linkedPkgs = requireJson.filter((pkg) => pkg.link === true);
/** @type {import("rollup").RollupOptions} */
const config = {
input: "src/index.ts",
plugins: [
pluginNodeResolve({
extensions: [".ts", ".mts", ".json"],
}),
pluginTypeScript({
typescript,
sourceMap: mode === "development",
}),
pluginJson(),
pluginHtml(),
pluginCss({
output: "global.css",
}),
pluginExecute([
`npm run --silent post-build -- ${passCliArgsStr}`,
// #MARKER run own commands after build:
// ...(mode === "development" ? ["npm run --silent invisible -- \"echo 'dev-only command'\""] : []),
]),
],
output: {
file: `${outputDir}/${getOutputFileName(suffix)}`,
format: "iife",
sourcemap: mode === "development",
compact: mode === "development",
globals: linkedPkgs.length > 0 ? Object.fromEntries(
Object.entries(globalPkgs).filter(([key]) => !linkedPkgs.some((pkg) => pkg.pkgName === key))
) : globalPkgs,
},
onwarn(warning) {
// ignore circular dependency warnings
if(warning.code !== "CIRCULAR_DEPENDENCY") {
const { message, ...rest } = warning;
console.error(`\x1b[33m(!)\x1b[0m ${message}\n`, rest);
}
},
external: linkedPkgs.length > 0 ? externalPkgs.filter(p => !linkedPkgs.map(lp => lp.pkgName).includes(p)) : externalPkgs,
};
return config;
})();
export { outputDir, outputFile };