1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const Dependency = require("../Dependency");
|
---|
9 | const makeSerializable = require("../util/makeSerializable");
|
---|
10 | const ModuleDependency = require("./ModuleDependency");
|
---|
11 |
|
---|
12 | /** @typedef {import("../ContextModule")} ContextModule */
|
---|
13 | /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
---|
14 | /** @typedef {import("../Module")} Module */
|
---|
15 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
---|
16 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
17 | /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
|
---|
18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
20 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
21 |
|
---|
22 | class ContextElementDependency extends ModuleDependency {
|
---|
23 | /**
|
---|
24 | * @param {string} request request
|
---|
25 | * @param {string|undefined} userRequest user request
|
---|
26 | * @param {string | undefined} typePrefix type prefix
|
---|
27 | * @param {string} category category
|
---|
28 | * @param {(string[][] | null)=} referencedExports referenced exports
|
---|
29 | * @param {string=} context context
|
---|
30 | * @param {ImportAttributes=} attributes import assertions
|
---|
31 | */
|
---|
32 | constructor(
|
---|
33 | request,
|
---|
34 | userRequest,
|
---|
35 | typePrefix,
|
---|
36 | category,
|
---|
37 | referencedExports,
|
---|
38 | context,
|
---|
39 | attributes
|
---|
40 | ) {
|
---|
41 | super(request);
|
---|
42 | this.referencedExports = referencedExports;
|
---|
43 | this._typePrefix = typePrefix;
|
---|
44 | this._category = category;
|
---|
45 | this._context = context || undefined;
|
---|
46 |
|
---|
47 | if (userRequest) {
|
---|
48 | this.userRequest = userRequest;
|
---|
49 | }
|
---|
50 |
|
---|
51 | this.assertions = attributes;
|
---|
52 | }
|
---|
53 |
|
---|
54 | get type() {
|
---|
55 | if (this._typePrefix) {
|
---|
56 | return `${this._typePrefix} context element`;
|
---|
57 | }
|
---|
58 |
|
---|
59 | return "context element";
|
---|
60 | }
|
---|
61 |
|
---|
62 | get category() {
|
---|
63 | return this._category;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Returns list of exports referenced by this dependency
|
---|
68 | * @param {ModuleGraph} moduleGraph module graph
|
---|
69 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
---|
70 | * @returns {(string[] | ReferencedExport)[]} referenced exports
|
---|
71 | */
|
---|
72 | getReferencedExports(moduleGraph, runtime) {
|
---|
73 | if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
|
---|
74 | const refs = [];
|
---|
75 | for (const referencedExport of this.referencedExports) {
|
---|
76 | if (
|
---|
77 | this._typePrefix === "import()" &&
|
---|
78 | referencedExport[0] === "default"
|
---|
79 | ) {
|
---|
80 | const selfModule =
|
---|
81 | /** @type {ContextModule} */
|
---|
82 | (moduleGraph.getParentModule(this));
|
---|
83 | const importedModule =
|
---|
84 | /** @type {Module} */
|
---|
85 | (moduleGraph.getModule(this));
|
---|
86 | const exportsType = importedModule.getExportsType(
|
---|
87 | moduleGraph,
|
---|
88 | selfModule.options.namespaceObject === "strict"
|
---|
89 | );
|
---|
90 | if (
|
---|
91 | exportsType === "default-only" ||
|
---|
92 | exportsType === "default-with-named"
|
---|
93 | ) {
|
---|
94 | return Dependency.EXPORTS_OBJECT_REFERENCED;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | refs.push({
|
---|
98 | name: referencedExport,
|
---|
99 | canMangle: false
|
---|
100 | });
|
---|
101 | }
|
---|
102 | return refs;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * @param {ObjectSerializerContext} context context
|
---|
107 | */
|
---|
108 | serialize(context) {
|
---|
109 | const { write } = context;
|
---|
110 | write(this._typePrefix);
|
---|
111 | write(this._category);
|
---|
112 | write(this.referencedExports);
|
---|
113 | write(this.assertions);
|
---|
114 | super.serialize(context);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * @param {ObjectDeserializerContext} context context
|
---|
119 | */
|
---|
120 | deserialize(context) {
|
---|
121 | const { read } = context;
|
---|
122 | this._typePrefix = read();
|
---|
123 | this._category = read();
|
---|
124 | this.referencedExports = read();
|
---|
125 | this.assertions = read();
|
---|
126 | super.deserialize(context);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | makeSerializable(
|
---|
131 | ContextElementDependency,
|
---|
132 | "webpack/lib/dependencies/ContextElementDependency"
|
---|
133 | );
|
---|
134 |
|
---|
135 | module.exports = ContextElementDependency;
|
---|