aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/config/graphvizUMDVitePlugin.ts
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/frontend/config/graphvizUMDVitePlugin.ts')
-rw-r--r--subprojects/frontend/config/graphvizUMDVitePlugin.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/subprojects/frontend/config/graphvizUMDVitePlugin.ts b/subprojects/frontend/config/graphvizUMDVitePlugin.ts
new file mode 100644
index 00000000..7a42560b
--- /dev/null
+++ b/subprojects/frontend/config/graphvizUMDVitePlugin.ts
@@ -0,0 +1,63 @@
1import { readFile } from 'node:fs/promises';
2import path from 'node:path';
3
4import pnpapi from 'pnpapi';
5import type { PluginOption, ResolvedConfig } from 'vite';
6
7// Use a CJS file as the PnP resolution issuer to force resolution to a non-ESM export.
8const issuerFileName = 'worker.cjs';
9
10export default function graphvizUMDVitePlugin(): PluginOption {
11 let command: ResolvedConfig['command'] = 'build';
12 let root: string | undefined;
13 let url: string | undefined;
14
15 return {
16 name: 'graphviz-umd',
17 enforce: 'post',
18 configResolved(config) {
19 ({ command, root } = config);
20 },
21 async buildStart() {
22 const issuer =
23 root === undefined ? issuerFileName : path.join(issuerFileName);
24 const resolvedPath = pnpapi.resolveRequest(
25 '@hpcc-js/wasm/graphviz',
26 issuer,
27 );
28 if (resolvedPath === null) {
29 return;
30 }
31 if (command === 'serve') {
32 url = `/@fs/${resolvedPath}`;
33 } else {
34 const content = await readFile(resolvedPath, null);
35 url = this.emitFile({
36 name: path.basename(resolvedPath),
37 type: 'asset',
38 source: content,
39 });
40 }
41 },
42 renderStart() {
43 if (url !== undefined && command !== 'serve') {
44 url = this.getFileName(url);
45 }
46 },
47 transformIndexHtml() {
48 if (url === undefined) {
49 return undefined;
50 }
51 return [
52 {
53 tag: 'script',
54 attrs: {
55 src: url,
56 type: 'javascript/worker',
57 },
58 injectTo: 'head',
59 },
60 ];
61 },
62 };
63}