| 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 ContextDependency = require("./ContextDependency");
|
|---|
| 11 | const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 14 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 15 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 16 | /** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
|
|---|
| 17 | /** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
|
|---|
| 18 | /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
|
|---|
| 19 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 20 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 21 |
|
|---|
| 22 | class CommonJsRequireContextDependency extends ContextDependency {
|
|---|
| 23 | /**
|
|---|
| 24 | * Creates an instance of CommonJsRequireContextDependency.
|
|---|
| 25 | * @param {ContextDependencyOptions} options options for the context module
|
|---|
| 26 | * @param {Range} range location in source code
|
|---|
| 27 | * @param {Range=} valueRange location of the require call
|
|---|
| 28 | * @param {boolean | string=} inShorthand true or name
|
|---|
| 29 | * @param {string=} context context
|
|---|
| 30 | */
|
|---|
| 31 | constructor(options, range, valueRange, inShorthand, context) {
|
|---|
| 32 | super(options, context);
|
|---|
| 33 |
|
|---|
| 34 | this.range = range;
|
|---|
| 35 | this.valueRange = valueRange;
|
|---|
| 36 | // inShorthand must be serialized by subclasses that use it
|
|---|
| 37 | this.inShorthand = inShorthand;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | get type() {
|
|---|
| 41 | return "cjs require context";
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Returns list of exports referenced by this dependency
|
|---|
| 46 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 47 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|---|
| 48 | * @returns {ReferencedExports} referenced exports
|
|---|
| 49 | */
|
|---|
| 50 | getReferencedExports(moduleGraph, runtime) {
|
|---|
| 51 | if (!this.options.referencedExports) {
|
|---|
| 52 | return Dependency.EXPORTS_OBJECT_REFERENCED;
|
|---|
| 53 | }
|
|---|
| 54 | return this.options.referencedExports.map((name) => ({
|
|---|
| 55 | name,
|
|---|
| 56 | canMangle: false
|
|---|
| 57 | }));
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Serializes this instance into the provided serializer context.
|
|---|
| 62 | * @param {ObjectSerializerContext} context context
|
|---|
| 63 | */
|
|---|
| 64 | serialize(context) {
|
|---|
| 65 | const { write } = context;
|
|---|
| 66 |
|
|---|
| 67 | write(this.range);
|
|---|
| 68 | write(this.valueRange);
|
|---|
| 69 | write(this.inShorthand);
|
|---|
| 70 |
|
|---|
| 71 | super.serialize(context);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Restores this instance from the provided deserializer context.
|
|---|
| 76 | * @param {ObjectDeserializerContext} context context
|
|---|
| 77 | */
|
|---|
| 78 | deserialize(context) {
|
|---|
| 79 | const { read } = context;
|
|---|
| 80 |
|
|---|
| 81 | this.range = read();
|
|---|
| 82 | this.valueRange = read();
|
|---|
| 83 | this.inShorthand = read();
|
|---|
| 84 |
|
|---|
| 85 | super.deserialize(context);
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | makeSerializable(
|
|---|
| 90 | CommonJsRequireContextDependency,
|
|---|
| 91 | "webpack/lib/dependencies/CommonJsRequireContextDependency"
|
|---|
| 92 | );
|
|---|
| 93 |
|
|---|
| 94 | CommonJsRequireContextDependency.Template =
|
|---|
| 95 | ContextDependencyTemplateAsRequireCall;
|
|---|
| 96 |
|
|---|
| 97 | module.exports = CommonJsRequireContextDependency;
|
|---|