1
|
/*
|
2
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
Author Tobias Koppers @sokra
|
4
|
*/
|
5
|
"use strict";
|
6
|
|
7
|
const path = require("path");
|
8
|
const asyncLib = require("neo-async");
|
9
|
const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
|
10
|
|
11
|
class LibManifestPlugin {
|
12
|
constructor(options) {
|
13
|
this.options = options;
|
14
|
}
|
15
|
|
16
|
apply(compiler) {
|
17
|
compiler.hooks.emit.tapAsync(
|
18
|
"LibManifestPlugin",
|
19
|
(compilation, callback) => {
|
20
|
asyncLib.forEach(
|
21
|
compilation.chunks,
|
22
|
(chunk, callback) => {
|
23
|
if (!chunk.isOnlyInitial()) {
|
24
|
callback();
|
25
|
return;
|
26
|
}
|
27
|
const targetPath = compilation.getPath(this.options.path, {
|
28
|
hash: compilation.hash,
|
29
|
chunk
|
30
|
});
|
31
|
const name =
|
32
|
this.options.name &&
|
33
|
compilation.getPath(this.options.name, {
|
34
|
hash: compilation.hash,
|
35
|
chunk
|
36
|
});
|
37
|
const manifest = {
|
38
|
name,
|
39
|
type: this.options.type,
|
40
|
content: Array.from(chunk.modulesIterable, module => {
|
41
|
if (
|
42
|
this.options.entryOnly &&
|
43
|
!module.reasons.some(
|
44
|
r => r.dependency instanceof SingleEntryDependency
|
45
|
)
|
46
|
) {
|
47
|
return;
|
48
|
}
|
49
|
if (module.libIdent) {
|
50
|
const ident = module.libIdent({
|
51
|
context: this.options.context || compiler.options.context
|
52
|
});
|
53
|
if (ident) {
|
54
|
return {
|
55
|
ident,
|
56
|
data: {
|
57
|
id: module.id,
|
58
|
buildMeta: module.buildMeta
|
59
|
}
|
60
|
};
|
61
|
}
|
62
|
}
|
63
|
})
|
64
|
.filter(Boolean)
|
65
|
.reduce((obj, item) => {
|
66
|
obj[item.ident] = item.data;
|
67
|
return obj;
|
68
|
}, Object.create(null))
|
69
|
};
|
70
|
// Apply formatting to content if format flag is true;
|
71
|
const manifestContent = this.options.format
|
72
|
? JSON.stringify(manifest, null, 2)
|
73
|
: JSON.stringify(manifest);
|
74
|
const content = Buffer.from(manifestContent, "utf8");
|
75
|
compiler.outputFileSystem.mkdirp(path.dirname(targetPath), err => {
|
76
|
if (err) return callback(err);
|
77
|
compiler.outputFileSystem.writeFile(
|
78
|
targetPath,
|
79
|
content,
|
80
|
callback
|
81
|
);
|
82
|
});
|
83
|
},
|
84
|
callback
|
85
|
);
|
86
|
}
|
87
|
);
|
88
|
}
|
89
|
}
|
90
|
module.exports = LibManifestPlugin;
|