source: imaps-frontend/node_modules/webpack/lib/dependencies/CommonJsFullRequireDependency.js@ 79a0317

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.6 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 Template = require("../Template");
9const { equals } = require("../util/ArrayHelpers");
10const { getTrimmedIdsAndRange } = require("../util/chainedImports");
11const makeSerializable = require("../util/makeSerializable");
12const propertyAccess = require("../util/propertyAccess");
13const ModuleDependency = require("./ModuleDependency");
14
15/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
16/** @typedef {import("../Dependency")} Dependency */
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
25class CommonJsFullRequireDependency extends ModuleDependency {
26 /**
27 * @param {string} request the request string
28 * @param {Range} range location in source code
29 * @param {string[]} names accessed properties on module
30 * @param {Range[]=} idRanges ranges for members of ids; the two arrays are right-aligned
31 */
32 constructor(
33 request,
34 range,
35 names,
36 idRanges /* TODO webpack 6 make this non-optional. It must always be set to properly trim ids. */
37 ) {
38 super(request);
39 this.range = range;
40 this.names = names;
41 this.idRanges = idRanges;
42 this.call = false;
43 this.asiSafe = undefined;
44 }
45
46 /**
47 * Returns list of exports referenced by this dependency
48 * @param {ModuleGraph} moduleGraph module graph
49 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
50 * @returns {(string[] | ReferencedExport)[]} referenced exports
51 */
52 getReferencedExports(moduleGraph, runtime) {
53 if (this.call) {
54 const importedModule = moduleGraph.getModule(this);
55 if (
56 !importedModule ||
57 importedModule.getExportsType(moduleGraph, false) !== "namespace"
58 ) {
59 return [this.names.slice(0, -1)];
60 }
61 }
62 return [this.names];
63 }
64
65 /**
66 * @param {ObjectSerializerContext} context context
67 */
68 serialize(context) {
69 const { write } = context;
70 write(this.names);
71 write(this.idRanges);
72 write(this.call);
73 write(this.asiSafe);
74 super.serialize(context);
75 }
76
77 /**
78 * @param {ObjectDeserializerContext} context context
79 */
80 deserialize(context) {
81 const { read } = context;
82 this.names = read();
83 this.idRanges = read();
84 this.call = read();
85 this.asiSafe = read();
86 super.deserialize(context);
87 }
88
89 get type() {
90 return "cjs full require";
91 }
92
93 get category() {
94 return "commonjs";
95 }
96}
97
98CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemplate extends (
99 ModuleDependency.Template
100) {
101 /**
102 * @param {Dependency} dependency the dependency for which the template should be applied
103 * @param {ReplaceSource} source the current replace source which can be modified
104 * @param {DependencyTemplateContext} templateContext the context object
105 * @returns {void}
106 */
107 apply(
108 dependency,
109 source,
110 {
111 module,
112 runtimeTemplate,
113 moduleGraph,
114 chunkGraph,
115 runtimeRequirements,
116 runtime,
117 initFragments
118 }
119 ) {
120 const dep = /** @type {CommonJsFullRequireDependency} */ (dependency);
121 if (!dep.range) return;
122 const importedModule = moduleGraph.getModule(dep);
123 let requireExpr = runtimeTemplate.moduleExports({
124 module: importedModule,
125 chunkGraph,
126 request: dep.request,
127 weak: dep.weak,
128 runtimeRequirements
129 });
130
131 const {
132 trimmedRange: [trimmedRangeStart, trimmedRangeEnd],
133 trimmedIds
134 } = getTrimmedIdsAndRange(
135 dep.names,
136 dep.range,
137 dep.idRanges,
138 moduleGraph,
139 dep
140 );
141
142 if (importedModule) {
143 const usedImported = moduleGraph
144 .getExportsInfo(importedModule)
145 .getUsedName(trimmedIds, runtime);
146 if (usedImported) {
147 const comment = equals(usedImported, trimmedIds)
148 ? ""
149 : `${Template.toNormalComment(propertyAccess(trimmedIds))} `;
150 const access = `${comment}${propertyAccess(usedImported)}`;
151 requireExpr =
152 dep.asiSafe === true
153 ? `(${requireExpr}${access})`
154 : `${requireExpr}${access}`;
155 }
156 }
157 source.replace(trimmedRangeStart, trimmedRangeEnd - 1, requireExpr);
158 }
159};
160
161makeSerializable(
162 CommonJsFullRequireDependency,
163 "webpack/lib/dependencies/CommonJsFullRequireDependency"
164);
165
166module.exports = CommonJsFullRequireDependency;
Note: See TracBrowser for help on using the repository browser.