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 { UsageState } = require("../ExportsInfo");
|
---|
9 | const makeSerializable = require("../util/makeSerializable");
|
---|
10 | const NullDependency = require("./NullDependency");
|
---|
11 |
|
---|
12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
13 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
14 | /** @typedef {import("../Dependency")} Dependency */
|
---|
15 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
16 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
17 | /** @typedef {import("../Module")} Module */
|
---|
18 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
19 | /** @typedef {import("../util/Hash")} Hash */
|
---|
20 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
24 | * @param {Module} module the module
|
---|
25 | * @param {string | null} exportName name of the export if any
|
---|
26 | * @param {string | null} property name of the requested property
|
---|
27 | * @param {RuntimeSpec} runtime for which runtime
|
---|
28 | * @returns {any} value of the property
|
---|
29 | */
|
---|
30 | const getProperty = (moduleGraph, module, exportName, property, runtime) => {
|
---|
31 | if (!exportName) {
|
---|
32 | switch (property) {
|
---|
33 | case "usedExports": {
|
---|
34 | const usedExports = moduleGraph
|
---|
35 | .getExportsInfo(module)
|
---|
36 | .getUsedExports(runtime);
|
---|
37 | if (
|
---|
38 | typeof usedExports === "boolean" ||
|
---|
39 | usedExports === undefined ||
|
---|
40 | usedExports === null
|
---|
41 | ) {
|
---|
42 | return usedExports;
|
---|
43 | }
|
---|
44 | return Array.from(usedExports).sort();
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 | switch (property) {
|
---|
49 | case "used":
|
---|
50 | return (
|
---|
51 | moduleGraph.getExportsInfo(module).getUsed(exportName, runtime) !==
|
---|
52 | UsageState.Unused
|
---|
53 | );
|
---|
54 | case "useInfo": {
|
---|
55 | const state = moduleGraph
|
---|
56 | .getExportsInfo(module)
|
---|
57 | .getUsed(exportName, runtime);
|
---|
58 | switch (state) {
|
---|
59 | case UsageState.Used:
|
---|
60 | case UsageState.OnlyPropertiesUsed:
|
---|
61 | return true;
|
---|
62 | case UsageState.Unused:
|
---|
63 | return false;
|
---|
64 | case UsageState.NoInfo:
|
---|
65 | return undefined;
|
---|
66 | case UsageState.Unknown:
|
---|
67 | return null;
|
---|
68 | default:
|
---|
69 | throw new Error(`Unexpected UsageState ${state}`);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | case "provideInfo":
|
---|
73 | return moduleGraph.getExportsInfo(module).isExportProvided(exportName);
|
---|
74 | }
|
---|
75 | return undefined;
|
---|
76 | };
|
---|
77 |
|
---|
78 | class ExportsInfoDependency extends NullDependency {
|
---|
79 | constructor(range, exportName, property) {
|
---|
80 | super();
|
---|
81 | this.range = range;
|
---|
82 | this.exportName = exportName;
|
---|
83 | this.property = property;
|
---|
84 | }
|
---|
85 |
|
---|
86 | serialize(context) {
|
---|
87 | const { write } = context;
|
---|
88 | write(this.range);
|
---|
89 | write(this.exportName);
|
---|
90 | write(this.property);
|
---|
91 | super.serialize(context);
|
---|
92 | }
|
---|
93 |
|
---|
94 | static deserialize(context) {
|
---|
95 | const obj = new ExportsInfoDependency(
|
---|
96 | context.read(),
|
---|
97 | context.read(),
|
---|
98 | context.read()
|
---|
99 | );
|
---|
100 | obj.deserialize(context);
|
---|
101 | return obj;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | makeSerializable(
|
---|
106 | ExportsInfoDependency,
|
---|
107 | "webpack/lib/dependencies/ExportsInfoDependency"
|
---|
108 | );
|
---|
109 |
|
---|
110 | ExportsInfoDependency.Template = class ExportsInfoDependencyTemplate extends (
|
---|
111 | NullDependency.Template
|
---|
112 | ) {
|
---|
113 | /**
|
---|
114 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
115 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
116 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
117 | * @returns {void}
|
---|
118 | */
|
---|
119 | apply(dependency, source, { module, moduleGraph, runtime }) {
|
---|
120 | const dep = /** @type {ExportsInfoDependency} */ (dependency);
|
---|
121 |
|
---|
122 | const value = getProperty(
|
---|
123 | moduleGraph,
|
---|
124 | module,
|
---|
125 | dep.exportName,
|
---|
126 | dep.property,
|
---|
127 | runtime
|
---|
128 | );
|
---|
129 | source.replace(
|
---|
130 | dep.range[0],
|
---|
131 | dep.range[1] - 1,
|
---|
132 | value === undefined ? "undefined" : JSON.stringify(value)
|
---|
133 | );
|
---|
134 | }
|
---|
135 | };
|
---|
136 |
|
---|
137 | module.exports = ExportsInfoDependency;
|
---|