|
1 | | -import { relative, sep } from "node:path"; |
2 | | -import { fileURLToPath } from "node:url"; |
3 | | -import { ManifestChunk, normalizePath, Plugin } from "vite"; |
4 | | -import viteManifest from "./manifest"; |
| 1 | +import { Manifest, Plugin } from "vite"; |
5 | 2 |
|
6 | | -const manifestJsonPath = "manifest.json"; |
7 | | -const manifestModulePath = fileURLToPath(new URL("manifest.js", import.meta.url)); |
8 | | -const manifestModulePaths = [".js", ".cjs"].map((extname) => normalizePath(manifestModulePath + extname)); |
| 3 | +const manifestPath = "manifest.json"; |
| 4 | +const _globalThis: { manifest?: Manifest } = globalThis as any; |
9 | 5 |
|
10 | | -export default (): Plugin => { |
11 | | - let isSsrBuild: boolean = false; |
| 6 | +export default ({ manifest }: { manifest: Manifest }): Plugin => { |
12 | 7 | return { |
13 | | - name: "vite-plugin-manifest", |
14 | | - apply: (_config, { isSsrBuild }) => !isSsrBuild, |
15 | | - config(_config, env) { |
16 | | - isSsrBuild = !!env.isSsrBuild; |
17 | | - |
18 | | - if (isSsrBuild) { |
19 | | - return; |
20 | | - } |
21 | | - |
22 | | - return { build: { manifest: manifestJsonPath } }; |
23 | | - }, |
24 | | - configResolved(config) { |
25 | | - if (isSsrBuild) { |
26 | | - return; |
27 | | - } |
28 | | - |
29 | | - const input = config.build.rollupOptions.input ?? []; |
30 | | - const inputs = typeof input === "string" ? [input] : Object.values(input); |
31 | | - const paths = inputs.map((input) => relative(".", input).split(sep).join("/")); |
32 | | - |
33 | | - Object.assign( |
34 | | - viteManifest, |
35 | | - Object.fromEntries(paths.map((path) => [path, { file: path, isEntry: true } satisfies ManifestChunk])), |
36 | | - ); |
37 | | - }, |
38 | | - load(id) { |
39 | | - if (!manifestModulePaths.includes(id)) { |
40 | | - return; |
| 8 | + name: "manifest", |
| 9 | + config: () => ({ build: { manifest: manifestPath } }), |
| 10 | + options() { |
| 11 | + if (_globalThis.manifest) { |
| 12 | + Object.keys(manifest).forEach((key) => delete manifest[key]); |
| 13 | + Object.assign(manifest, _globalThis.manifest); |
41 | 14 | } |
42 | | - |
43 | | - return `export default Object.assign(Object.create(null), JSON.parse(${JSON.stringify(JSON.stringify(viteManifest))}))`; |
44 | 15 | }, |
45 | 16 | generateBundle: { |
46 | 17 | order: "post", |
47 | | - handler(_, bundle) { |
48 | | - if (isSsrBuild) { |
49 | | - return; |
50 | | - } |
51 | | - |
52 | | - if (bundle[manifestJsonPath]?.type !== "asset") { |
53 | | - return; |
| 18 | + handler(_options, bundle) { |
| 19 | + if (bundle[manifestPath]?.type === "asset") { |
| 20 | + _globalThis.manifest = JSON.parse(`${bundle[manifestPath].source}`); |
| 21 | + delete bundle[manifestPath]; |
54 | 22 | } |
55 | | - |
56 | | - for (const key of Object.keys(viteManifest)) { |
57 | | - delete bundle[key]; |
58 | | - } |
59 | | - |
60 | | - Object.assign(viteManifest, JSON.parse(`${bundle[manifestJsonPath].source}`)); |
61 | | - delete bundle[manifestJsonPath]; |
62 | 23 | }, |
63 | 24 | }, |
64 | 25 | }; |
|
0 commit comments