| 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 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 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 | * Handles the hook callback for this code path.
|
|---|
| 39 | * @param {JavascriptParser} parser the parser
|
|---|
| 40 | * @returns {void}
|
|---|
| 41 | */
|
|---|
| 42 | const handler = (parser) => {
|
|---|
| 43 | parser.hooks.expressionMemberChain
|
|---|
| 44 | .for("__webpack_exports_info__")
|
|---|
| 45 | .tap(PLUGIN_NAME, (expr, members) => {
|
|---|
| 46 | const dep =
|
|---|
| 47 | members.length >= 2
|
|---|
| 48 | ? new ExportsInfoDependency(
|
|---|
| 49 | /** @type {Range} */ (expr.range),
|
|---|
| 50 | members.slice(0, -1),
|
|---|
| 51 | members[members.length - 1]
|
|---|
| 52 | )
|
|---|
| 53 | : new ExportsInfoDependency(
|
|---|
| 54 | /** @type {Range} */ (expr.range),
|
|---|
| 55 | null,
|
|---|
| 56 | members[0]
|
|---|
| 57 | );
|
|---|
| 58 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|---|
| 59 | parser.state.module.addDependency(dep);
|
|---|
| 60 | return true;
|
|---|
| 61 | });
|
|---|
| 62 | parser.hooks.expression
|
|---|
| 63 | .for("__webpack_exports_info__")
|
|---|
| 64 | .tap(PLUGIN_NAME, (expr) => {
|
|---|
| 65 | const dep = new ConstDependency(
|
|---|
| 66 | "true",
|
|---|
| 67 | /** @type {Range} */ (expr.range)
|
|---|
| 68 | );
|
|---|
| 69 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|---|
| 70 | parser.state.module.addPresentationalDependency(dep);
|
|---|
| 71 | return true;
|
|---|
| 72 | });
|
|---|
| 73 | };
|
|---|
| 74 | normalModuleFactory.hooks.parser
|
|---|
| 75 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
|---|
| 76 | .tap(PLUGIN_NAME, handler);
|
|---|
| 77 | normalModuleFactory.hooks.parser
|
|---|
| 78 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
|---|
| 79 | .tap(PLUGIN_NAME, handler);
|
|---|
| 80 | normalModuleFactory.hooks.parser
|
|---|
| 81 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
|---|
| 82 | .tap(PLUGIN_NAME, handler);
|
|---|
| 83 | }
|
|---|
| 84 | );
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | module.exports = ExportsInfoApiPlugin;
|
|---|