forked from hyperbrew/bolt-cep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.es.config.ts
90 lines (84 loc) · 2.38 KB
/
vite.es.config.ts
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
import fs from "fs";
import { rollup, watch, RollupOptions, OutputOptions } from "rollup";
import nodeResolve from "@rollup/plugin-node-resolve";
import babel from "@rollup/plugin-babel";
import { jsxInclude, jsxBin, jsxPonyfill } from "vite-cep-plugin";
import { CEP_Config } from "vite-cep-plugin";
import json from "@rollup/plugin-json";
import path from "path";
const GLOBAL_THIS = "thisObj";
export const extendscriptConfig = (
extendscriptEntry: string,
outPath: string,
cepConfig: CEP_Config,
extensions: string[],
isProduction: boolean,
isPackage: boolean
) => {
console.log(outPath);
const config: RollupOptions = {
input: extendscriptEntry,
treeshake: true,
output: {
file: outPath,
sourcemap: isPackage
? cepConfig.zxp.sourceMap
: cepConfig.build?.sourceMap,
},
plugins: [
json(),
nodeResolve({
extensions,
}),
babel({
extensions,
exclude: /node_modules/,
babelrc: false,
babelHelpers: "inline",
presets: ["@babel/preset-env", "@babel/preset-typescript"],
plugins: [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-class-properties",
],
}),
jsxPonyfill(),
jsxInclude({
iife: true,
globalThis: GLOBAL_THIS,
}),
jsxBin(isPackage ? cepConfig.zxp.jsxBin : cepConfig.build?.jsxBin),
],
};
async function build() {
const bundle = await rollup(config);
await bundle.write(config.output as OutputOptions);
await bundle.close();
}
const triggerHMR = () => {
// No built-in way to trigger Vite's HMR reload from outside the root folder
// Workaround will read and save index.html file for each panel to triggger reload
console.log("ExtendScript Change");
cepConfig.panels.map((panel) => {
const tmpPath = path.join(process.cwd(), "src", "js", panel.mainPath);
if (fs.existsSync(tmpPath)) {
const txt = fs.readFileSync(tmpPath, { encoding: "utf-8" });
fs.writeFileSync(tmpPath, txt, { encoding: "utf-8" });
}
});
};
const watchRollup = async () => {
const watcher = watch(config);
watcher.on("event", ({ result }: any) => {
if (result) {
triggerHMR();
result.close();
}
});
watcher.close();
};
if (isProduction) {
build();
} else {
watchRollup();
}
};