[79a0317] | 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("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 15 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 16 | /** @typedef {import("../javascript/JavascriptModulesPlugin").ChunkRenderContext} ChunkRenderContext */
|
---|
| 17 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 20 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 21 |
|
---|
| 22 | class ExternalModuleDependency extends CachedConstDependency {
|
---|
| 23 | /**
|
---|
| 24 | * @param {string} module module
|
---|
| 25 | * @param {{ name: string, value: string }[]} importSpecifiers import specifiers
|
---|
| 26 | * @param {string | undefined} defaultImport default import
|
---|
| 27 | * @param {string} expression expression
|
---|
| 28 | * @param {Range} range range
|
---|
| 29 | * @param {string} identifier identifier
|
---|
| 30 | */
|
---|
| 31 | constructor(
|
---|
| 32 | module,
|
---|
| 33 | importSpecifiers,
|
---|
| 34 | defaultImport,
|
---|
| 35 | expression,
|
---|
| 36 | range,
|
---|
| 37 | identifier
|
---|
| 38 | ) {
|
---|
| 39 | super(expression, range, identifier);
|
---|
| 40 |
|
---|
| 41 | this.importedModule = module;
|
---|
| 42 | this.specifiers = importSpecifiers;
|
---|
| 43 | this.default = defaultImport;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * @returns {string} hash update
|
---|
| 48 | */
|
---|
| 49 | _createHashUpdate() {
|
---|
| 50 | return `${this.importedModule}${JSON.stringify(this.specifiers)}${
|
---|
| 51 | this.default || "null"
|
---|
| 52 | }${super._createHashUpdate()}`;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * @param {ObjectSerializerContext} context context
|
---|
| 57 | */
|
---|
| 58 | serialize(context) {
|
---|
| 59 | super.serialize(context);
|
---|
| 60 | const { write } = context;
|
---|
| 61 | write(this.importedModule);
|
---|
| 62 | write(this.specifiers);
|
---|
| 63 | write(this.default);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * @param {ObjectDeserializerContext} context context
|
---|
| 68 | */
|
---|
| 69 | deserialize(context) {
|
---|
| 70 | super.deserialize(context);
|
---|
| 71 | const { read } = context;
|
---|
| 72 | this.importedModule = read();
|
---|
| 73 | this.specifiers = read();
|
---|
| 74 | this.default = read();
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | makeSerializable(
|
---|
| 79 | ExternalModuleDependency,
|
---|
| 80 | "webpack/lib/dependencies/ExternalModuleDependency"
|
---|
| 81 | );
|
---|
| 82 |
|
---|
| 83 | ExternalModuleDependency.Template = class ExternalModuleDependencyTemplate extends (
|
---|
| 84 | CachedConstDependency.Template
|
---|
| 85 | ) {
|
---|
| 86 | /**
|
---|
| 87 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 88 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 89 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 90 | * @returns {void}
|
---|
| 91 | */
|
---|
| 92 | apply(dependency, source, templateContext) {
|
---|
| 93 | super.apply(dependency, source, templateContext);
|
---|
| 94 | const dep = /** @type {ExternalModuleDependency} */ (dependency);
|
---|
| 95 | const { chunkInitFragments, runtimeTemplate } = templateContext;
|
---|
| 96 |
|
---|
| 97 | chunkInitFragments.push(
|
---|
| 98 | new ExternalModuleInitFragment(
|
---|
| 99 | `${runtimeTemplate.supportNodePrefixForCoreModules() ? "node:" : ""}${
|
---|
| 100 | dep.importedModule
|
---|
| 101 | }`,
|
---|
| 102 | dep.specifiers,
|
---|
| 103 | dep.default
|
---|
| 104 | )
|
---|
| 105 | );
|
---|
| 106 | }
|
---|
| 107 | };
|
---|
| 108 |
|
---|
| 109 | module.exports = ExternalModuleDependency;
|
---|