| 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 ModuleDependency = require("./ModuleDependency");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("../ContextModule")} ContextModule */
|
|---|
| 13 | /** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
|
|---|
| 14 | /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
|
|---|
| 15 | /** @typedef {import("../Module")} Module */
|
|---|
| 16 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 17 | /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
|
|---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 20 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 21 |
|
|---|
| 22 | class ContextElementDependency extends ModuleDependency {
|
|---|
| 23 | /**
|
|---|
| 24 | * Creates an instance of ContextElementDependency.
|
|---|
| 25 | * @param {string} request request
|
|---|
| 26 | * @param {string | undefined} userRequest user request
|
|---|
| 27 | * @param {string | undefined} typePrefix type prefix
|
|---|
| 28 | * @param {string} category category
|
|---|
| 29 | * @param {RawReferencedExports | null=} referencedExports referenced exports
|
|---|
| 30 | * @param {string=} context context
|
|---|
| 31 | * @param {ImportAttributes=} attributes import assertions
|
|---|
| 32 | */
|
|---|
| 33 | constructor(
|
|---|
| 34 | request,
|
|---|
| 35 | userRequest,
|
|---|
| 36 | typePrefix,
|
|---|
| 37 | category,
|
|---|
| 38 | referencedExports,
|
|---|
| 39 | context,
|
|---|
| 40 | attributes
|
|---|
| 41 | ) {
|
|---|
| 42 | super(request);
|
|---|
| 43 |
|
|---|
| 44 | if (userRequest) {
|
|---|
| 45 | this.userRequest = userRequest;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | this._typePrefix = typePrefix;
|
|---|
| 49 | this._category = category;
|
|---|
| 50 | this.referencedExports = referencedExports;
|
|---|
| 51 | this._context = context || undefined;
|
|---|
| 52 | this.attributes = attributes;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | get type() {
|
|---|
| 56 | if (this._typePrefix) {
|
|---|
| 57 | return `${this._typePrefix} context element`;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | return "context element";
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | get category() {
|
|---|
| 64 | return this._category;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Returns an identifier to merge equal requests.
|
|---|
| 69 | * @returns {string | null} an identifier to merge equal requests
|
|---|
| 70 | */
|
|---|
| 71 | getResourceIdentifier() {
|
|---|
| 72 | let str = super.getResourceIdentifier();
|
|---|
| 73 | if (this.attributes) {
|
|---|
| 74 | str += `|attributes${JSON.stringify(this.attributes)}`;
|
|---|
| 75 | }
|
|---|
| 76 | return str;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Returns list of exports referenced by this dependency
|
|---|
| 81 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 82 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|---|
| 83 | * @returns {ReferencedExports} referenced exports
|
|---|
| 84 | */
|
|---|
| 85 | getReferencedExports(moduleGraph, runtime) {
|
|---|
| 86 | if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
|
|---|
| 87 | /** @type {ReferencedExports} */
|
|---|
| 88 | const refs = [];
|
|---|
| 89 | for (const referencedExport of this.referencedExports) {
|
|---|
| 90 | if (
|
|---|
| 91 | this._typePrefix === "import()" &&
|
|---|
| 92 | referencedExport[0] === "default"
|
|---|
| 93 | ) {
|
|---|
| 94 | const selfModule =
|
|---|
| 95 | /** @type {ContextModule} */
|
|---|
| 96 | (moduleGraph.getParentModule(this));
|
|---|
| 97 | const importedModule =
|
|---|
| 98 | /** @type {Module} */
|
|---|
| 99 | (moduleGraph.getModule(this));
|
|---|
| 100 | const exportsType = importedModule.getExportsType(
|
|---|
| 101 | moduleGraph,
|
|---|
| 102 | selfModule.options.namespaceObject === "strict"
|
|---|
| 103 | );
|
|---|
| 104 | if (
|
|---|
| 105 | exportsType === "default-only" ||
|
|---|
| 106 | exportsType === "default-with-named"
|
|---|
| 107 | ) {
|
|---|
| 108 | return Dependency.EXPORTS_OBJECT_REFERENCED;
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | refs.push({
|
|---|
| 112 | name: referencedExport,
|
|---|
| 113 | canMangle: false
|
|---|
| 114 | });
|
|---|
| 115 | }
|
|---|
| 116 | return refs;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * Serializes this instance into the provided serializer context.
|
|---|
| 121 | * @param {ObjectSerializerContext} context context
|
|---|
| 122 | */
|
|---|
| 123 | serialize(context) {
|
|---|
| 124 | const { write } = context;
|
|---|
| 125 | write(this._typePrefix);
|
|---|
| 126 | write(this._category);
|
|---|
| 127 | write(this.referencedExports);
|
|---|
| 128 | write(this.attributes);
|
|---|
| 129 | super.serialize(context);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /**
|
|---|
| 133 | * Restores this instance from the provided deserializer context.
|
|---|
| 134 | * @param {ObjectDeserializerContext} context context
|
|---|
| 135 | */
|
|---|
| 136 | deserialize(context) {
|
|---|
| 137 | const { read } = context;
|
|---|
| 138 | this._typePrefix = read();
|
|---|
| 139 | this._category = read();
|
|---|
| 140 | this.referencedExports = read();
|
|---|
| 141 | this.attributes = read();
|
|---|
| 142 | super.deserialize(context);
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | makeSerializable(
|
|---|
| 147 | ContextElementDependency,
|
|---|
| 148 | "webpack/lib/dependencies/ContextElementDependency"
|
|---|
| 149 | );
|
|---|
| 150 |
|
|---|
| 151 | module.exports = ContextElementDependency;
|
|---|