| 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 makeSerializable = require("../util/makeSerializable");
|
|---|
| 9 | const NullDependency = require("./NullDependency");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 12 | /** @typedef {import("./NullDependency").RawRuntimeRequirements} RawRuntimeRequirements */
|
|---|
| 13 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 14 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 15 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 16 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 17 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 18 | /** @typedef {import("../util/Hash")} Hash */
|
|---|
| 19 |
|
|---|
| 20 | class RuntimeRequirementsDependency extends NullDependency {
|
|---|
| 21 | /**
|
|---|
| 22 | * Creates an instance of RuntimeRequirementsDependency.
|
|---|
| 23 | * @param {RawRuntimeRequirements} runtimeRequirements runtime requirements
|
|---|
| 24 | */
|
|---|
| 25 | constructor(runtimeRequirements) {
|
|---|
| 26 | super();
|
|---|
| 27 | this.runtimeRequirements = new Set(runtimeRequirements);
|
|---|
| 28 | /** @type {undefined | string} */
|
|---|
| 29 | this._hashUpdate = undefined;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Updates the hash with the data contributed by this instance.
|
|---|
| 34 | * @param {Hash} hash hash to be updated
|
|---|
| 35 | * @param {UpdateHashContext} context context
|
|---|
| 36 | * @returns {void}
|
|---|
| 37 | */
|
|---|
| 38 | updateHash(hash, context) {
|
|---|
| 39 | if (this._hashUpdate === undefined) {
|
|---|
| 40 | this._hashUpdate = `${[...this.runtimeRequirements].join()}`;
|
|---|
| 41 | }
|
|---|
| 42 | hash.update(this._hashUpdate);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Serializes this instance into the provided serializer context.
|
|---|
| 47 | * @param {ObjectSerializerContext} context context
|
|---|
| 48 | */
|
|---|
| 49 | serialize(context) {
|
|---|
| 50 | const { write } = context;
|
|---|
| 51 | write(this.runtimeRequirements);
|
|---|
| 52 | super.serialize(context);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Restores this instance from the provided deserializer context.
|
|---|
| 57 | * @param {ObjectDeserializerContext} context context
|
|---|
| 58 | */
|
|---|
| 59 | deserialize(context) {
|
|---|
| 60 | const { read } = context;
|
|---|
| 61 | this.runtimeRequirements = read();
|
|---|
| 62 | super.deserialize(context);
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | makeSerializable(
|
|---|
| 67 | RuntimeRequirementsDependency,
|
|---|
| 68 | "webpack/lib/dependencies/RuntimeRequirementsDependency"
|
|---|
| 69 | );
|
|---|
| 70 |
|
|---|
| 71 | RuntimeRequirementsDependency.Template = class RuntimeRequirementsDependencyTemplate extends (
|
|---|
| 72 | NullDependency.Template
|
|---|
| 73 | ) {
|
|---|
| 74 | /**
|
|---|
| 75 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 76 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 77 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 78 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 79 | * @returns {void}
|
|---|
| 80 | */
|
|---|
| 81 | apply(dependency, source, { runtimeRequirements }) {
|
|---|
| 82 | const dep = /** @type {RuntimeRequirementsDependency} */ (dependency);
|
|---|
| 83 | for (const req of dep.runtimeRequirements) {
|
|---|
| 84 | runtimeRequirements.add(req);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | };
|
|---|
| 88 |
|
|---|
| 89 | module.exports = RuntimeRequirementsDependency;
|
|---|