| 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 { RawSource } = require("webpack-sources");
|
|---|
| 9 | const Module = require("../Module");
|
|---|
| 10 | const {
|
|---|
| 11 | JAVASCRIPT_TYPE,
|
|---|
| 12 | JAVASCRIPT_TYPES
|
|---|
| 13 | } = require("../ModuleSourceTypeConstants");
|
|---|
| 14 | const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("../ModuleTypeConstants");
|
|---|
| 15 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 16 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 17 |
|
|---|
| 18 | /** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
|
|---|
| 19 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 20 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 21 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 22 | /** @typedef {import("../Generator").SourceTypes} SourceTypes */
|
|---|
| 23 | /** @typedef {import("../Module").BuildCallback} BuildCallback */
|
|---|
| 24 | /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
|
|---|
| 25 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
|---|
| 26 | /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
|
|---|
| 27 | /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
|
|---|
| 28 | /** @typedef {import("../Module").Sources} Sources */
|
|---|
| 29 | /** @typedef {import("../RequestShortener")} RequestShortener */
|
|---|
| 30 | /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
|---|
| 31 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 32 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 33 | /** @typedef {import("../util/Hash")} Hash */
|
|---|
| 34 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 35 |
|
|---|
| 36 | const RUNTIME_REQUIREMENTS = new Set([
|
|---|
| 37 | RuntimeGlobals.require,
|
|---|
| 38 | RuntimeGlobals.module
|
|---|
| 39 | ]);
|
|---|
| 40 |
|
|---|
| 41 | class DllModule extends Module {
|
|---|
| 42 | /**
|
|---|
| 43 | * Creates an instance of DllModule.
|
|---|
| 44 | * @param {string} context context path
|
|---|
| 45 | * @param {Dependency[]} dependencies dependencies
|
|---|
| 46 | * @param {string} name name
|
|---|
| 47 | */
|
|---|
| 48 | constructor(context, dependencies, name) {
|
|---|
| 49 | super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, context);
|
|---|
| 50 |
|
|---|
| 51 | // Info from Factory
|
|---|
| 52 | /** @type {Dependency[]} */
|
|---|
| 53 | this.dependencies = dependencies;
|
|---|
| 54 | this.name = name;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Returns the source types this module can generate.
|
|---|
| 59 | * @returns {SourceTypes} types available (do not mutate)
|
|---|
| 60 | */
|
|---|
| 61 | getSourceTypes() {
|
|---|
| 62 | return JAVASCRIPT_TYPES;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Returns the unique identifier used to reference this module.
|
|---|
| 67 | * @returns {string} a unique identifier of the module
|
|---|
| 68 | */
|
|---|
| 69 | identifier() {
|
|---|
| 70 | return `dll ${this.name}`;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Returns a human-readable identifier for this module.
|
|---|
| 75 | * @param {RequestShortener} requestShortener the request shortener
|
|---|
| 76 | * @returns {string} a user readable identifier of the module
|
|---|
| 77 | */
|
|---|
| 78 | readableIdentifier(requestShortener) {
|
|---|
| 79 | return `dll ${this.name}`;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Builds the module using the provided compilation context.
|
|---|
| 84 | * @param {WebpackOptions} options webpack options
|
|---|
| 85 | * @param {Compilation} compilation the compilation
|
|---|
| 86 | * @param {ResolverWithOptions} resolver the resolver
|
|---|
| 87 | * @param {InputFileSystem} fs the file system
|
|---|
| 88 | * @param {BuildCallback} callback callback function
|
|---|
| 89 | * @returns {void}
|
|---|
| 90 | */
|
|---|
| 91 | build(options, compilation, resolver, fs, callback) {
|
|---|
| 92 | this.buildMeta = {};
|
|---|
| 93 | this.buildInfo = {};
|
|---|
| 94 | return callback();
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Generates code and runtime requirements for this module.
|
|---|
| 99 | * @param {CodeGenerationContext} context context for code generation
|
|---|
| 100 | * @returns {CodeGenerationResult} result
|
|---|
| 101 | */
|
|---|
| 102 | codeGeneration(context) {
|
|---|
| 103 | /** @type {Sources} */
|
|---|
| 104 | const sources = new Map();
|
|---|
| 105 | sources.set(
|
|---|
| 106 | JAVASCRIPT_TYPE,
|
|---|
| 107 | new RawSource(`module.exports = ${RuntimeGlobals.require};`)
|
|---|
| 108 | );
|
|---|
| 109 | return {
|
|---|
| 110 | sources,
|
|---|
| 111 | runtimeRequirements: RUNTIME_REQUIREMENTS
|
|---|
| 112 | };
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Checks whether the module needs to be rebuilt for the current build state.
|
|---|
| 117 | * @param {NeedBuildContext} context context info
|
|---|
| 118 | * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
|
|---|
| 119 | * @returns {void}
|
|---|
| 120 | */
|
|---|
| 121 | needBuild(context, callback) {
|
|---|
| 122 | return callback(null, !this.buildMeta);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Returns the estimated size for the requested source type.
|
|---|
| 127 | * @param {string=} type the source type for which the size should be estimated
|
|---|
| 128 | * @returns {number} the estimated size of the module (must be non-zero)
|
|---|
| 129 | */
|
|---|
| 130 | size(type) {
|
|---|
| 131 | return 12;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Updates the hash with the data contributed by this instance.
|
|---|
| 136 | * @param {Hash} hash the hash used to track dependencies
|
|---|
| 137 | * @param {UpdateHashContext} context context
|
|---|
| 138 | * @returns {void}
|
|---|
| 139 | */
|
|---|
| 140 | updateHash(hash, context) {
|
|---|
| 141 | hash.update(`dll module${this.name || ""}`);
|
|---|
| 142 | super.updateHash(hash, context);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /**
|
|---|
| 146 | * Serializes this instance into the provided serializer context.
|
|---|
| 147 | * @param {ObjectSerializerContext} context context
|
|---|
| 148 | */
|
|---|
| 149 | serialize(context) {
|
|---|
| 150 | context.write(this.name);
|
|---|
| 151 | super.serialize(context);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * Restores this instance from the provided deserializer context.
|
|---|
| 156 | * @param {ObjectDeserializerContext} context context
|
|---|
| 157 | */
|
|---|
| 158 | deserialize(context) {
|
|---|
| 159 | this.name = context.read();
|
|---|
| 160 | super.deserialize(context);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * Assuming this module is in the cache. Update the (cached) module with
|
|---|
| 165 | * the fresh module from the factory. Usually updates internal references
|
|---|
| 166 | * and properties.
|
|---|
| 167 | * @param {Module} module fresh module
|
|---|
| 168 | * @returns {void}
|
|---|
| 169 | */
|
|---|
| 170 | updateCacheModule(module) {
|
|---|
| 171 | super.updateCacheModule(module);
|
|---|
| 172 | this.dependencies = module.dependencies;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
|
|---|
| 177 | */
|
|---|
| 178 | cleanupForCache() {
|
|---|
| 179 | super.cleanupForCache();
|
|---|
| 180 | this.dependencies = /** @type {EXPECTED_ANY} */ (undefined);
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | makeSerializable(DllModule, "webpack/lib/dll/DllModule");
|
|---|
| 185 |
|
|---|
| 186 | module.exports = DllModule;
|
|---|