| 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("../Dependency")} Dependency */
|
|---|
| 14 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 15 | /** @typedef {import("../Module")} Module */
|
|---|
| 16 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 17 | /** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
|
|---|
| 18 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 20 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 21 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Defines the sortable set type used by this module.
|
|---|
| 25 | * @template T
|
|---|
| 26 | * @typedef {import("../util/SortableSet")<T>} SortableSet
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Returns value of the property.
|
|---|
| 31 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 32 | * @param {Module} module the module
|
|---|
| 33 | * @param {ExportInfoName[] | null} exportName_ name of the export if any
|
|---|
| 34 | * @param {string | null} property name of the requested property
|
|---|
| 35 | * @param {RuntimeSpec} runtime for which runtime
|
|---|
| 36 | * @returns {undefined | null | boolean | ExportInfoName[]} value of the property
|
|---|
| 37 | */
|
|---|
| 38 | const getProperty = (moduleGraph, module, exportName_, property, runtime) => {
|
|---|
| 39 | if (!exportName_) {
|
|---|
| 40 | switch (property) {
|
|---|
| 41 | case "usedExports": {
|
|---|
| 42 | const usedExports = moduleGraph
|
|---|
| 43 | .getExportsInfo(module)
|
|---|
| 44 | .getUsedExports(runtime);
|
|---|
| 45 | if (
|
|---|
| 46 | typeof usedExports === "boolean" ||
|
|---|
| 47 | usedExports === undefined ||
|
|---|
| 48 | usedExports === null
|
|---|
| 49 | ) {
|
|---|
| 50 | return usedExports;
|
|---|
| 51 | }
|
|---|
| 52 | return [...usedExports].sort();
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | const exportName = /** @type {ExportInfoName[]} */ (exportName_);
|
|---|
| 57 | switch (property) {
|
|---|
| 58 | case "canMangle": {
|
|---|
| 59 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
|---|
| 60 | const exportInfo = exportsInfo.getReadOnlyExportInfoRecursive(exportName);
|
|---|
| 61 | if (exportInfo) return exportInfo.canMangle;
|
|---|
| 62 | return exportsInfo.otherExportsInfo.canMangle;
|
|---|
| 63 | }
|
|---|
| 64 | case "used":
|
|---|
| 65 | return (
|
|---|
| 66 | moduleGraph.getExportsInfo(module).getUsed(exportName, runtime) !==
|
|---|
| 67 | UsageState.Unused
|
|---|
| 68 | );
|
|---|
| 69 | case "useInfo": {
|
|---|
| 70 | const state = moduleGraph
|
|---|
| 71 | .getExportsInfo(module)
|
|---|
| 72 | .getUsed(exportName, runtime);
|
|---|
| 73 | switch (state) {
|
|---|
| 74 | case UsageState.Used:
|
|---|
| 75 | case UsageState.OnlyPropertiesUsed:
|
|---|
| 76 | return true;
|
|---|
| 77 | case UsageState.Unused:
|
|---|
| 78 | return false;
|
|---|
| 79 | case UsageState.NoInfo:
|
|---|
| 80 | return;
|
|---|
| 81 | case UsageState.Unknown:
|
|---|
| 82 | return null;
|
|---|
| 83 | default:
|
|---|
| 84 | throw new Error(`Unexpected UsageState ${state}`);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | case "provideInfo":
|
|---|
| 88 | return moduleGraph.getExportsInfo(module).isExportProvided(exportName);
|
|---|
| 89 | }
|
|---|
| 90 | };
|
|---|
| 91 |
|
|---|
| 92 | class ExportsInfoDependency extends NullDependency {
|
|---|
| 93 | /**
|
|---|
| 94 | * Creates an instance of ExportsInfoDependency.
|
|---|
| 95 | * @param {Range} range range
|
|---|
| 96 | * @param {ExportInfoName[] | null} exportName export name
|
|---|
| 97 | * @param {string | null} property property
|
|---|
| 98 | */
|
|---|
| 99 | constructor(range, exportName, property) {
|
|---|
| 100 | super();
|
|---|
| 101 | this.range = range;
|
|---|
| 102 | this.exportName = exportName;
|
|---|
| 103 | this.property = property;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Serializes this instance into the provided serializer context.
|
|---|
| 108 | * @param {ObjectSerializerContext} context context
|
|---|
| 109 | */
|
|---|
| 110 | serialize(context) {
|
|---|
| 111 | const { write } = context;
|
|---|
| 112 | write(this.range);
|
|---|
| 113 | write(this.exportName);
|
|---|
| 114 | write(this.property);
|
|---|
| 115 | super.serialize(context);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | * Restores this instance from the provided deserializer context.
|
|---|
| 120 | * @param {ObjectDeserializerContext} context context
|
|---|
| 121 | * @returns {ExportsInfoDependency} ExportsInfoDependency
|
|---|
| 122 | */
|
|---|
| 123 | static deserialize(context) {
|
|---|
| 124 | const obj = new ExportsInfoDependency(
|
|---|
| 125 | context.read(),
|
|---|
| 126 | context.read(),
|
|---|
| 127 | context.read()
|
|---|
| 128 | );
|
|---|
| 129 | obj.deserialize(context);
|
|---|
| 130 | return obj;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | makeSerializable(
|
|---|
| 135 | ExportsInfoDependency,
|
|---|
| 136 | "webpack/lib/dependencies/ExportsInfoDependency"
|
|---|
| 137 | );
|
|---|
| 138 |
|
|---|
| 139 | ExportsInfoDependency.Template = class ExportsInfoDependencyTemplate extends (
|
|---|
| 140 | NullDependency.Template
|
|---|
| 141 | ) {
|
|---|
| 142 | /**
|
|---|
| 143 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 144 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 145 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 146 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 147 | * @returns {void}
|
|---|
| 148 | */
|
|---|
| 149 | apply(dependency, source, { module, moduleGraph, runtime }) {
|
|---|
| 150 | const dep = /** @type {ExportsInfoDependency} */ (dependency);
|
|---|
| 151 |
|
|---|
| 152 | const value = getProperty(
|
|---|
| 153 | moduleGraph,
|
|---|
| 154 | module,
|
|---|
| 155 | dep.exportName,
|
|---|
| 156 | dep.property,
|
|---|
| 157 | runtime
|
|---|
| 158 | );
|
|---|
| 159 | source.replace(
|
|---|
| 160 | dep.range[0],
|
|---|
| 161 | dep.range[1] - 1,
|
|---|
| 162 | value === undefined ? "undefined" : JSON.stringify(value)
|
|---|
| 163 | );
|
|---|
| 164 | }
|
|---|
| 165 | };
|
|---|
| 166 |
|
|---|
| 167 | module.exports = ExportsInfoDependency;
|
|---|