source: frontend/node_modules/webpack/lib/dependencies/CommonJsSelfReferenceDependency.js@ cdcff72

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

Fix frontend appearance

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const { equals } = require("../util/ArrayHelpers");
10const makeSerializable = require("../util/makeSerializable");
11const { propertyAccess } = require("../util/property");
12const NullDependency = require("./NullDependency");
13
14/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
15/** @typedef {import("../Dependency")} Dependency */
16/** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
17/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
18/** @typedef {import("../ModuleGraph")} ModuleGraph */
19/** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
20/** @typedef {import("../javascript/JavascriptParser").Range} Range */
21/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
22/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
23/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
24/** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */
25
26class CommonJsSelfReferenceDependency extends NullDependency {
27 /**
28 * Creates an instance of CommonJsSelfReferenceDependency.
29 * @param {Range} range range
30 * @param {CommonJSDependencyBaseKeywords} base base
31 * @param {ExportInfoName[]} names names
32 * @param {boolean} call is a call
33 */
34 constructor(range, base, names, call) {
35 super();
36 this.range = range;
37 this.base = base;
38 this.names = names;
39 this.call = call;
40 }
41
42 get type() {
43 return "cjs self exports reference";
44 }
45
46 get category() {
47 return "self";
48 }
49
50 /**
51 * Returns an identifier to merge equal requests.
52 * @returns {string | null} an identifier to merge equal requests
53 */
54 getResourceIdentifier() {
55 return "self";
56 }
57
58 /**
59 * Returns list of exports referenced by this dependency
60 * @param {ModuleGraph} moduleGraph module graph
61 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
62 * @returns {ReferencedExports} referenced exports
63 */
64 getReferencedExports(moduleGraph, runtime) {
65 return [this.call ? this.names.slice(0, -1) : this.names];
66 }
67
68 /**
69 * Serializes this instance into the provided serializer context.
70 * @param {ObjectSerializerContext} context context
71 */
72 serialize(context) {
73 const { write } = context;
74 write(this.range);
75 write(this.base);
76 write(this.names);
77 write(this.call);
78 super.serialize(context);
79 }
80
81 /**
82 * Restores this instance from the provided deserializer context.
83 * @param {ObjectDeserializerContext} context context
84 */
85 deserialize(context) {
86 const { read } = context;
87 this.range = read();
88 this.base = read();
89 this.names = read();
90 this.call = read();
91 super.deserialize(context);
92 }
93}
94
95makeSerializable(
96 CommonJsSelfReferenceDependency,
97 "webpack/lib/dependencies/CommonJsSelfReferenceDependency"
98);
99
100CommonJsSelfReferenceDependency.Template = class CommonJsSelfReferenceDependencyTemplate extends (
101 NullDependency.Template
102) {
103 /**
104 * Applies the plugin by registering its hooks on the compiler.
105 * @param {Dependency} dependency the dependency for which the template should be applied
106 * @param {ReplaceSource} source the current replace source which can be modified
107 * @param {DependencyTemplateContext} templateContext the context object
108 * @returns {void}
109 */
110 apply(
111 dependency,
112 source,
113 { module, moduleGraph, runtime, runtimeRequirements }
114 ) {
115 const dep = /** @type {CommonJsSelfReferenceDependency} */ (dependency);
116 const used =
117 dep.names.length === 0
118 ? dep.names
119 : moduleGraph.getExportsInfo(module).getUsedName(dep.names, runtime);
120 if (!used) {
121 throw new Error(
122 "Self-reference dependency has unused export name: This should not happen"
123 );
124 }
125
126 /** @type {string} */
127 let base;
128 switch (dep.base) {
129 case "exports":
130 runtimeRequirements.add(RuntimeGlobals.exports);
131 base = module.exportsArgument;
132 break;
133 case "module.exports":
134 runtimeRequirements.add(RuntimeGlobals.module);
135 base = `${module.moduleArgument}.exports`;
136 break;
137 case "this":
138 runtimeRequirements.add(RuntimeGlobals.thisAsExports);
139 base = "this";
140 break;
141 default:
142 throw new Error(`Unsupported base ${dep.base}`);
143 }
144
145 if (base === dep.base && equals(used, dep.names)) {
146 // Nothing has to be changed
147 // We don't use a replacement for compat reasons
148 // for plugins that update `module._source` which they
149 // shouldn't do!
150 return;
151 }
152
153 source.replace(
154 dep.range[0],
155 dep.range[1] - 1,
156 `${base}${propertyAccess(used)}`
157 );
158 }
159};
160
161module.exports = CommonJsSelfReferenceDependency;
Note: See TracBrowser for help on using the repository browser.