-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.ts
123 lines (108 loc) · 3.7 KB
/
bundle.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
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
import * as esbuild from "esbuild";
import { match } from "ts-pattern";
import { join, dirname } from "node:path";
import copy from "recursive-copy";
import { platform, homedir } from "node:os";
import { readFile, rm, mkdir, copyFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
// Roughly https://github.com/demurgos/appdata-path/blob/master/lib/index.js but appdata local and .local/share, try to match `dirs` from rust
function getAppDataLocalPath() {
const identifier = "com.ironcladapp.rivet";
return match(platform())
.with("win32", () => join(homedir(), "AppData", "Local", identifier))
.with("darwin", () =>
join(homedir(), "Library", "Application Support", identifier)
)
.with("linux", () => join(homedir(), ".local", "share", identifier))
.otherwise(() => {
if (platform().startsWith("win")) {
return join(homedir(), "AppData", "Local", identifier);
} else {
return join(homedir(), ".local", "share", identifier);
}
});
}
const syncPlugin: esbuild.Plugin = {
name: "onBuild",
setup(build) {
build.onEnd(async () => {
const packageJson = JSON.parse(
await readFile(join(__dirname, "package.json"), "utf-8")
);
const pluginName = packageJson.name;
const rivetPluginsDirectory = join(getAppDataLocalPath(), "plugins");
const thisPluginDirectory = join(
rivetPluginsDirectory,
`${pluginName}-latest`
);
await rm(join(thisPluginDirectory, "package"), {
recursive: true,
force: true,
});
await mkdir(join(thisPluginDirectory, "package"), { recursive: true });
await copy(
join(__dirname, "dist"),
join(thisPluginDirectory, "package", "dist")
);
await copyFile(
join(__dirname, "package.json"),
join(thisPluginDirectory, "package", "package.json")
);
// Copy .git to mark as locally installed plugin
await copy(
join(__dirname, ".git"),
join(thisPluginDirectory, "package", ".git")
);
console.log(
`Synced ${pluginName} to Rivet at ${thisPluginDirectory}. Refresh or restart Rivet to see changes.`
);
});
},
};
// The isomorphic dynamically imports the node entry point, so we need to rewrite the import to point to the
// bundled node entry point instead of the original place it was.
const rewriteNodeEntryPlugin: esbuild.Plugin = {
name: "rewrite-node-entry",
setup(build) {
build.onResolve({ filter: /\/nodeEntry$/ }, (args) => {
return {
external: true,
path: "../dist/nodeEntry.cjs",
};
});
},
};
const isomorphicBundleOptions: esbuild.BuildOptions = {
entryPoints: ["src/index.ts"],
bundle: true,
platform: "neutral",
target: "es2020",
outfile: "dist/bundle.js",
format: "esm",
external: ["./src/nodeEntry"],
plugins: [rewriteNodeEntryPlugin],
};
const nodeBundleOptions = {
entryPoints: ["src/nodeEntry.ts"],
bundle: true,
platform: "node",
target: "es2020",
outfile: "dist/nodeEntry.cjs",
format: "cjs",
plugins: [] as esbuild.Plugin[],
} satisfies esbuild.BuildOptions;
// TODO will node bundle always run after isomorphic bundle, or is there a race condition?
if (process.argv.includes("--sync")) {
nodeBundleOptions.plugins.push(syncPlugin);
}
if (process.argv.includes("--watch")) {
const isoContext = await esbuild.context(isomorphicBundleOptions);
await isoContext.watch();
const nodeContext = await esbuild.context(nodeBundleOptions);
await nodeContext.watch();
console.log("Watching for changes...");
} else {
await esbuild.build(isomorphicBundleOptions);
await esbuild.build(nodeBundleOptions);
}