source: frontend/node_modules/webpack/lib/dependencies/ExternalModuleInitFragment.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Ivan Kopeykin @vankop
4*/
5
6"use strict";
7
8const InitFragment = require("../InitFragment");
9const 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 {{ name: string, value?: string }[]} ArrayImportSpecifiers */
16/** @typedef {Set<string>} ImportSpecifier */
17/** @typedef {Map<string, ImportSpecifier>} ImportSpecifiers */
18
19/**
20 * @extends {InitFragment<GenerateContext>}
21 */
22class ExternalModuleInitFragment extends InitFragment {
23 /**
24 * @param {string} importedModule imported module
25 * @param {ArrayImportSpecifiers | ImportSpecifiers} specifiers import specifiers
26 * @param {string=} defaultImport default import
27 */
28 constructor(importedModule, specifiers, defaultImport) {
29 super(
30 undefined,
31 InitFragment.STAGE_CONSTANTS,
32 0,
33 `external module imports|${importedModule}|${defaultImport || "null"}`
34 );
35 this.importedModule = importedModule;
36 if (Array.isArray(specifiers)) {
37 /** @type {ImportSpecifiers} */
38 this.specifiers = new Map();
39 for (const { name, value } of specifiers) {
40 let specifiers = this.specifiers.get(name);
41 if (!specifiers) {
42 /** @type {ImportSpecifier} */
43 specifiers = new Set();
44 this.specifiers.set(name, specifiers);
45 }
46 specifiers.add(value || name);
47 }
48 } else {
49 this.specifiers = specifiers;
50 }
51 this.defaultImport = defaultImport;
52 }
53
54 /**
55 * @param {ExternalModuleInitFragment} other other
56 * @returns {ExternalModuleInitFragment} ExternalModuleInitFragment
57 */
58 merge(other) {
59 const newSpecifiersMap = new Map(this.specifiers);
60 for (const [name, specifiers] of other.specifiers) {
61 if (newSpecifiersMap.has(name)) {
62 const currentSpecifiers =
63 /** @type {Set<string>} */
64 (newSpecifiersMap.get(name));
65 for (const spec of specifiers) currentSpecifiers.add(spec);
66 } else {
67 newSpecifiersMap.set(name, specifiers);
68 }
69 }
70 return new ExternalModuleInitFragment(
71 this.importedModule,
72 newSpecifiersMap,
73 this.defaultImport
74 );
75 }
76
77 /**
78 * Returns the source code that will be included as initialization code.
79 * @param {GenerateContext} context context
80 * @returns {string | Source | undefined} the source code that will be included as initialization code
81 */
82 getContent({ runtimeRequirements }) {
83 /** @type {string[]} */
84 const namedImports = [];
85
86 for (const [name, specifiers] of this.specifiers) {
87 for (const spec of specifiers) {
88 if (spec === name) {
89 namedImports.push(name);
90 } else {
91 namedImports.push(`${name} as ${spec}`);
92 }
93 }
94 }
95
96 let importsString =
97 namedImports.length > 0 ? `{${namedImports.join(",")}}` : "";
98
99 if (this.defaultImport) {
100 importsString = `${this.defaultImport}${
101 importsString ? `, ${importsString}` : ""
102 }`;
103 }
104
105 return `import ${importsString} from ${JSON.stringify(
106 this.importedModule
107 )};\n`;
108 }
109
110 /**
111 * Serializes this instance into the provided serializer context.
112 * @param {ObjectSerializerContext} context context
113 */
114 serialize(context) {
115 super.serialize(context);
116 const { write } = context;
117 write(this.importedModule);
118 write(this.specifiers);
119 write(this.defaultImport);
120 }
121
122 /**
123 * Restores this instance from the provided deserializer context.
124 * @param {ObjectDeserializerContext} context context
125 */
126 deserialize(context) {
127 super.deserialize(context);
128 const { read } = context;
129 this.importedModule = read();
130 this.specifiers = read();
131 this.defaultImport = read();
132 }
133}
134
135makeSerializable(
136 ExternalModuleInitFragment,
137 "webpack/lib/dependencies/ExternalModuleInitFragment"
138);
139
140module.exports = ExternalModuleInitFragment;
Note: See TracBrowser for help on using the repository browser.