-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Expand file tree
/
Copy pathrollup.config.js
More file actions
135 lines (119 loc) · 4.07 KB
/
rollup.config.js
File metadata and controls
135 lines (119 loc) · 4.07 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
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
import path from "path";
import fs from "fs";
import appRootPath from "app-root-path";
import typescript from "rollup-plugin-typescript2";
import alias from "@rollup/plugin-alias";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import replace from "@rollup/plugin-replace";
import chalk from "chalk";
import { spawn, exec } from "child_process";
// Set macOS SDK environment variables for compatibility
if (process.platform === "darwin") {
process.env.MACOSX_DEPLOYMENT_TARGET =
process.env.MACOSX_DEPLOYMENT_TARGET || "14.0";
process.env.CFLAGS = process.env.CFLAGS || "-fno-modules";
process.env.CXXFLAGS = process.env.CXXFLAGS || "-fno-modules";
}
const isProduction = process.env.NODE_ENV === "production";
const devPlugins = !isProduction ? [pakeCliDevPlugin()] : [];
export default {
input: isProduction ? "bin/cli.ts" : "bin/dev.ts",
output: {
file: isProduction ? "dist/cli.js" : "dist/dev.js",
format: "es",
sourcemap: !isProduction,
banner: isProduction ? "#!/usr/bin/env node" : "",
},
watch: {
include: "bin/**",
exclude: "node_modules/**",
},
external: (id) => {
if (id === "bin/cli.ts" || id === "bin/dev.ts") return false;
if (id.startsWith(".") || path.isAbsolute(id) || id.startsWith("@/"))
return false;
return true;
},
onwarn(warning, warn) {
if (warning.code === "UNRESOLVED_IMPORT") {
return;
}
warn(warning);
},
plugins: [
typescript({
tsconfig: "./tsconfig.json",
sourceMap: !isProduction,
inlineSources: !isProduction,
noEmitOnError: false,
compilerOptions: {
target: "es2020",
module: "esnext",
moduleResolution: "node",
esModuleInterop: true,
allowSyntheticDefaultImports: true,
},
}),
json(),
commonjs(),
replace({
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
preventAssignment: true,
}),
alias({
entries: [{ find: "@", replacement: path.join(appRootPath.path, "bin") }],
}),
...devPlugins,
],
};
function pakeCliDevPlugin() {
let devChildProcess;
let cliChildProcess;
let devHasStarted = false;
// 智能检测包管理器
const detectPackageManager = () => {
if (fs.existsSync("pnpm-lock.yaml")) return "pnpm";
if (fs.existsSync("yarn.lock")) return "yarn";
return "npm";
};
return {
name: "pake-cli-dev-plugin",
buildEnd() {
const command = "node";
// Pass through arguments, ignoring the first 2 (node rollup) and filtering out rollup-specifics
// We need to keep only arguments meant for our CLI script
const args = process.argv.slice(2).filter((arg) => {
// Filter out typical rollup flags if they are mixed in
// This is a simplistic filter, might need adjustment based on how npm script invokes rollup
return !["-c", "-w", "--config", "--watch"].includes(arg);
});
const cliCmdArgs = ["./dist/dev.js", ...args];
cliChildProcess = spawn(command, cliCmdArgs, { detached: true });
cliChildProcess.stdout.on("data", (data) => {
console.log(chalk.green(data.toString()));
});
cliChildProcess.stderr.on("data", (data) => {
console.error(chalk.yellow(data.toString()));
});
cliChildProcess.on("close", async (code) => {
console.log(chalk.yellow(`cli running end with code: ${code}`));
if (devHasStarted) return;
devHasStarted = true;
const packageManager = detectPackageManager();
const command = `${packageManager} run tauri dev --config ./src-tauri/.pake/tauri.conf.json -- --features cli-build`;
devChildProcess = exec(command);
devChildProcess.stdout.on("data", (data) => {
console.log(chalk.green(data.toString()));
});
devChildProcess.stderr.on("data", (data) => {
console.error(chalk.yellow(data.toString()));
});
devChildProcess.on("close", (code) => {
console.log(chalk.yellow(`dev running end: ${code}`));
process.exit(code);
});
});
},
};
}