| 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 { ModuleExternalInitFragment } = require("./ExternalModule");
|
|---|
| 9 | const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
|
|---|
| 10 | const ConcatenatedModule = require("./optimize/ConcatenatedModule");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("../declarations/WebpackOptions").ExternalsType} ExternalsType */
|
|---|
| 13 | /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
|
|---|
| 14 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 15 | /** @typedef {import("./ExternalModule").Imported} Imported */
|
|---|
| 16 | /** @typedef {import("./Dependency")} Dependency */
|
|---|
| 17 |
|
|---|
| 18 | const PLUGIN_NAME = "ExternalsPlugin";
|
|---|
| 19 |
|
|---|
| 20 | class ExternalsPlugin {
|
|---|
| 21 | /**
|
|---|
| 22 | * Creates an instance of ExternalsPlugin.
|
|---|
| 23 | * @param {ExternalsType | ((dependency: Dependency) => ExternalsType)} type default external type
|
|---|
| 24 | * @param {Externals} externals externals config
|
|---|
| 25 | */
|
|---|
| 26 | constructor(type, externals) {
|
|---|
| 27 | this.type = type;
|
|---|
| 28 | this.externals = externals;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 33 | * @param {Compiler} compiler the compiler instance
|
|---|
| 34 | * @returns {void}
|
|---|
| 35 | */
|
|---|
| 36 | apply(compiler) {
|
|---|
| 37 | compiler.hooks.compile.tap(PLUGIN_NAME, ({ normalModuleFactory }) => {
|
|---|
| 38 | new ExternalModuleFactoryPlugin(this.type, this.externals).apply(
|
|---|
| 39 | normalModuleFactory
|
|---|
| 40 | );
|
|---|
| 41 | });
|
|---|
| 42 |
|
|---|
| 43 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 44 | const { concatenatedModuleInfo } =
|
|---|
| 45 | ConcatenatedModule.getCompilationHooks(compilation);
|
|---|
| 46 | concatenatedModuleInfo.tap(PLUGIN_NAME, (updatedInfo, moduleInfo) => {
|
|---|
| 47 | const rawExportMap = updatedInfo.rawExportMap;
|
|---|
| 48 |
|
|---|
| 49 | if (!rawExportMap) {
|
|---|
| 50 | return;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | const chunkInitFragments = moduleInfo.chunkInitFragments;
|
|---|
| 54 | const moduleExternalInitFragments =
|
|---|
| 55 | /** @type {ModuleExternalInitFragment[]} */
|
|---|
| 56 | (
|
|---|
| 57 | chunkInitFragments
|
|---|
| 58 | ? /** @type {unknown[]} */
|
|---|
| 59 | (chunkInitFragments).filter(
|
|---|
| 60 | (fragment) => fragment instanceof ModuleExternalInitFragment
|
|---|
| 61 | )
|
|---|
| 62 | : []
|
|---|
| 63 | );
|
|---|
| 64 |
|
|---|
| 65 | let initFragmentChanged = false;
|
|---|
| 66 |
|
|---|
| 67 | for (const fragment of moduleExternalInitFragments) {
|
|---|
| 68 | const imported = fragment.getImported();
|
|---|
| 69 |
|
|---|
| 70 | if (Array.isArray(imported)) {
|
|---|
| 71 | const newImported =
|
|---|
| 72 | /** @type {Imported} */
|
|---|
| 73 | (
|
|---|
| 74 | imported.map(([specifier, finalName]) => [
|
|---|
| 75 | specifier,
|
|---|
| 76 | rawExportMap.has(specifier)
|
|---|
| 77 | ? rawExportMap.get(specifier)
|
|---|
| 78 | : finalName
|
|---|
| 79 | ])
|
|---|
| 80 | );
|
|---|
| 81 | fragment.setImported(newImported);
|
|---|
| 82 | initFragmentChanged = true;
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | if (initFragmentChanged) {
|
|---|
| 87 | return true;
|
|---|
| 88 | }
|
|---|
| 89 | });
|
|---|
| 90 | });
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | module.exports = ExternalsPlugin;
|
|---|