UNPKG

4.31 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { RawSource } = require("webpack-sources");
9const Module = require("./Module");
10const RuntimeGlobals = require("./RuntimeGlobals");
11const makeSerializable = require("./util/makeSerializable");
12
13/** @typedef {import("webpack-sources").Source} Source */
14/** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
15/** @typedef {import("./ChunkGraph")} ChunkGraph */
16/** @typedef {import("./Compilation")} Compilation */
17/** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
18/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
19/** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
20/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
21/** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
22/** @typedef {import("./Module").SourceContext} SourceContext */
23/** @typedef {import("./RequestShortener")} RequestShortener */
24/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
25/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
26/** @typedef {import("./WebpackError")} WebpackError */
27/** @typedef {import("./util/Hash")} Hash */
28/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
29
30const TYPES = new Set(["javascript"]);
31const RUNTIME_REQUIREMENTS = new Set([
32 RuntimeGlobals.require,
33 RuntimeGlobals.module
34]);
35
36class DllModule extends Module {
37 constructor(context, dependencies, name) {
38 super("javascript/dynamic", context);
39
40 // Info from Factory
41 this.dependencies = dependencies;
42 this.name = name;
43 }
44
45 /**
46 * @returns {Set<string>} types available (do not mutate)
47 */
48 getSourceTypes() {
49 return TYPES;
50 }
51
52 /**
53 * @returns {string} a unique identifier of the module
54 */
55 identifier() {
56 return `dll ${this.name}`;
57 }
58
59 /**
60 * @param {RequestShortener} requestShortener the request shortener
61 * @returns {string} a user readable identifier of the module
62 */
63 readableIdentifier(requestShortener) {
64 return `dll ${this.name}`;
65 }
66
67 /**
68 * @param {WebpackOptions} options webpack options
69 * @param {Compilation} compilation the compilation
70 * @param {ResolverWithOptions} resolver the resolver
71 * @param {InputFileSystem} fs the file system
72 * @param {function(WebpackError=): void} callback callback function
73 * @returns {void}
74 */
75 build(options, compilation, resolver, fs, callback) {
76 this.buildMeta = {};
77 this.buildInfo = {};
78 return callback();
79 }
80
81 /**
82 * @param {CodeGenerationContext} context context for code generation
83 * @returns {CodeGenerationResult} result
84 */
85 codeGeneration(context) {
86 const sources = new Map();
87 sources.set(
88 "javascript",
89 new RawSource("module.exports = __webpack_require__;")
90 );
91 return {
92 sources,
93 runtimeRequirements: RUNTIME_REQUIREMENTS
94 };
95 }
96
97 /**
98 * @param {NeedBuildContext} context context info
99 * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
100 * @returns {void}
101 */
102 needBuild(context, callback) {
103 return callback(null, !this.buildMeta);
104 }
105
106 /**
107 * @param {string=} type the source type for which the size should be estimated
108 * @returns {number} the estimated size of the module (must be non-zero)
109 */
110 size(type) {
111 return 12;
112 }
113
114 /**
115 * @param {Hash} hash the hash used to track dependencies
116 * @param {UpdateHashContext} context context
117 * @returns {void}
118 */
119 updateHash(hash, context) {
120 hash.update("dll module");
121 hash.update(this.name || "");
122 super.updateHash(hash, context);
123 }
124
125 serialize(context) {
126 context.write(this.name);
127 super.serialize(context);
128 }
129
130 deserialize(context) {
131 this.name = context.read();
132 super.deserialize(context);
133 }
134
135 /**
136 * Assuming this module is in the cache. Update the (cached) module with
137 * the fresh module from the factory. Usually updates internal references
138 * and properties.
139 * @param {Module} module fresh module
140 * @returns {void}
141 */
142 updateCacheModule(module) {
143 super.updateCacheModule(module);
144 this.dependencies = module.dependencies;
145 }
146}
147
148makeSerializable(DllModule, "webpack/lib/DllModule");
149
150module.exports = DllModule;