forked from unpkg/unpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentryManifest.js
95 lines (80 loc) · 2.38 KB
/
entryManifest.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const writeFile = require('fs').writeFileSync;
const tempy = require('tempy');
function updateWatchfile(watchfile) {
writeFile(watchfile, '' + Date.now());
}
function entryManifest() {
let manifests = [];
const watchfile = tempy.file();
updateWatchfile(watchfile);
return {
/**
* A Rollup plugin that populates the entry manifest. This should be
* used in the client bundle where the code-splitting is taking place.
*/
record(options = {}) {
const publicPath = (options.publicPath || '/').replace(/\/*$/, '/');
let manifest = Object.create(null);
manifests.push(manifest);
return {
name: 'entry-manifest-record',
buildStart() {
for (const key in manifest) {
delete manifest[key];
}
},
generateBundle(options, bundle) {
Object.keys(bundle).forEach(fileName => {
const info = bundle[fileName];
// We're interested only in entry points.
if (!info.isEntry) return;
const globalImports = info.imports.filter(
name => options.globals && options.globals[name]
);
let entryPoints = manifest[info.name];
if (!entryPoints) {
entryPoints = manifest[info.name] = [];
}
entryPoints.push({
format: options.format,
globalImports: globalImports,
url: publicPath + fileName,
code: info.code
});
});
updateWatchfile(watchfile);
}
};
},
/**
* A Rollup plugin that provides the current entry manifest via a
* virtual module id.
*/
inject(options = {}) {
const virtualId = options.virtualId || 'entry-manifest';
return {
name: 'entry-manifest-inject',
resolveId(id) {
if (id === virtualId) {
return id;
}
return null;
},
load(id) {
if (id === virtualId) {
const value = manifests.length === 1 ? manifests[0] : manifests;
return 'export default ' + JSON.stringify(value);
}
return null;
},
transform(code, id) {
if (id === virtualId) {
this.addWatchFile(watchfile);
}
return null;
}
};
}
};
}
module.exports = entryManifest;