forked from urlaubsverwaltung/urlaubsverwaltung
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup-plugin-assets-manifest.mjs
68 lines (60 loc) · 2.06 KB
/
rollup-plugin-assets-manifest.mjs
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
import fs from "node:fs";
/**
*
* @param output {string} filename of the generated assets manifest
* @param publicPath {string} public path prefix for assets
* @returns {{generateBundle(*, *): void, name: string}}
*/
export function assetsManifest({ output, publicPath}) {
function url(dep) {
return `${publicPath}/${dep}`;
}
return {
name: "generate-manifest",
generateBundle(options, bundle) {
const bundleEntries = Object.entries(bundle);
function indexOfKey(key) {
return bundleEntries.findIndex(([k]) => k === key);
}
function getImportsOf(dep) {
const [, entry] = bundleEntries[indexOfKey(dep)];
return entry ? [...entry.imports.map((d) => url(d)), ...entry.imports.flatMap((d) => getImportsOf(d))] : [];
}
function transitiveDependencies(id) {
return [...new Set(getImportsOf(id))];
}
const bundledEntryPoints = Object.entries(bundle)
.map(([id, entry]) => {
if (entry.isEntry) {
const keyName = `${entry.name}.js`;
const dependencies = transitiveDependencies(id);
return [
keyName,
{
url: `${publicPath}/${entry.fileName}`,
dependencies,
},
];
}
if (entry.type === "asset") {
// since rollup v3 sourcemaps are exposed as "asset". sourcemaps do not have a name, however.
// and I don't think sourcemaps have to be added to the manifest file.
const keyName = entry.name;
if (keyName) {
return [
keyName,
{
url: `${publicPath}/${entry.fileName}`,
dependencies: [],
},
];
}
}
})
.filter(Boolean);
// sort keys alphabetically
bundledEntryPoints.sort(([nameA], [nameB]) => nameA.localeCompare(nameB));
fs.writeFileSync(output, JSON.stringify(Object.fromEntries(bundledEntryPoints), undefined, 2), "utf8");
},
};
}