| 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 Template = require("../Template");
|
|---|
| 9 | const { equals } = require("../util/ArrayHelpers");
|
|---|
| 10 | const { getTrimmedIdsAndRange } = require("../util/chainedImports");
|
|---|
| 11 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 12 | const { propertyAccess } = require("../util/property");
|
|---|
| 13 | const {
|
|---|
| 14 | ESM_MODULE_EXPORTS_NAME,
|
|---|
| 15 | getRequireEsmModuleExportsAccess,
|
|---|
| 16 | isRequireEsmModuleExportsModule
|
|---|
| 17 | } = require("./CommonJsDependencyHelpers");
|
|---|
| 18 | const ModuleDependency = require("./ModuleDependency");
|
|---|
| 19 |
|
|---|
| 20 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 21 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 22 | /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
|
|---|
| 23 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 24 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 25 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 26 | /** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
|
|---|
| 27 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 28 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 29 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 30 | /** @typedef {import("../util/chainedImports").IdRanges} IdRanges */
|
|---|
| 31 |
|
|---|
| 32 | class CommonJsFullRequireDependency extends ModuleDependency {
|
|---|
| 33 | /**
|
|---|
| 34 | * Creates an instance of CommonJsFullRequireDependency.
|
|---|
| 35 | * @param {string} request the request string
|
|---|
| 36 | * @param {Range} range location in source code
|
|---|
| 37 | * @param {ExportInfoName[]} names accessed properties on module
|
|---|
| 38 | * @param {IdRanges=} idRanges ranges for members of ids; the two arrays are right-aligned
|
|---|
| 39 | */
|
|---|
| 40 | constructor(
|
|---|
| 41 | request,
|
|---|
| 42 | range,
|
|---|
| 43 | names,
|
|---|
| 44 | idRanges /* TODO webpack 6 make this non-optional. It must always be set to properly trim ids. */
|
|---|
| 45 | ) {
|
|---|
| 46 | super(request);
|
|---|
| 47 | this.range = range;
|
|---|
| 48 | this.names = names;
|
|---|
| 49 | this.idRanges = idRanges;
|
|---|
| 50 | /** @type {boolean} */
|
|---|
| 51 | this.call = false;
|
|---|
| 52 | /** @type {undefined | boolean} */
|
|---|
| 53 | this.asiSafe = undefined;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Returns list of exports referenced by this dependency
|
|---|
| 58 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 59 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|---|
| 60 | * @returns {ReferencedExports} referenced exports
|
|---|
| 61 | */
|
|---|
| 62 | getReferencedExports(moduleGraph, runtime) {
|
|---|
| 63 | const importedModule = moduleGraph.getModule(this);
|
|---|
| 64 | if (
|
|---|
| 65 | importedModule &&
|
|---|
| 66 | isRequireEsmModuleExportsModule(importedModule, moduleGraph)
|
|---|
| 67 | ) {
|
|---|
| 68 | // When `require(esm)` unwraps a `"module.exports"` named export, the
|
|---|
| 69 | // user's property access lands on that value (which webpack does not
|
|---|
| 70 | // model), so only the "module.exports" export itself is referenced.
|
|---|
| 71 | return [[ESM_MODULE_EXPORTS_NAME]];
|
|---|
| 72 | }
|
|---|
| 73 | if (
|
|---|
| 74 | this.call &&
|
|---|
| 75 | (!importedModule ||
|
|---|
| 76 | importedModule.getExportsType(moduleGraph, false) !== "namespace")
|
|---|
| 77 | ) {
|
|---|
| 78 | return [this.names.slice(0, -1)];
|
|---|
| 79 | }
|
|---|
| 80 | return [this.names];
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Serializes this instance into the provided serializer context.
|
|---|
| 85 | * @param {ObjectSerializerContext} context context
|
|---|
| 86 | */
|
|---|
| 87 | serialize(context) {
|
|---|
| 88 | const { write } = context;
|
|---|
| 89 | write(this.names);
|
|---|
| 90 | write(this.idRanges);
|
|---|
| 91 | write(this.call);
|
|---|
| 92 | write(this.asiSafe);
|
|---|
| 93 | super.serialize(context);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * Restores this instance from the provided deserializer context.
|
|---|
| 98 | * @param {ObjectDeserializerContext} context context
|
|---|
| 99 | */
|
|---|
| 100 | deserialize(context) {
|
|---|
| 101 | const { read } = context;
|
|---|
| 102 | this.names = read();
|
|---|
| 103 | this.idRanges = read();
|
|---|
| 104 | this.call = read();
|
|---|
| 105 | this.asiSafe = read();
|
|---|
| 106 | super.deserialize(context);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | get type() {
|
|---|
| 110 | return "cjs full require";
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | get category() {
|
|---|
| 114 | return "commonjs";
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemplate extends (
|
|---|
| 119 | ModuleDependency.Template
|
|---|
| 120 | ) {
|
|---|
| 121 | /**
|
|---|
| 122 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 123 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 124 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 125 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 126 | * @returns {void}
|
|---|
| 127 | */
|
|---|
| 128 | apply(
|
|---|
| 129 | dependency,
|
|---|
| 130 | source,
|
|---|
| 131 | { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements, runtime }
|
|---|
| 132 | ) {
|
|---|
| 133 | const dep = /** @type {CommonJsFullRequireDependency} */ (dependency);
|
|---|
| 134 | if (!dep.range) return;
|
|---|
| 135 | const importedModule = moduleGraph.getModule(dep);
|
|---|
| 136 | let requireExpr = runtimeTemplate.moduleExports({
|
|---|
| 137 | module: importedModule,
|
|---|
| 138 | chunkGraph,
|
|---|
| 139 | request: dep.request,
|
|---|
| 140 | weak: dep.weak,
|
|---|
| 141 | runtimeRequirements
|
|---|
| 142 | });
|
|---|
| 143 |
|
|---|
| 144 | const esmRequireAccess = importedModule
|
|---|
| 145 | ? getRequireEsmModuleExportsAccess(importedModule, moduleGraph, runtime)
|
|---|
| 146 | : null;
|
|---|
| 147 |
|
|---|
| 148 | const {
|
|---|
| 149 | trimmedRange: [trimmedRangeStart, trimmedRangeEnd],
|
|---|
| 150 | trimmedIds
|
|---|
| 151 | } = getTrimmedIdsAndRange(
|
|---|
| 152 | dep.names,
|
|---|
| 153 | dep.range,
|
|---|
| 154 | dep.idRanges,
|
|---|
| 155 | moduleGraph,
|
|---|
| 156 | dep
|
|---|
| 157 | );
|
|---|
| 158 |
|
|---|
| 159 | if (esmRequireAccess !== null) {
|
|---|
| 160 | const access = `${esmRequireAccess}${propertyAccess(trimmedIds)}`;
|
|---|
| 161 | requireExpr =
|
|---|
| 162 | dep.asiSafe === true
|
|---|
| 163 | ? `(${requireExpr}${access})`
|
|---|
| 164 | : `${requireExpr}${access}`;
|
|---|
| 165 | } else if (importedModule) {
|
|---|
| 166 | const usedImported = moduleGraph
|
|---|
| 167 | .getExportsInfo(importedModule)
|
|---|
| 168 | .getUsedName(trimmedIds, runtime);
|
|---|
| 169 | if (usedImported) {
|
|---|
| 170 | const comment = equals(usedImported, trimmedIds)
|
|---|
| 171 | ? ""
|
|---|
| 172 | : `${Template.toNormalComment(propertyAccess(trimmedIds))} `;
|
|---|
| 173 | const access = `${comment}${propertyAccess(usedImported)}`;
|
|---|
| 174 | requireExpr =
|
|---|
| 175 | dep.asiSafe === true
|
|---|
| 176 | ? `(${requireExpr}${access})`
|
|---|
| 177 | : `${requireExpr}${access}`;
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 | source.replace(trimmedRangeStart, trimmedRangeEnd - 1, requireExpr);
|
|---|
| 181 | }
|
|---|
| 182 | };
|
|---|
| 183 |
|
|---|
| 184 | makeSerializable(
|
|---|
| 185 | CommonJsFullRequireDependency,
|
|---|
| 186 | "webpack/lib/dependencies/CommonJsFullRequireDependency"
|
|---|
| 187 | );
|
|---|
| 188 |
|
|---|
| 189 | module.exports = CommonJsFullRequireDependency;
|
|---|