source: imaps-frontend/node_modules/webpack/lib/dependencies/CommonJsSelfReferenceDependency.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

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