[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const {
|
---|
| 9 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
| 10 | JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
---|
| 11 | JAVASCRIPT_MODULE_TYPE_ESM
|
---|
| 12 | } = require("./ModuleTypeConstants");
|
---|
| 13 | const ConstDependency = require("./dependencies/ConstDependency");
|
---|
| 14 | const ExportsInfoDependency = require("./dependencies/ExportsInfoDependency");
|
---|
| 15 |
|
---|
| 16 | /** @typedef {import("./Compiler")} Compiler */
|
---|
| 17 | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
|
---|
| 18 | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
---|
| 19 | /** @typedef {import("./javascript/JavascriptParser").Range} Range */
|
---|
| 20 |
|
---|
| 21 | const PLUGIN_NAME = "ExportsInfoApiPlugin";
|
---|
| 22 |
|
---|
| 23 | class ExportsInfoApiPlugin {
|
---|
| 24 | /**
|
---|
| 25 | * Apply the plugin
|
---|
| 26 | * @param {Compiler} compiler the compiler instance
|
---|
| 27 | * @returns {void}
|
---|
| 28 | */
|
---|
| 29 | apply(compiler) {
|
---|
| 30 | compiler.hooks.compilation.tap(
|
---|
| 31 | PLUGIN_NAME,
|
---|
| 32 | (compilation, { normalModuleFactory }) => {
|
---|
| 33 | compilation.dependencyTemplates.set(
|
---|
| 34 | ExportsInfoDependency,
|
---|
| 35 | new ExportsInfoDependency.Template()
|
---|
| 36 | );
|
---|
| 37 | /**
|
---|
| 38 | * @param {JavascriptParser} parser the parser
|
---|
| 39 | * @returns {void}
|
---|
| 40 | */
|
---|
| 41 | const handler = parser => {
|
---|
| 42 | parser.hooks.expressionMemberChain
|
---|
| 43 | .for("__webpack_exports_info__")
|
---|
| 44 | .tap(PLUGIN_NAME, (expr, members) => {
|
---|
| 45 | const dep =
|
---|
| 46 | members.length >= 2
|
---|
| 47 | ? new ExportsInfoDependency(
|
---|
| 48 | /** @type {Range} */ (expr.range),
|
---|
| 49 | members.slice(0, -1),
|
---|
| 50 | members[members.length - 1]
|
---|
| 51 | )
|
---|
| 52 | : new ExportsInfoDependency(
|
---|
| 53 | /** @type {Range} */ (expr.range),
|
---|
| 54 | null,
|
---|
| 55 | members[0]
|
---|
| 56 | );
|
---|
| 57 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
| 58 | parser.state.module.addDependency(dep);
|
---|
| 59 | return true;
|
---|
| 60 | });
|
---|
| 61 | parser.hooks.expression
|
---|
| 62 | .for("__webpack_exports_info__")
|
---|
| 63 | .tap(PLUGIN_NAME, expr => {
|
---|
| 64 | const dep = new ConstDependency(
|
---|
| 65 | "true",
|
---|
| 66 | /** @type {Range} */ (expr.range)
|
---|
| 67 | );
|
---|
| 68 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
| 69 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 70 | return true;
|
---|
| 71 | });
|
---|
| 72 | };
|
---|
| 73 | normalModuleFactory.hooks.parser
|
---|
| 74 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
| 75 | .tap(PLUGIN_NAME, handler);
|
---|
| 76 | normalModuleFactory.hooks.parser
|
---|
| 77 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
| 78 | .tap(PLUGIN_NAME, handler);
|
---|
| 79 | normalModuleFactory.hooks.parser
|
---|
| 80 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
---|
| 81 | .tap(PLUGIN_NAME, handler);
|
---|
| 82 | }
|
---|
| 83 | );
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | module.exports = ExportsInfoApiPlugin;
|
---|