-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (48 loc) · 1.36 KB
/
index.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
"use strict"
const validateOptions = require("schema-utils");
class CustomModuleIdsPlugin {
constructor(options) {
this.options = options || {};
validateOptions({
type: 'object',
additionalProperties: false,
properties: {
idFunction: {
description: "Function called to generate ID of each module. Return value is used as module ID.",
instanceof: "Function",
},
callWhenMissingLibident: {
"type": "boolean",
},
},
required: ['idFunction'],
}, this.options, "Custom Module IDs Plugin");
}
_processModules(modules, context) {
for (const module of modules) {
if (module.id === null && (module.libIdent || this.options.callWhenMissingLibident)) {
module.id = this.options.idFunction(module.libIdent ? module.libIdent({
context,
}) : null, module);
}
}
}
apply(compiler) {
const context = this.options.context || compiler.options.context;
if (compiler.hooks) {
compiler.hooks.compilation.tap("CustomModuleIdsPlugin", compilation => {
compilation.hooks.beforeModuleIds.tap("CustomModuleIdsPlugin", modules => {
this._processModules(modules, context);
});
});
}
else {
compiler.plugin("compilation", compilation => {
compilation.plugin("before-module-ids", modules => {
this._processModules(modules, context);
});
});
}
}
}
module.exports = CustomModuleIdsPlugin;