-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
87 lines (82 loc) · 2.41 KB
/
Copy pathrollup.config.mjs
File metadata and controls
87 lines (82 loc) · 2.41 KB
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
import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
import { execSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import url from "node:url";
const isWatching = !!process.env.ROLLUP_WATCH;
const sdPlugin = "com.warmuptill.advanced-scene-switcher.sdPlugin";
function getVersionFromGit() {
try {
const raw = execSync("git describe --tags --long", { encoding: "utf8" }).trim();
const match = raw.match(/^v?(\d+)\.(\d+)\.(\d+)-(\d+)-g[0-9a-f]+$/);
if (match) {
return `${match[1]}.${match[2]}.${match[3]}.${match[4]}`;
}
} catch {
try {
const count = execSync("git rev-list --count HEAD", { encoding: "utf8" }).trim();
return `0.0.0.${count}`;
} catch { /* not a git repo */ }
}
return "0.0.0.0";
}
const pluginVersion = getVersionFromGit();
/**
* @type {import('rollup').RollupOptions}
*/
const config = {
input: "src/plugin.ts",
output: {
file: `${sdPlugin}/bin/plugin.js`,
sourcemap: isWatching,
sourcemapPathTransform: (relativeSourcePath, sourcemapPath) => {
return url.pathToFileURL(path.resolve(path.dirname(sourcemapPath), relativeSourcePath)).href;
}
},
plugins: [
{
name: "version-from-git",
resolveId(id) {
if (id === "virtual:plugin-version") return "\0virtual:plugin-version";
},
load(id) {
if (id === "\0virtual:plugin-version") return `export const pluginVersion = "${pluginVersion}";`;
},
buildStart() {
const manifestPath = `${sdPlugin}/manifest.json`;
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
if (manifest.Version !== pluginVersion) {
manifest.Version = pluginVersion;
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, "\t") + "\n");
console.log(`Version set to ${pluginVersion}`);
}
},
},
{
name: "watch-externals",
buildStart: function () {
this.addWatchFile(`${sdPlugin}/manifest.json`);
},
},
typescript({
mapRoot: isWatching ? "./" : undefined
}),
nodeResolve({
browser: false,
exportConditions: ["node"],
preferBuiltins: true
}),
commonjs(),
!isWatching && terser(),
{
name: "emit-module-package-file",
generateBundle() {
this.emitFile({ fileName: "package.json", source: `{ "type": "module" }`, type: "asset" });
}
}
]
};
export default config;