* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 49 | * @returns {void} |
| 50 | */ |
| 51 | apply(compiler) { |
| 52 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
| 53 | const moduleGraph = compilation.moduleGraph; |
| 54 | compilation.hooks.optimizeDependencies.tap( |
| 55 | { name: PLUGIN_NAME, stage: STAGE_DEFAULT }, |
| 56 | (modules) => { |
| 57 | if (compilation.moduleMemCaches) { |
| 58 | throw new Error( |
| 59 | "optimization.usedExports can't be used with cacheUnaffected as export usage is a global effect" |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | const logger = compilation.getLogger(PLUGIN_LOGGER_NAME); |
| 64 | /** @type {Map<ExportsInfo, Module>} */ |
| 65 | const exportInfoToModuleMap = new Map(); |
| 66 | |
| 67 | /** @type {TupleQueue<Module, RuntimeSpec>} */ |
| 68 | const queue = new TupleQueue(); |
| 69 | |
| 70 | /** |
| 71 | * Process referenced module. |
| 72 | * @param {Module} module module to process |
| 73 | * @param {ReferencedExports} usedExports list of used exports |
| 74 | * @param {RuntimeSpec} runtime part of which runtime |
| 75 | * @param {boolean} forceSideEffects always apply side effects |
| 76 | * @returns {void} |
| 77 | */ |
| 78 | const processReferencedModule = ( |
| 79 | module, |
| 80 | usedExports, |
| 81 | runtime, |
| 82 | forceSideEffects |
| 83 | ) => { |
| 84 | const exportsInfo = moduleGraph.getExportsInfo(module); |
| 85 | if (usedExports === EXPORTS_OBJECT_REFERENCED_MANGLEABLE) { |
| 86 | // The whole namespace object escapes via a reference that codegen |
| 87 | // can materialize as a decoupled namespace object. When all exports |
| 88 | // are statically known we keep them mangleable instead of marking |
| 89 | // them used-in-unknown-way. |
| 90 | if ( |
| 91 | this.mangleEscapingNamespaces && |
| 92 | module.buildMeta && |
| 93 | module.buildMeta.exportsType === "namespace" && |
| 94 | exportsInfo.otherExportsInfo.provided === false |
| 95 | ) { |
| 96 | let changed = exportsInfo.setAllKnownExportsUsed(runtime); |
| 97 | // The whole namespace object is observed as a value, so the |
| 98 | // module must stay a real ES module namespace at runtime |
| 99 | // (keep __esModule / the namespace object, i.e. `r()`). |
| 100 | if ( |
| 101 | exportsInfo |
| 102 | .getExportInfo("__esModule") |
| 103 | .setUsed(UsageState.Used, runtime) |
| 104 | ) { |
| 105 | changed = true; |
| 106 | } |
| 107 | // Exports must keep a real binding (not be inlined) so member |
| 108 | // access on the namespace has ES namespace semantics, e.g. |
nothing calls this directly
no test coverage detected