2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
8 const { OriginalSource, RawSource } = require("webpack-sources");
9 const Module = require("./Module");
10 const makeSerializable = require("./util/makeSerializable");
12 /** @typedef {import("webpack-sources").Source} Source */
13 /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
14 /** @typedef {import("./ChunkGraph")} ChunkGraph */
15 /** @typedef {import("./Compilation")} Compilation */
16 /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
17 /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
18 /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
19 /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
20 /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
21 /** @typedef {import("./RequestShortener")} RequestShortener */
22 /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
23 /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
24 /** @typedef {import("./WebpackError")} WebpackError */
25 /** @typedef {import("./util/Hash")} Hash */
26 /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
28 const TYPES = new Set(["javascript"]);
30 class RawModule extends Module {
32 * @param {string} source source code
33 * @param {string} identifier unique identifier
34 * @param {string=} readableIdentifier readable identifier
35 * @param {ReadonlySet<string>=} runtimeRequirements runtime requirements needed for the source code
37 constructor(source, identifier, readableIdentifier, runtimeRequirements) {
38 super("javascript/dynamic", null);
39 this.sourceStr = source;
40 this.identifierStr = identifier || this.sourceStr;
41 this.readableIdentifierStr = readableIdentifier || this.identifierStr;
42 this.runtimeRequirements = runtimeRequirements || null;
46 * @returns {Set<string>} types available (do not mutate)
53 * @returns {string} a unique identifier of the module
56 return this.identifierStr;
60 * @param {string=} type the source type for which the size should be estimated
61 * @returns {number} the estimated size of the module (must be non-zero)
64 return Math.max(1, this.sourceStr.length);
68 * @param {RequestShortener} requestShortener the request shortener
69 * @returns {string} a user readable identifier of the module
71 readableIdentifier(requestShortener) {
72 return requestShortener.shorten(this.readableIdentifierStr);
76 * @param {NeedBuildContext} context context info
77 * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
80 needBuild(context, callback) {
81 return callback(null, !this.buildMeta);
85 * @param {WebpackOptions} options webpack options
86 * @param {Compilation} compilation the compilation
87 * @param {ResolverWithOptions} resolver the resolver
88 * @param {InputFileSystem} fs the file system
89 * @param {function(WebpackError=): void} callback callback function
92 build(options, compilation, resolver, fs, callback) {
101 * @param {CodeGenerationContext} context context for code generation
102 * @returns {CodeGenerationResult} result
104 codeGeneration(context) {
105 const sources = new Map();
106 if (this.useSourceMap || this.useSimpleSourceMap) {
109 new OriginalSource(this.sourceStr, this.identifier())
112 sources.set("javascript", new RawSource(this.sourceStr));
114 return { sources, runtimeRequirements: this.runtimeRequirements };
118 * @param {Hash} hash the hash used to track dependencies
119 * @param {UpdateHashContext} context context
122 updateHash(hash, context) {
123 hash.update(this.sourceStr);
124 super.updateHash(hash, context);
128 const { write } = context;
130 write(this.sourceStr);
131 write(this.identifierStr);
132 write(this.readableIdentifierStr);
133 write(this.runtimeRequirements);
135 super.serialize(context);
138 deserialize(context) {
139 const { read } = context;
141 this.sourceStr = read();
142 this.identifierStr = read();
143 this.readableIdentifierStr = read();
144 this.runtimeRequirements = read();
146 super.deserialize(context);
150 makeSerializable(RawModule, "webpack/lib/RawModule");
152 module.exports = RawModule;