UNPKG

4.63 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 { OriginalSource, RawSource } = require("webpack-sources");
9const Module = require("./Module");
10const makeSerializable = require("./util/makeSerializable");
11
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 */
27
28const TYPES = new Set(["javascript"]);
29
30class RawModule extends Module {
31 /**
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
36 */
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;
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 this.identifierStr;
57 }
58
59 /**
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)
62 */
63 size(type) {
64 return Math.max(1, this.sourceStr.length);
65 }
66
67 /**
68 * @param {RequestShortener} requestShortener the request shortener
69 * @returns {string} a user readable identifier of the module
70 */
71 readableIdentifier(requestShortener) {
72 return requestShortener.shorten(this.readableIdentifierStr);
73 }
74
75 /**
76 * @param {NeedBuildContext} context context info
77 * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
78 * @returns {void}
79 */
80 needBuild(context, callback) {
81 return callback(null, !this.buildMeta);
82 }
83
84 /**
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
90 * @returns {void}
91 */
92 build(options, compilation, resolver, fs, callback) {
93 this.buildMeta = {};
94 this.buildInfo = {
95 cacheable: true
96 };
97 callback();
98 }
99
100 /**
101 * @param {CodeGenerationContext} context context for code generation
102 * @returns {CodeGenerationResult} result
103 */
104 codeGeneration(context) {
105 const sources = new Map();
106 if (this.useSourceMap || this.useSimpleSourceMap) {
107 sources.set(
108 "javascript",
109 new OriginalSource(this.sourceStr, this.identifier())
110 );
111 } else {
112 sources.set("javascript", new RawSource(this.sourceStr));
113 }
114 return { sources, runtimeRequirements: this.runtimeRequirements };
115 }
116
117 /**
118 * @param {Hash} hash the hash used to track dependencies
119 * @param {UpdateHashContext} context context
120 * @returns {void}
121 */
122 updateHash(hash, context) {
123 hash.update(this.sourceStr);
124 super.updateHash(hash, context);
125 }
126
127 serialize(context) {
128 const { write } = context;
129
130 write(this.sourceStr);
131 write(this.identifierStr);
132 write(this.readableIdentifierStr);
133 write(this.runtimeRequirements);
134
135 super.serialize(context);
136 }
137
138 deserialize(context) {
139 const { read } = context;
140
141 this.sourceStr = read();
142 this.identifierStr = read();
143 this.readableIdentifierStr = read();
144 this.runtimeRequirements = read();
145
146 super.deserialize(context);
147 }
148}
149
150makeSerializable(RawModule, "webpack/lib/RawModule");
151
152module.exports = RawModule;