| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Alexander Akait @alexander-akait
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const DependencyTemplate = require("../DependencyTemplate");
|
|---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 10 | const ExternalModuleInitFragment = require("./ExternalModuleInitFragment");
|
|---|
| 11 | const NullDependency = require("./NullDependency");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 14 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 15 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 16 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 17 | /** @typedef {import("../dependencies/ExternalModuleInitFragment").ArrayImportSpecifiers} ArrayImportSpecifiers */
|
|---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 20 |
|
|---|
| 21 | class ExternalModuleInitFragmentDependency extends NullDependency {
|
|---|
| 22 | /**
|
|---|
| 23 | * Creates an instance of ExternalModuleInitFragmentDependency.
|
|---|
| 24 | * @param {string} module module
|
|---|
| 25 | * @param {ArrayImportSpecifiers} importSpecifiers import specifiers
|
|---|
| 26 | * @param {string | undefined} defaultImport default import
|
|---|
| 27 | */
|
|---|
| 28 | constructor(module, importSpecifiers, defaultImport) {
|
|---|
| 29 | super();
|
|---|
| 30 | this.importedModule = module;
|
|---|
| 31 | this.specifiers = importSpecifiers;
|
|---|
| 32 | this.default = defaultImport;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Serializes this instance into the provided serializer context.
|
|---|
| 37 | * @param {ObjectSerializerContext} context context
|
|---|
| 38 | */
|
|---|
| 39 | serialize(context) {
|
|---|
| 40 | const { write } = context;
|
|---|
| 41 | write(this.importedModule);
|
|---|
| 42 | write(this.specifiers);
|
|---|
| 43 | write(this.default);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Restores this instance from the provided deserializer context.
|
|---|
| 48 | * @param {ObjectDeserializerContext} context context
|
|---|
| 49 | */
|
|---|
| 50 | deserialize(context) {
|
|---|
| 51 | const { read } = context;
|
|---|
| 52 | this.importedModule = read();
|
|---|
| 53 | this.specifiers = read();
|
|---|
| 54 | this.default = read();
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | makeSerializable(
|
|---|
| 59 | ExternalModuleInitFragmentDependency,
|
|---|
| 60 | "webpack/lib/dependencies/ExternalModuleConstDependency"
|
|---|
| 61 | );
|
|---|
| 62 |
|
|---|
| 63 | ExternalModuleInitFragmentDependency.Template = class ExternalModuleConstDependencyTemplate extends (
|
|---|
| 64 | DependencyTemplate
|
|---|
| 65 | ) {
|
|---|
| 66 | /**
|
|---|
| 67 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 68 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 69 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 70 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 71 | * @returns {void}
|
|---|
| 72 | */
|
|---|
| 73 | apply(dependency, source, templateContext) {
|
|---|
| 74 | const dep =
|
|---|
| 75 | /** @type {ExternalModuleInitFragmentDependency} */
|
|---|
| 76 | (dependency);
|
|---|
| 77 | const { chunkInitFragments, runtimeTemplate } = templateContext;
|
|---|
| 78 |
|
|---|
| 79 | chunkInitFragments.push(
|
|---|
| 80 | new ExternalModuleInitFragment(
|
|---|
| 81 | `${runtimeTemplate.supportNodePrefixForCoreModules() ? "node:" : ""}${
|
|---|
| 82 | dep.importedModule
|
|---|
| 83 | }`,
|
|---|
| 84 | dep.specifiers,
|
|---|
| 85 | dep.default
|
|---|
| 86 | )
|
|---|
| 87 | );
|
|---|
| 88 | }
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 | module.exports = ExternalModuleInitFragmentDependency;
|
|---|