-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (56 loc) · 2.77 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
56
57
58
59
60
61
/**
* @author Erik Desjardins
* See LICENSE file in root directory for full license.
*/
'use strict';
const RawSource = require('webpack-sources').RawSource;
function InertEntryPlugin() {}
InertEntryPlugin.prototype.apply = function(compiler) {
compiler.hooks.thisCompilation.tap(InertEntryPlugin.name, function(compilation, { normalModuleFactory }) {
// parser and generator originally assigned here:
// https://github.com/webpack/webpack/blob/f916fc0bb70585cf04a92cd99e004e4879f1d337/lib/NormalModuleFactory.js#L317-L319
// but it doesn't appear that we can interfere before this point,
// so mutate the resolved object after resolution:
// https://github.com/webpack/webpack/blob/f916fc0bb70585cf04a92cd99e004e4879f1d337/lib/NormalModuleFactory.js#L126
normalModuleFactory.hooks.afterResolve.tap(InertEntryPlugin.name, data => {
data.type = 'inert-entry-plugin';
// https://github.com/webpack/webpack/blob/f916fc0bb70585cf04a92cd99e004e4879f1d337/lib/JsonModulesPlugin.js
data.parser = {
// https://github.com/webpack/webpack/blob/f916fc0bb70585cf04a92cd99e004e4879f1d337/lib/JsonParser.js
parse(source, state) {
state.module.buildInfo.source = new RawSource(source);
return state;
}
};
data.generator = {
// https://github.com/webpack/webpack/blob/f916fc0bb70585cf04a92cd99e004e4879f1d337/lib/JsonGenerator.js
generate(module) {
// may be undefined, if, e.g. the module has errors
// (but seemingly only in very specific situations, so this is untested)
return module.buildInfo.source;
}
};
});
// prevent the bootstrap code from being emitted
// (can't override render directly because JavascriptModulesPlugin::renderJavascript forcibly appends a semicolon)
// https://github.com/webpack/webpack/blob/f916fc0bb70585cf04a92cd99e004e4879f1d337/lib/MainTemplate.js#L118
// https://github.com/webpack/webpack/blob/f916fc0bb70585cf04a92cd99e004e4879f1d337/lib/JavascriptModulesPlugin.js#L109
// https://github.com/webpack-contrib/mini-css-extract-plugin/blob/31742323d4a6004ee4a2d2be92f642de01f66cbc/src/index.js#L107
compilation.mainTemplate.hooks.renderManifest.tap(InertEntryPlugin.name, (result, { chunk, dependencyTemplates, outputOptions }) => {
if (chunk.getNumberOfModules() !== 1) {
throw new Error('Assertion failed: inert entry point must have exactly 1 module');
}
result.push({
render: () => chunk.entryModule.source(dependencyTemplates, 'inert-entry-webpack-plugin_unused_runtimeTemplate'),
filenameTemplate: chunk.filenameTemplate || outputOptions.filename,
pathOptions: {
chunk,
},
identifier: `inert-entry-plugin.${chunk.id}`
});
// kill any following plugins by returning a dummy array that they'll push to
return [];
});
});
};
module.exports = InertEntryPlugin;