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