1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const RuntimeGlobals = require("../RuntimeGlobals");
|
9 | const { equals } = require("../util/ArrayHelpers");
|
10 | const makeSerializable = require("../util/makeSerializable");
|
11 | const propertyAccess = require("../util/propertyAccess");
|
12 | const NullDependency = require("./NullDependency");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class CommonJsSelfReferenceDependency extends NullDependency {
|
23 | constructor(range, base, names, call) {
|
24 | super();
|
25 | this.range = range;
|
26 | this.base = base;
|
27 | this.names = names;
|
28 | this.call = call;
|
29 | }
|
30 |
|
31 | get type() {
|
32 | return "cjs self exports reference";
|
33 | }
|
34 |
|
35 | get category() {
|
36 | return "self";
|
37 | }
|
38 |
|
39 | |
40 |
|
41 |
|
42 | getResourceIdentifier() {
|
43 | return `self`;
|
44 | }
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | getReferencedExports(moduleGraph, runtime) {
|
53 | return [this.call ? this.names.slice(0, -1) : this.names];
|
54 | }
|
55 |
|
56 | serialize(context) {
|
57 | const { write } = context;
|
58 | write(this.range);
|
59 | write(this.base);
|
60 | write(this.names);
|
61 | write(this.call);
|
62 | super.serialize(context);
|
63 | }
|
64 |
|
65 | deserialize(context) {
|
66 | const { read } = context;
|
67 | this.range = read();
|
68 | this.base = read();
|
69 | this.names = read();
|
70 | this.call = read();
|
71 | super.deserialize(context);
|
72 | }
|
73 | }
|
74 |
|
75 | makeSerializable(
|
76 | CommonJsSelfReferenceDependency,
|
77 | "webpack/lib/dependencies/CommonJsSelfReferenceDependency"
|
78 | );
|
79 |
|
80 | CommonJsSelfReferenceDependency.Template = class CommonJsSelfReferenceDependencyTemplate extends (
|
81 | NullDependency.Template
|
82 | ) {
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | apply(
|
90 | dependency,
|
91 | source,
|
92 | { module, moduleGraph, runtime, runtimeRequirements }
|
93 | ) {
|
94 | const dep = (dependency);
|
95 | let used;
|
96 | if (dep.names.length === 0) {
|
97 | used = dep.names;
|
98 | } else {
|
99 | used = moduleGraph.getExportsInfo(module).getUsedName(dep.names, runtime);
|
100 | }
|
101 | if (!used) {
|
102 | throw new Error(
|
103 | "Self-reference dependency has unused export name: This should not happen"
|
104 | );
|
105 | }
|
106 |
|
107 | let base = undefined;
|
108 | switch (dep.base) {
|
109 | case "exports":
|
110 | runtimeRequirements.add(RuntimeGlobals.exports);
|
111 | base = module.exportsArgument;
|
112 | break;
|
113 | case "module.exports":
|
114 | runtimeRequirements.add(RuntimeGlobals.module);
|
115 | base = `${module.moduleArgument}.exports`;
|
116 | break;
|
117 | case "this":
|
118 | runtimeRequirements.add(RuntimeGlobals.thisAsExports);
|
119 | base = "this";
|
120 | break;
|
121 | default:
|
122 | throw new Error(`Unsupported base ${dep.base}`);
|
123 | }
|
124 |
|
125 | if (base === dep.base && equals(used, dep.names)) {
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | return;
|
131 | }
|
132 |
|
133 | source.replace(
|
134 | dep.range[0],
|
135 | dep.range[1] - 1,
|
136 | `${base}${propertyAccess(used)}`
|
137 | );
|
138 | }
|
139 | };
|
140 |
|
141 | module.exports = CommonJsSelfReferenceDependency;
|