| [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 Dependency = require("../Dependency");
|
|---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 10 | const {
|
|---|
| 11 | ESM_MODULE_EXPORTS_NAME,
|
|---|
| 12 | getRequireEsmModuleExportsAccess,
|
|---|
| 13 | isRequireEsmModuleExportsModule
|
|---|
| 14 | } = require("./CommonJsDependencyHelpers");
|
|---|
| 15 | const ModuleDependency = require("./ModuleDependency");
|
|---|
| 16 |
|
|---|
| 17 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 18 | /** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
|
|---|
| 19 | /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
|
|---|
| 20 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 21 | /** @typedef {import("../Module")} Module */
|
|---|
| 22 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 23 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 24 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 25 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 26 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 27 |
|
|---|
| 28 | class CommonJsRequireDependency extends ModuleDependency {
|
|---|
| 29 | /**
|
|---|
| 30 | * Creates an instance of CommonJsRequireDependency.
|
|---|
| 31 | * @param {string} request request
|
|---|
| 32 | * @param {Range=} range location in source code of the string-literal argument (gets replaced by the module id)
|
|---|
| 33 | * @param {string=} context request context
|
|---|
| 34 | * @param {RawReferencedExports | null=} referencedExports list of referenced exports
|
|---|
| 35 | * @param {Range=} valueRange location in source code of the whole `require(...)` call (for `require(esm)` interop)
|
|---|
| 36 | */
|
|---|
| 37 | constructor(
|
|---|
| 38 | request,
|
|---|
| 39 | range,
|
|---|
| 40 | context,
|
|---|
| 41 | referencedExports = null,
|
|---|
| 42 | valueRange = undefined
|
|---|
| 43 | ) {
|
|---|
| 44 | super(request);
|
|---|
| 45 | this.range = range;
|
|---|
| 46 | this._context = context;
|
|---|
| 47 | this.referencedExports = referencedExports;
|
|---|
| 48 | this.valueRange = valueRange;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | get type() {
|
|---|
| 52 | return "cjs require";
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | get category() {
|
|---|
| 56 | return "commonjs";
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Returns list of exports referenced by this dependency
|
|---|
| 61 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 62 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|---|
| 63 | * @returns {ReferencedExports} referenced exports
|
|---|
| 64 | */
|
|---|
| 65 | getReferencedExports(moduleGraph, runtime) {
|
|---|
| 66 | const importedModule = moduleGraph.getModule(this);
|
|---|
| 67 | if (
|
|---|
| 68 | importedModule &&
|
|---|
| 69 | isRequireEsmModuleExportsModule(importedModule, moduleGraph)
|
|---|
| 70 | ) {
|
|---|
| 71 | // `require(esm)` will unwrap the "module.exports" named export; only
|
|---|
| 72 | // that export is observable through this `require()` call.
|
|---|
| 73 | return [[ESM_MODULE_EXPORTS_NAME]];
|
|---|
| 74 | }
|
|---|
| 75 | if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
|
|---|
| 76 | return this.referencedExports.map((name) => ({
|
|---|
| 77 | name,
|
|---|
| 78 | canMangle: false
|
|---|
| 79 | }));
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Serializes this instance into the provided serializer context.
|
|---|
| 84 | * @param {ObjectSerializerContext} context context
|
|---|
| 85 | */
|
|---|
| 86 | serialize(context) {
|
|---|
| 87 | const { write } = context;
|
|---|
| 88 | write(this.referencedExports);
|
|---|
| 89 | write(this.valueRange);
|
|---|
| 90 | super.serialize(context);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Restores this instance from the provided deserializer context.
|
|---|
| 95 | * @param {ObjectDeserializerContext} context context
|
|---|
| 96 | */
|
|---|
| 97 | deserialize(context) {
|
|---|
| 98 | const { read } = context;
|
|---|
| 99 | this.referencedExports = read();
|
|---|
| 100 | this.valueRange = read();
|
|---|
| 101 | super.deserialize(context);
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | CommonJsRequireDependency.Template = class CommonJsRequireDependencyTemplate extends (
|
|---|
| 106 | ModuleDependency.Template
|
|---|
| 107 | ) {
|
|---|
| 108 | /**
|
|---|
| 109 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 110 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 111 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 112 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 113 | * @returns {void}
|
|---|
| 114 | */
|
|---|
| 115 | apply(
|
|---|
| 116 | dependency,
|
|---|
| 117 | source,
|
|---|
| 118 | { runtimeTemplate, moduleGraph, chunkGraph, runtime }
|
|---|
| 119 | ) {
|
|---|
| 120 | const dep = /** @type {CommonJsRequireDependency} */ (dependency);
|
|---|
| 121 | if (!dep.range) return;
|
|---|
| 122 | const importedModule = /** @type {Module} */ (moduleGraph.getModule(dep));
|
|---|
| 123 | const content = runtimeTemplate.moduleId({
|
|---|
| 124 | module: importedModule,
|
|---|
| 125 | chunkGraph,
|
|---|
| 126 | request: dep.request,
|
|---|
| 127 | weak: dep.weak
|
|---|
| 128 | });
|
|---|
| 129 | source.replace(dep.range[0], dep.range[1] - 1, content);
|
|---|
| 130 |
|
|---|
| 131 | if (dep.valueRange && importedModule) {
|
|---|
| 132 | const access = getRequireEsmModuleExportsAccess(
|
|---|
| 133 | importedModule,
|
|---|
| 134 | moduleGraph,
|
|---|
| 135 | runtime
|
|---|
| 136 | );
|
|---|
| 137 | if (access !== null) {
|
|---|
| 138 | source.insert(dep.valueRange[1], access);
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | };
|
|---|
| 143 |
|
|---|
| 144 | makeSerializable(
|
|---|
| 145 | CommonJsRequireDependency,
|
|---|
| 146 | "webpack/lib/dependencies/CommonJsRequireDependency"
|
|---|
| 147 | );
|
|---|
| 148 |
|
|---|
| 149 | module.exports = CommonJsRequireDependency;
|
|---|