| [9af201e] | 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 InitFragment = require("../InitFragment");
|
|---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 10 | const { propertyAccess } = require("../util/property");
|
|---|
| 11 | const { handleDependencyBase } = require("./CommonJsDependencyHelpers");
|
|---|
| 12 | const NullDependency = require("./NullDependency");
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 15 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 16 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
|---|
| 17 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 18 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 19 | /** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
|
|---|
| 20 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 21 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 23 | /** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */
|
|---|
| 24 |
|
|---|
| 25 | const EMPTY_OBJECT = {};
|
|---|
| 26 |
|
|---|
| 27 | class CommonJsExportsDependency extends NullDependency {
|
|---|
| 28 | /**
|
|---|
| 29 | * Creates an instance of CommonJsExportsDependency.
|
|---|
| 30 | * @param {Range} range range
|
|---|
| 31 | * @param {Range | null} valueRange value range
|
|---|
| 32 | * @param {CommonJSDependencyBaseKeywords} base base
|
|---|
| 33 | * @param {ExportInfoName[]} names names
|
|---|
| 34 | */
|
|---|
| 35 | constructor(range, valueRange, base, names) {
|
|---|
| 36 | super();
|
|---|
| 37 | this.range = range;
|
|---|
| 38 | this.valueRange = valueRange;
|
|---|
| 39 | this.base = base;
|
|---|
| 40 | this.names = names;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | get type() {
|
|---|
| 44 | return "cjs exports";
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Returns the exported names
|
|---|
| 49 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 50 | * @returns {ExportsSpec | undefined} export names
|
|---|
| 51 | */
|
|---|
| 52 | getExports(moduleGraph) {
|
|---|
| 53 | const name = this.names[0];
|
|---|
| 54 | return {
|
|---|
| 55 | exports: [
|
|---|
| 56 | {
|
|---|
| 57 | name,
|
|---|
| 58 | // we can't mangle names that are in an empty object
|
|---|
| 59 | // because one could access the prototype property
|
|---|
| 60 | // when export isn't set yet
|
|---|
| 61 | canMangle: !(name in EMPTY_OBJECT)
|
|---|
| 62 | }
|
|---|
| 63 | ],
|
|---|
| 64 | dependencies: undefined
|
|---|
| 65 | };
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Serializes this instance into the provided serializer context.
|
|---|
| 70 | * @param {ObjectSerializerContext} context context
|
|---|
| 71 | */
|
|---|
| 72 | serialize(context) {
|
|---|
| 73 | const { write } = context;
|
|---|
| 74 | write(this.range);
|
|---|
| 75 | write(this.valueRange);
|
|---|
| 76 | write(this.base);
|
|---|
| 77 | write(this.names);
|
|---|
| 78 | super.serialize(context);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Restores this instance from the provided deserializer context.
|
|---|
| 83 | * @param {ObjectDeserializerContext} context context
|
|---|
| 84 | */
|
|---|
| 85 | deserialize(context) {
|
|---|
| 86 | const { read } = context;
|
|---|
| 87 | this.range = read();
|
|---|
| 88 | this.valueRange = read();
|
|---|
| 89 | this.base = read();
|
|---|
| 90 | this.names = read();
|
|---|
| 91 | super.deserialize(context);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | makeSerializable(
|
|---|
| 96 | CommonJsExportsDependency,
|
|---|
| 97 | "webpack/lib/dependencies/CommonJsExportsDependency"
|
|---|
| 98 | );
|
|---|
| 99 |
|
|---|
| 100 | CommonJsExportsDependency.Template = class CommonJsExportsDependencyTemplate extends (
|
|---|
| 101 | NullDependency.Template
|
|---|
| 102 | ) {
|
|---|
| 103 | /**
|
|---|
| 104 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 105 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 106 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 107 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 108 | * @returns {void}
|
|---|
| 109 | */
|
|---|
| 110 | apply(
|
|---|
| 111 | dependency,
|
|---|
| 112 | source,
|
|---|
| 113 | { module, moduleGraph, initFragments, runtimeRequirements, runtime }
|
|---|
| 114 | ) {
|
|---|
| 115 | const dep = /** @type {CommonJsExportsDependency} */ (dependency);
|
|---|
| 116 | const used = moduleGraph
|
|---|
| 117 | .getExportsInfo(module)
|
|---|
| 118 | .getUsedName(dep.names, runtime);
|
|---|
| 119 |
|
|---|
| 120 | const [type, base] = handleDependencyBase(
|
|---|
| 121 | dep.base,
|
|---|
| 122 | module,
|
|---|
| 123 | runtimeRequirements
|
|---|
| 124 | );
|
|---|
| 125 |
|
|---|
| 126 | switch (type) {
|
|---|
| 127 | case "expression":
|
|---|
| 128 | if (!used) {
|
|---|
| 129 | initFragments.push(
|
|---|
| 130 | new InitFragment(
|
|---|
| 131 | "var __webpack_unused_export__;\n",
|
|---|
| 132 | InitFragment.STAGE_CONSTANTS,
|
|---|
| 133 | 0,
|
|---|
| 134 | "__webpack_unused_export__"
|
|---|
| 135 | )
|
|---|
| 136 | );
|
|---|
| 137 | source.replace(
|
|---|
| 138 | dep.range[0],
|
|---|
| 139 | dep.range[1] - 1,
|
|---|
| 140 | "__webpack_unused_export__"
|
|---|
| 141 | );
|
|---|
| 142 | return;
|
|---|
| 143 | }
|
|---|
| 144 | source.replace(
|
|---|
| 145 | dep.range[0],
|
|---|
| 146 | dep.range[1] - 1,
|
|---|
| 147 | `${base}${propertyAccess(used)}`
|
|---|
| 148 | );
|
|---|
| 149 | return;
|
|---|
| 150 | case "Object.defineProperty":
|
|---|
| 151 | if (!used) {
|
|---|
| 152 | initFragments.push(
|
|---|
| 153 | new InitFragment(
|
|---|
| 154 | "var __webpack_unused_export__;\n",
|
|---|
| 155 | InitFragment.STAGE_CONSTANTS,
|
|---|
| 156 | 0,
|
|---|
| 157 | "__webpack_unused_export__"
|
|---|
| 158 | )
|
|---|
| 159 | );
|
|---|
| 160 | source.replace(
|
|---|
| 161 | dep.range[0],
|
|---|
| 162 | /** @type {Range} */ (dep.valueRange)[0] - 1,
|
|---|
| 163 | "__webpack_unused_export__ = ("
|
|---|
| 164 | );
|
|---|
| 165 | source.replace(
|
|---|
| 166 | /** @type {Range} */ (dep.valueRange)[1],
|
|---|
| 167 | dep.range[1] - 1,
|
|---|
| 168 | ")"
|
|---|
| 169 | );
|
|---|
| 170 | return;
|
|---|
| 171 | }
|
|---|
| 172 | source.replace(
|
|---|
| 173 | dep.range[0],
|
|---|
| 174 | /** @type {Range} */ (dep.valueRange)[0] - 1,
|
|---|
| 175 | `Object.defineProperty(${base}${propertyAccess(
|
|---|
| 176 | used.slice(0, -1)
|
|---|
| 177 | )}, ${JSON.stringify(used[used.length - 1])}, (`
|
|---|
| 178 | );
|
|---|
| 179 | source.replace(
|
|---|
| 180 | /** @type {Range} */ (dep.valueRange)[1],
|
|---|
| 181 | dep.range[1] - 1,
|
|---|
| 182 | "))"
|
|---|
| 183 | );
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | };
|
|---|
| 187 |
|
|---|
| 188 | module.exports = CommonJsExportsDependency;
|
|---|