[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 InitFragment = require("../InitFragment");
|
---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 10 |
|
---|
| 11 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 12 | /** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
---|
| 13 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 14 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 15 | /** @typedef {Map<string, Set<string>>} ImportSpecifiers */
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * @extends {InitFragment<GenerateContext>}
|
---|
| 19 | */
|
---|
| 20 | class ExternalModuleInitFragment extends InitFragment {
|
---|
| 21 | /**
|
---|
| 22 | * @param {string} importedModule imported module
|
---|
| 23 | * @param {Array<{ name: string, value?: string }> | ImportSpecifiers} specifiers import specifiers
|
---|
| 24 | * @param {string=} defaultImport default import
|
---|
| 25 | */
|
---|
| 26 | constructor(importedModule, specifiers, defaultImport) {
|
---|
| 27 | super(
|
---|
| 28 | undefined,
|
---|
| 29 | InitFragment.STAGE_CONSTANTS,
|
---|
| 30 | 0,
|
---|
| 31 | `external module imports|${importedModule}|${defaultImport || "null"}`
|
---|
| 32 | );
|
---|
| 33 | this.importedModule = importedModule;
|
---|
| 34 | if (Array.isArray(specifiers)) {
|
---|
| 35 | /** @type {ImportSpecifiers} */
|
---|
| 36 | this.specifiers = new Map();
|
---|
| 37 | for (const { name, value } of specifiers) {
|
---|
| 38 | let specifiers = this.specifiers.get(name);
|
---|
| 39 | if (!specifiers) {
|
---|
| 40 | specifiers = new Set();
|
---|
| 41 | this.specifiers.set(name, specifiers);
|
---|
| 42 | }
|
---|
| 43 | specifiers.add(value || name);
|
---|
| 44 | }
|
---|
| 45 | } else {
|
---|
| 46 | this.specifiers = specifiers;
|
---|
| 47 | }
|
---|
| 48 | this.defaultImport = defaultImport;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * @param {ExternalModuleInitFragment} other other
|
---|
| 53 | * @returns {ExternalModuleInitFragment} ExternalModuleInitFragment
|
---|
| 54 | */
|
---|
| 55 | merge(other) {
|
---|
| 56 | const newSpecifiersMap = new Map(this.specifiers);
|
---|
| 57 | for (const [name, specifiers] of other.specifiers) {
|
---|
| 58 | if (newSpecifiersMap.has(name)) {
|
---|
| 59 | const currentSpecifiers =
|
---|
| 60 | /** @type {Set<string>} */
|
---|
| 61 | (newSpecifiersMap.get(name));
|
---|
| 62 | for (const spec of specifiers) currentSpecifiers.add(spec);
|
---|
| 63 | } else {
|
---|
| 64 | newSpecifiersMap.set(name, specifiers);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | return new ExternalModuleInitFragment(
|
---|
| 68 | this.importedModule,
|
---|
| 69 | newSpecifiersMap,
|
---|
| 70 | this.defaultImport
|
---|
| 71 | );
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * @param {GenerateContext} context context
|
---|
| 76 | * @returns {string | Source | undefined} the source code that will be included as initialization code
|
---|
| 77 | */
|
---|
| 78 | getContent({ runtimeRequirements }) {
|
---|
| 79 | const namedImports = [];
|
---|
| 80 |
|
---|
| 81 | for (const [name, specifiers] of this.specifiers) {
|
---|
| 82 | for (const spec of specifiers) {
|
---|
| 83 | if (spec === name) {
|
---|
| 84 | namedImports.push(name);
|
---|
| 85 | } else {
|
---|
| 86 | namedImports.push(`${name} as ${spec}`);
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | let importsString =
|
---|
| 92 | namedImports.length > 0 ? `{${namedImports.join(",")}}` : "";
|
---|
| 93 |
|
---|
| 94 | if (this.defaultImport) {
|
---|
| 95 | importsString = `${this.defaultImport}${
|
---|
| 96 | importsString ? `, ${importsString}` : ""
|
---|
| 97 | }`;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return `import ${importsString} from ${JSON.stringify(
|
---|
| 101 | this.importedModule
|
---|
| 102 | )};`;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * @param {ObjectSerializerContext} context context
|
---|
| 107 | */
|
---|
| 108 | serialize(context) {
|
---|
| 109 | super.serialize(context);
|
---|
| 110 | const { write } = context;
|
---|
| 111 | write(this.importedModule);
|
---|
| 112 | write(this.specifiers);
|
---|
| 113 | write(this.defaultImport);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * @param {ObjectDeserializerContext} context context
|
---|
| 118 | */
|
---|
| 119 | deserialize(context) {
|
---|
| 120 | super.deserialize(context);
|
---|
| 121 | const { read } = context;
|
---|
| 122 | this.importedModule = read();
|
---|
| 123 | this.specifiers = read();
|
---|
| 124 | this.defaultImport = read();
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | makeSerializable(
|
---|
| 129 | ExternalModuleInitFragment,
|
---|
| 130 | "webpack/lib/dependencies/ExternalModuleInitFragment"
|
---|
| 131 | );
|
---|
| 132 |
|
---|
| 133 | module.exports = ExternalModuleInitFragment;
|
---|