UNPKG

2.63 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Dependency = require("../Dependency");
9const makeSerializable = require("../util/makeSerializable");
10const ModuleDependency = require("./ModuleDependency");
11
12/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
13/** @typedef {import("../ModuleGraph")} ModuleGraph */
14/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
15
16class ContextElementDependency extends ModuleDependency {
17 /**
18 * @param {string} request request
19 * @param {string|undefined} userRequest user request
20 * @param {string} typePrefix type prefix
21 * @param {string} category category
22 * @param {string[][]=} referencedExports referenced exports
23 * @param {string=} context context
24 */
25 constructor(
26 request,
27 userRequest,
28 typePrefix,
29 category,
30 referencedExports,
31 context
32 ) {
33 super(request);
34 this.referencedExports = referencedExports;
35 this._typePrefix = typePrefix;
36 this._category = category;
37 this._context = context || undefined;
38
39 if (userRequest) {
40 this.userRequest = userRequest;
41 }
42 }
43
44 get type() {
45 if (this._typePrefix) {
46 return `${this._typePrefix} context element`;
47 }
48
49 return "context element";
50 }
51
52 /**
53 * @returns {string | undefined} a request context
54 */
55 getContext() {
56 return this._context;
57 }
58
59 /**
60 * @returns {string | null} an identifier to merge equal requests
61 */
62 getResourceIdentifier() {
63 return `context${this._context || ""}|${super.getResourceIdentifier()}`;
64 }
65
66 get category() {
67 return this._category;
68 }
69
70 /**
71 * Returns list of exports referenced by this dependency
72 * @param {ModuleGraph} moduleGraph module graph
73 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
74 * @returns {(string[] | ReferencedExport)[]} referenced exports
75 */
76 getReferencedExports(moduleGraph, runtime) {
77 return this.referencedExports
78 ? this.referencedExports.map(e => ({
79 name: e,
80 canMangle: false
81 }))
82 : Dependency.EXPORTS_OBJECT_REFERENCED;
83 }
84
85 serialize(context) {
86 const { write } = context;
87 write(this._typePrefix);
88 write(this._category);
89 write(this._context);
90 write(this.referencedExports);
91 super.serialize(context);
92 }
93
94 deserialize(context) {
95 const { read } = context;
96 this._typePrefix = read();
97 this._category = read();
98 this._context = read();
99 this.referencedExports = read();
100 super.deserialize(context);
101 }
102}
103
104makeSerializable(
105 ContextElementDependency,
106 "webpack/lib/dependencies/ContextElementDependency"
107);
108
109module.exports = ContextElementDependency;