| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Natsu @xiaoxiaojx
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const InitFragment = require("../InitFragment");
|
|---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 10 | const NullDependency = require("./NullDependency");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 13 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 14 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 15 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 16 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 17 | /**
|
|---|
| 18 | * A dependency that adds an init fragment to the module
|
|---|
| 19 | */
|
|---|
| 20 | class ModuleInitFragmentDependency extends NullDependency {
|
|---|
| 21 | /**
|
|---|
| 22 | * Creates an instance of ModuleInitFragmentDependency.
|
|---|
| 23 | * @param {string} initCode the initialization code
|
|---|
| 24 | * @param {string[]} runtimeRequirements runtime requirements
|
|---|
| 25 | * @param {string=} key unique key to avoid emitting the same initialization code twice
|
|---|
| 26 | */
|
|---|
| 27 | constructor(initCode, runtimeRequirements, key) {
|
|---|
| 28 | super();
|
|---|
| 29 | this.initCode = initCode;
|
|---|
| 30 | this.runtimeRequirements = runtimeRequirements;
|
|---|
| 31 | this.key = key;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Serializes this instance into the provided serializer context.
|
|---|
| 36 | * @param {ObjectSerializerContext} context context
|
|---|
| 37 | */
|
|---|
| 38 | serialize(context) {
|
|---|
| 39 | const { write } = context;
|
|---|
| 40 | write(this.initCode);
|
|---|
| 41 | write(this.runtimeRequirements);
|
|---|
| 42 | write(this.key);
|
|---|
| 43 | super.serialize(context);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Restores this instance from the provided deserializer context.
|
|---|
| 48 | * @param {ObjectDeserializerContext} context context
|
|---|
| 49 | */
|
|---|
| 50 | deserialize(context) {
|
|---|
| 51 | const { read } = context;
|
|---|
| 52 | this.initCode = read();
|
|---|
| 53 | this.runtimeRequirements = read();
|
|---|
| 54 | this.key = read();
|
|---|
| 55 | super.deserialize(context);
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | makeSerializable(
|
|---|
| 60 | ModuleInitFragmentDependency,
|
|---|
| 61 | "webpack/lib/dependencies/ModuleInitFragmentDependency"
|
|---|
| 62 | );
|
|---|
| 63 |
|
|---|
| 64 | ModuleInitFragmentDependency.Template = class ModuleInitFragmentDependencyTemplate extends (
|
|---|
| 65 | NullDependency.Template
|
|---|
| 66 | ) {
|
|---|
| 67 | /**
|
|---|
| 68 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 69 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 70 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 71 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 72 | * @returns {void}
|
|---|
| 73 | */
|
|---|
| 74 | apply(dependency, source, { initFragments, runtimeRequirements }) {
|
|---|
| 75 | const dep = /** @type {ModuleInitFragmentDependency} */ (dependency);
|
|---|
| 76 | for (const req of dep.runtimeRequirements) {
|
|---|
| 77 | runtimeRequirements.add(req);
|
|---|
| 78 | }
|
|---|
| 79 | initFragments.push(
|
|---|
| 80 | new InitFragment(
|
|---|
| 81 | dep.initCode,
|
|---|
| 82 | InitFragment.STAGE_CONSTANTS,
|
|---|
| 83 | 0,
|
|---|
| 84 | dep.key,
|
|---|
| 85 | undefined
|
|---|
| 86 | )
|
|---|
| 87 | );
|
|---|
| 88 | }
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 | module.exports = ModuleInitFragmentDependency;
|
|---|