source: trip-planner-front/node_modules/webpack/lib/dependencies/CommonJsExportsDependency.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • 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 Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const InitFragment = require("../InitFragment");
9const makeSerializable = require("../util/makeSerializable");
10const propertyAccess = require("../util/propertyAccess");
11const { handleDependencyBase } = require("./CommonJsDependencyHelpers");
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("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
18/** @typedef {import("../ModuleGraph")} ModuleGraph */
19
20const EMPTY_OBJECT = {};
21
22class CommonJsExportsDependency extends NullDependency {
23 constructor(range, valueRange, base, names) {
24 super();
25 this.range = range;
26 this.valueRange = valueRange;
27 this.base = base;
28 this.names = names;
29 }
30
31 get type() {
32 return "cjs exports";
33 }
34
35 /**
36 * Returns the exported names
37 * @param {ModuleGraph} moduleGraph module graph
38 * @returns {ExportsSpec | undefined} export names
39 */
40 getExports(moduleGraph) {
41 const name = this.names[0];
42 return {
43 exports: [
44 {
45 name,
46 // we can't mangle names that are in an empty object
47 // because one could access the prototype property
48 // when export isn't set yet
49 canMangle: !(name in EMPTY_OBJECT)
50 }
51 ],
52 dependencies: undefined
53 };
54 }
55
56 serialize(context) {
57 const { write } = context;
58 write(this.range);
59 write(this.valueRange);
60 write(this.base);
61 write(this.names);
62 super.serialize(context);
63 }
64
65 deserialize(context) {
66 const { read } = context;
67 this.range = read();
68 this.valueRange = read();
69 this.base = read();
70 this.names = read();
71 super.deserialize(context);
72 }
73}
74
75makeSerializable(
76 CommonJsExportsDependency,
77 "webpack/lib/dependencies/CommonJsExportsDependency"
78);
79
80CommonJsExportsDependency.Template = class CommonJsExportsDependencyTemplate extends (
81 NullDependency.Template
82) {
83 /**
84 * @param {Dependency} dependency the dependency for which the template should be applied
85 * @param {ReplaceSource} source the current replace source which can be modified
86 * @param {DependencyTemplateContext} templateContext the context object
87 * @returns {void}
88 */
89 apply(
90 dependency,
91 source,
92 { module, moduleGraph, initFragments, runtimeRequirements, runtime }
93 ) {
94 const dep = /** @type {CommonJsExportsDependency} */ (dependency);
95 const used = moduleGraph
96 .getExportsInfo(module)
97 .getUsedName(dep.names, runtime);
98
99 const [type, base] = handleDependencyBase(
100 dep.base,
101 module,
102 runtimeRequirements
103 );
104
105 switch (type) {
106 case "expression":
107 if (!used) {
108 initFragments.push(
109 new InitFragment(
110 "var __webpack_unused_export__;\n",
111 InitFragment.STAGE_CONSTANTS,
112 0,
113 "__webpack_unused_export__"
114 )
115 );
116 source.replace(
117 dep.range[0],
118 dep.range[1] - 1,
119 "__webpack_unused_export__"
120 );
121 return;
122 }
123 source.replace(
124 dep.range[0],
125 dep.range[1] - 1,
126 `${base}${propertyAccess(used)}`
127 );
128 return;
129 case "Object.defineProperty":
130 if (!used) {
131 initFragments.push(
132 new InitFragment(
133 "var __webpack_unused_export__;\n",
134 InitFragment.STAGE_CONSTANTS,
135 0,
136 "__webpack_unused_export__"
137 )
138 );
139 source.replace(
140 dep.range[0],
141 dep.valueRange[0] - 1,
142 "__webpack_unused_export__ = ("
143 );
144 source.replace(dep.valueRange[1], dep.range[1] - 1, ")");
145 return;
146 }
147 source.replace(
148 dep.range[0],
149 dep.valueRange[0] - 1,
150 `Object.defineProperty(${base}${propertyAccess(
151 used.slice(0, -1)
152 )}, ${JSON.stringify(used[used.length - 1])}, (`
153 );
154 source.replace(dep.valueRange[1], dep.range[1] - 1, "))");
155 return;
156 }
157 }
158};
159
160module.exports = CommonJsExportsDependency;
Note: See TracBrowser for help on using the repository browser.