| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Ivan Kopeykin @vankop
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 9 | const CachedConstDependency = require("./CachedConstDependency");
|
|---|
| 10 | const ExternalModuleInitFragment = require("./ExternalModuleInitFragment");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 13 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 14 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 15 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 16 | /** @typedef {import("../dependencies/ExternalModuleInitFragment").ArrayImportSpecifiers} ArrayImportSpecifiers */
|
|---|
| 17 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 19 |
|
|---|
| 20 | class ExternalModuleDependency extends CachedConstDependency {
|
|---|
| 21 | /**
|
|---|
| 22 | * Creates an instance of ExternalModuleDependency.
|
|---|
| 23 | * @param {string} module module
|
|---|
| 24 | * @param {ArrayImportSpecifiers} importSpecifiers import specifiers
|
|---|
| 25 | * @param {string | undefined} defaultImport default import
|
|---|
| 26 | * @param {string} expression expression
|
|---|
| 27 | * @param {Range | null} range range
|
|---|
| 28 | * @param {string} identifier identifier
|
|---|
| 29 | * @param {number=} place place where we inject the expression
|
|---|
| 30 | */
|
|---|
| 31 | constructor(
|
|---|
| 32 | module,
|
|---|
| 33 | importSpecifiers,
|
|---|
| 34 | defaultImport,
|
|---|
| 35 | expression,
|
|---|
| 36 | range,
|
|---|
| 37 | identifier,
|
|---|
| 38 | place = CachedConstDependency.PLACE_MODULE
|
|---|
| 39 | ) {
|
|---|
| 40 | super(expression, range, identifier, place);
|
|---|
| 41 |
|
|---|
| 42 | this.importedModule = module;
|
|---|
| 43 | this.specifiers = importSpecifiers;
|
|---|
| 44 | this.default = defaultImport;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Create hash update.
|
|---|
| 49 | * @returns {string} hash update
|
|---|
| 50 | */
|
|---|
| 51 | _createHashUpdate() {
|
|---|
| 52 | return `${this.importedModule}${JSON.stringify(this.specifiers)}${
|
|---|
| 53 | this.default || "null"
|
|---|
| 54 | }${super._createHashUpdate()}`;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Serializes this instance into the provided serializer context.
|
|---|
| 59 | * @param {ObjectSerializerContext} context context
|
|---|
| 60 | */
|
|---|
| 61 | serialize(context) {
|
|---|
| 62 | super.serialize(context);
|
|---|
| 63 | const { write } = context;
|
|---|
| 64 | write(this.importedModule);
|
|---|
| 65 | write(this.specifiers);
|
|---|
| 66 | write(this.default);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Restores this instance from the provided deserializer context.
|
|---|
| 71 | * @param {ObjectDeserializerContext} context context
|
|---|
| 72 | */
|
|---|
| 73 | deserialize(context) {
|
|---|
| 74 | super.deserialize(context);
|
|---|
| 75 | const { read } = context;
|
|---|
| 76 | this.importedModule = read();
|
|---|
| 77 | this.specifiers = read();
|
|---|
| 78 | this.default = read();
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | makeSerializable(
|
|---|
| 83 | ExternalModuleDependency,
|
|---|
| 84 | "webpack/lib/dependencies/ExternalModuleDependency"
|
|---|
| 85 | );
|
|---|
| 86 |
|
|---|
| 87 | ExternalModuleDependency.Template = class ExternalModuleDependencyTemplate extends (
|
|---|
| 88 | CachedConstDependency.Template
|
|---|
| 89 | ) {
|
|---|
| 90 | /**
|
|---|
| 91 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 92 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 93 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 94 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 95 | * @returns {void}
|
|---|
| 96 | */
|
|---|
| 97 | apply(dependency, source, templateContext) {
|
|---|
| 98 | super.apply(dependency, source, templateContext);
|
|---|
| 99 | const dep = /** @type {ExternalModuleDependency} */ (dependency);
|
|---|
| 100 | const { chunkInitFragments, runtimeTemplate } = templateContext;
|
|---|
| 101 |
|
|---|
| 102 | chunkInitFragments.push(
|
|---|
| 103 | new ExternalModuleInitFragment(
|
|---|
| 104 | `${runtimeTemplate.supportNodePrefixForCoreModules() ? "node:" : ""}${
|
|---|
| 105 | dep.importedModule
|
|---|
| 106 | }`,
|
|---|
| 107 | dep.specifiers,
|
|---|
| 108 | dep.default
|
|---|
| 109 | )
|
|---|
| 110 | );
|
|---|
| 111 | }
|
|---|
| 112 | };
|
|---|
| 113 |
|
|---|
| 114 | module.exports = ExternalModuleDependency;
|
|---|