This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
117 lines (108 loc) · 3.33 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
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import ts from "@rollup/plugin-typescript";
import replace from "@rollup/plugin-replace";
import external from "rollup-plugin-peer-deps-external";
import styles from "rollup-plugin-styles";
import json from "@rollup/plugin-json";
import child_process from "child_process";
import fs from "fs";
import path from "path";
import os from "os";
import typescript from "typescript";
import { promisify } from "util";
import { findpath } from "nw";
const exec = promisify(child_process.exec);
const package_json = require("./package.json");
const tsconfig = require("./tsconfig.json");
export default {
input: "src/index.ts",
output: [
{
file: path.join(tsconfig.outDir, package_json.main),
format: "cjs",
sourcemap: process.env.NODE_ENV === "production" ? false : "inline",
},
],
plugins: [
styles(),
external(),
resolve(),
commonjs(),
ts({
typescript,
tsconfig: "./tsconfig.json",
sourceMap: process.env.NODE_ENV === "production",
inlineSources: true,
}),
json(),
replace({
"process.env.NODE_ENV": JSON.stringify(
process.env.NODE_ENV || "development"
),
}),
{
async writeBundle() {
if (!fs.existsSync(tsconfig.outDir)) {
await fs.promises.mkdir(tsconfig.outDir);
}
const [src] = tsconfig.include;
await fs.promises.copyFile(
path.join(src, "manifest.json"),
path.join(tsconfig.outDir, "package.json")
);
const script =
process.env.NODE_ENV === "production"
? `<script>nw.Window.get().evalNWBin(null, "${package_json.main.replace(
".js",
".bin"
)}");</script>`
: `<script src="${package_json.main}"></script>`;
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<title>${package_json.displayName}</title>
</head>
<body style="margin: 0px;">
${script}
</body>
</html>`;
await fs.promises.writeFile(tsconfig.outDir + "/index.html", html);
if (process.env.NODE_ENV === "production") {
let compiler = findpath();
if (os.platform() === "win32")
compiler = compiler.replace("nw.exe", "nwjc.exe");
if (os.platform() === "darwin")
compiler = compiler.replace("nwjs.app/Contents/MacOS/nwjs", "nwjc");
if (os.platform() === "linux")
compiler = compiler.replace(
"node_modules/nw/nwjs/nw",
"node_modules/nw/nwjs/nwjc"
);
await exec(
`${compiler} ${path.join(
tsconfig.outDir,
package_json.main
)} ${path.join(
tsconfig.outDir,
package_json.main.replace(".js", ".bin")
)}`
);
await fs.promises.unlink(
path.join(tsconfig.outDir, package_json.main)
);
}
if (process.env.RUN_NWJS === "true") {
exec("node --loader ts-node/esm ./start.ts");
}
},
},
],
watch: {
exclude: "node_modules/**",
include: "src/**",
},
};