forked from ScelarOrg/Nodepod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.lib.config.js
More file actions
104 lines (100 loc) · 3.2 KB
/
vite.lib.config.js
File metadata and controls
104 lines (100 loc) · 3.2 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
import { defineConfig } from "vite";
import topLevelAwait from "vite-plugin-top-level-await";
import wasm from "vite-plugin-wasm";
import { resolve } from "path";
import { readFileSync } from "fs";
import { build as esbuild } from "esbuild";
const pkg = JSON.parse(
readFileSync(resolve(__dirname, "package.json"), "utf-8"),
);
// Only peer deps and Node.js builtins are external.
// Runtime deps (pako, acorn, etc.) are inlined so the bundle is self-contained
// and works in any environment (bundler, browser, etc.) without extra config.
const peerDeps = Object.keys(pkg.peerDependencies || {});
const allExternal = [
...peerDeps,
/^node:/,
// Framework integrations: keep these external so users' own copies are
// used and rollup doesn't choke on `next/server` when `next` isn't
// installed locally.
"vite",
"next",
"next/server",
];
/**
* Vite plugin that pre-bundles process-worker-entry.ts into a self-contained
* JS string. This is necessary because consumers of nodepod (Next.js, Webpack,
* etc.) can't resolve Vite-specific worker chunk URLs. Instead, we embed the
* entire worker bundle as a string and create Blob URL workers at runtime.
*/
function inlineProcessWorkerPlugin() {
const VIRTUAL_ID = "virtual:process-worker-bundle";
const RESOLVED_ID = "\0" + VIRTUAL_ID;
let workerBundle = "";
return {
name: "inline-process-worker",
async buildStart() {
const result = await esbuild({
entryPoints: [resolve(__dirname, "src/threading/process-worker-entry.ts")],
bundle: true,
format: "iife",
platform: "browser",
target: "esnext",
write: false,
minify: false,
sourcemap: false,
// Don't externalize anything — the worker must be fully self-contained
});
workerBundle = result.outputFiles[0].text;
},
resolveId(id) {
if (id === VIRTUAL_ID) return RESOLVED_ID;
},
load(id) {
if (id === RESOLVED_ID) {
return `export const PROCESS_WORKER_BUNDLE = ${JSON.stringify(workerBundle)};`;
}
},
};
}
export default defineConfig({
plugins: [wasm(), topLevelAwait(), inlineProcessWorkerPlugin()],
resolve: {
alias: {
// isomorphic-git does require('crypto') — resolve to our polyfill
crypto: resolve(__dirname, "src/polyfills/crypto.ts"),
},
},
worker: {
format: "es",
rollupOptions: {
external: allExternal,
},
},
build: {
lib: {
entry: {
index: resolve(__dirname, "src/index.ts"),
// Each framework integration is its own subpath export (see
// package.json `exports`). Slashes in the key push the output
// under dist/integrations/*.
"integrations/server": resolve(
__dirname,
"src/integrations/server.ts",
),
"integrations/vite": resolve(__dirname, "src/integrations/vite.ts"),
"integrations/next": resolve(__dirname, "src/integrations/next.ts"),
},
formats: ["es", "cjs"],
fileName: (format, entryName) => {
const ext = format === "es" ? "mjs" : "cjs";
return `${entryName}.${ext}`;
},
},
rollupOptions: {
external: allExternal,
},
sourcemap: true,
minify: false,
},
});