UNPKG

4.25 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 constructor(source, identifier, readableIdentifier) {
32 super("javascript/dynamic", null);
33 /** @type {string} */
34 this.sourceStr = source;
35 /** @type {string} */
36 this.identifierStr = identifier || this.sourceStr;
37 /** @type {string} */
38 this.readableIdentifierStr = readableIdentifier || this.identifierStr;
39 }
40
41 /**
42 * @returns {Set<string>} types available (do not mutate)
43 */
44 getSourceTypes() {
45 return TYPES;
46 }
47
48 /**
49 * @returns {string} a unique identifier of the module
50 */
51 identifier() {
52 return this.identifierStr;
53 }
54
55 /**
56 * @param {string=} type the source type for which the size should be estimated
57 * @returns {number} the estimated size of the module (must be non-zero)
58 */
59 size(type) {
60 return Math.max(1, this.sourceStr.length);
61 }
62
63 /**
64 * @param {RequestShortener} requestShortener the request shortener
65 * @returns {string} a user readable identifier of the module
66 */
67 readableIdentifier(requestShortener) {
68 return requestShortener.shorten(this.readableIdentifierStr);
69 }
70
71 /**
72 * @param {NeedBuildContext} context context info
73 * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
74 * @returns {void}
75 */
76 needBuild(context, callback) {
77 return callback(null, !this.buildMeta);
78 }
79
80 /**
81 * @param {WebpackOptions} options webpack options
82 * @param {Compilation} compilation the compilation
83 * @param {ResolverWithOptions} resolver the resolver
84 * @param {InputFileSystem} fs the file system
85 * @param {function(WebpackError=): void} callback callback function
86 * @returns {void}
87 */
88 build(options, compilation, resolver, fs, callback) {
89 this.buildMeta = {};
90 this.buildInfo = {
91 cacheable: true
92 };
93 callback();
94 }
95
96 /**
97 * @param {CodeGenerationContext} context context for code generation
98 * @returns {CodeGenerationResult} result
99 */
100 codeGeneration(context) {
101 const sources = new Map();
102 if (this.useSourceMap) {
103 sources.set(
104 "javascript",
105 new OriginalSource(this.sourceStr, this.identifier())
106 );
107 } else {
108 sources.set("javascript", new RawSource(this.sourceStr));
109 }
110 return { sources, runtimeRequirements: null };
111 }
112
113 /**
114 * @param {Hash} hash the hash used to track dependencies
115 * @param {UpdateHashContext} context context
116 * @returns {void}
117 */
118 updateHash(hash, context) {
119 hash.update(this.sourceStr);
120 super.updateHash(hash, context);
121 }
122
123 serialize(context) {
124 const { write } = context;
125
126 write(this.sourceStr);
127 write(this.identifierStr);
128 write(this.readableIdentifierStr);
129
130 super.serialize(context);
131 }
132
133 deserialize(context) {
134 const { read } = context;
135
136 this.sourceStr = read();
137 this.identifierStr = read();
138 this.readableIdentifierStr = read();
139
140 super.deserialize(context);
141 }
142}
143
144makeSerializable(RawModule, "webpack/lib/RawModule");
145
146module.exports = RawModule;