[6a3a178] | 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 makeSerializable = require("../util/makeSerializable");
|
---|
| 11 | const propertyAccess = require("../util/propertyAccess");
|
---|
| 12 | const ModuleDependency = require("./ModuleDependency");
|
---|
| 13 |
|
---|
| 14 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 15 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 16 | /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
---|
| 17 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 18 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
| 19 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
| 20 |
|
---|
| 21 | class CommonJsFullRequireDependency extends ModuleDependency {
|
---|
| 22 | /**
|
---|
| 23 | * @param {string} request the request string
|
---|
| 24 | * @param {[number, number]} range location in source code
|
---|
| 25 | * @param {string[]} names accessed properties on module
|
---|
| 26 | */
|
---|
| 27 | constructor(request, range, names) {
|
---|
| 28 | super(request);
|
---|
| 29 | this.range = range;
|
---|
| 30 | this.names = names;
|
---|
| 31 | this.call = false;
|
---|
| 32 | this.asiSafe = undefined;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * Returns list of exports referenced by this dependency
|
---|
| 37 | * @param {ModuleGraph} moduleGraph module graph
|
---|
| 38 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
---|
| 39 | * @returns {(string[] | ReferencedExport)[]} referenced exports
|
---|
| 40 | */
|
---|
| 41 | getReferencedExports(moduleGraph, runtime) {
|
---|
| 42 | if (this.call) {
|
---|
| 43 | const importedModule = moduleGraph.getModule(this);
|
---|
| 44 | if (
|
---|
| 45 | !importedModule ||
|
---|
| 46 | importedModule.getExportsType(moduleGraph, false) !== "namespace"
|
---|
| 47 | ) {
|
---|
| 48 | return [this.names.slice(0, -1)];
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | return [this.names];
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | serialize(context) {
|
---|
| 55 | const { write } = context;
|
---|
| 56 | write(this.names);
|
---|
| 57 | write(this.call);
|
---|
| 58 | write(this.asiSafe);
|
---|
| 59 | super.serialize(context);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | deserialize(context) {
|
---|
| 63 | const { read } = context;
|
---|
| 64 | this.names = read();
|
---|
| 65 | this.call = read();
|
---|
| 66 | this.asiSafe = read();
|
---|
| 67 | super.deserialize(context);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | get type() {
|
---|
| 71 | return "cjs full require";
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | get category() {
|
---|
| 75 | return "commonjs";
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemplate extends (
|
---|
| 80 | ModuleDependency.Template
|
---|
| 81 | ) {
|
---|
| 82 | /**
|
---|
| 83 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 84 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 85 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 86 | * @returns {void}
|
---|
| 87 | */
|
---|
| 88 | apply(
|
---|
| 89 | dependency,
|
---|
| 90 | source,
|
---|
| 91 | {
|
---|
| 92 | module,
|
---|
| 93 | runtimeTemplate,
|
---|
| 94 | moduleGraph,
|
---|
| 95 | chunkGraph,
|
---|
| 96 | runtimeRequirements,
|
---|
| 97 | runtime,
|
---|
| 98 | initFragments
|
---|
| 99 | }
|
---|
| 100 | ) {
|
---|
| 101 | const dep = /** @type {CommonJsFullRequireDependency} */ (dependency);
|
---|
| 102 | if (!dep.range) return;
|
---|
| 103 | const importedModule = moduleGraph.getModule(dep);
|
---|
| 104 | let requireExpr = runtimeTemplate.moduleExports({
|
---|
| 105 | module: importedModule,
|
---|
| 106 | chunkGraph,
|
---|
| 107 | request: dep.request,
|
---|
| 108 | weak: dep.weak,
|
---|
| 109 | runtimeRequirements
|
---|
| 110 | });
|
---|
| 111 | const ids = dep.names;
|
---|
| 112 | const usedImported = moduleGraph
|
---|
| 113 | .getExportsInfo(importedModule)
|
---|
| 114 | .getUsedName(ids, runtime);
|
---|
| 115 | if (usedImported) {
|
---|
| 116 | const comment = equals(usedImported, ids)
|
---|
| 117 | ? ""
|
---|
| 118 | : Template.toNormalComment(propertyAccess(ids)) + " ";
|
---|
| 119 | requireExpr += `${comment}${propertyAccess(usedImported)}`;
|
---|
| 120 | }
|
---|
| 121 | source.replace(dep.range[0], dep.range[1] - 1, requireExpr);
|
---|
| 122 | }
|
---|
| 123 | };
|
---|
| 124 |
|
---|
| 125 | makeSerializable(
|
---|
| 126 | CommonJsFullRequireDependency,
|
---|
| 127 | "webpack/lib/dependencies/CommonJsFullRequireDependency"
|
---|
| 128 | );
|
---|
| 129 |
|
---|
| 130 | module.exports = CommonJsFullRequireDependency;
|
---|