Skip to content

Commit ecdeb0e

Browse files
committed
feat: refactor vite-plugin-manifest to streamline manifest handling and remove unused manifest module
1 parent ad756fc commit ecdeb0e

File tree

3 files changed

+14
-64
lines changed

3 files changed

+14
-64
lines changed

packages/vite-plugin-manifest/package.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121
"import": "./dist/index.js",
2222
"require": "./dist/index.cjs",
2323
"default": "./dist/index.js"
24-
},
25-
"./manifest": {
26-
"types": "./dist/manifest.d.ts",
27-
"import": "./dist/manifest.js",
28-
"require": "./dist/manifest.cjs",
29-
"default": "./dist/manifest.js"
3024
}
3125
},
3226
"main": "./dist/index.js",

packages/vite-plugin-manifest/src/index.ts

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,25 @@
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";
52

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;
95

10-
export default (): Plugin => {
11-
let isSsrBuild: boolean = false;
6+
export default ({ manifest }: { manifest: Manifest }): Plugin => {
127
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);
4114
}
42-
43-
return `export default Object.assign(Object.create(null), JSON.parse(${JSON.stringify(JSON.stringify(viteManifest))}))`;
4415
},
4516
generateBundle: {
4617
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];
5422
}
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];
6223
},
6324
},
6425
};

packages/vite-plugin-manifest/src/manifest.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)