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 { JS_TYPES } = require("../ModuleSourceTypesConstants");
|
---|
11 | const { ASSET_MODULE_TYPE_RAW_DATA_URL } = require("../ModuleTypeConstants");
|
---|
12 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
13 | const makeSerializable = require("../util/makeSerializable");
|
---|
14 |
|
---|
15 | /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
|
---|
16 | /** @typedef {import("../Compilation")} Compilation */
|
---|
17 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
18 | /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
|
---|
19 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
---|
20 | /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
|
---|
21 | /** @typedef {import("../Module").SourceTypes} SourceTypes */
|
---|
22 | /** @typedef {import("../RequestShortener")} RequestShortener */
|
---|
23 | /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
---|
24 | /** @typedef {import("../WebpackError")} WebpackError */
|
---|
25 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
26 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
27 | /** @typedef {import("../util/Hash")} Hash */
|
---|
28 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
---|
29 |
|
---|
30 | class RawDataUrlModule extends Module {
|
---|
31 | /**
|
---|
32 | * @param {string} url raw url
|
---|
33 | * @param {string} identifier unique identifier
|
---|
34 | * @param {string=} readableIdentifier readable identifier
|
---|
35 | */
|
---|
36 | constructor(url, identifier, readableIdentifier) {
|
---|
37 | super(ASSET_MODULE_TYPE_RAW_DATA_URL, null);
|
---|
38 | this.url = url;
|
---|
39 | this.urlBuffer = url ? Buffer.from(url) : undefined;
|
---|
40 | this.identifierStr = identifier || this.url;
|
---|
41 | this.readableIdentifierStr = readableIdentifier || this.identifierStr;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * @returns {SourceTypes} types available (do not mutate)
|
---|
46 | */
|
---|
47 | getSourceTypes() {
|
---|
48 | return JS_TYPES;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * @returns {string} a unique identifier of the module
|
---|
53 | */
|
---|
54 | identifier() {
|
---|
55 | return this.identifierStr;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * @param {string=} type the source type for which the size should be estimated
|
---|
60 | * @returns {number} the estimated size of the module (must be non-zero)
|
---|
61 | */
|
---|
62 | size(type) {
|
---|
63 | if (this.url === undefined)
|
---|
64 | this.url = /** @type {Buffer} */ (this.urlBuffer).toString();
|
---|
65 | return Math.max(1, this.url.length);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * @param {RequestShortener} requestShortener the request shortener
|
---|
70 | * @returns {string} a user readable identifier of the module
|
---|
71 | */
|
---|
72 | readableIdentifier(requestShortener) {
|
---|
73 | return /** @type {string} */ (
|
---|
74 | requestShortener.shorten(this.readableIdentifierStr)
|
---|
75 | );
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * @param {NeedBuildContext} context context info
|
---|
80 | * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
|
---|
81 | * @returns {void}
|
---|
82 | */
|
---|
83 | needBuild(context, callback) {
|
---|
84 | return callback(null, !this.buildMeta);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * @param {WebpackOptions} options webpack options
|
---|
89 | * @param {Compilation} compilation the compilation
|
---|
90 | * @param {ResolverWithOptions} resolver the resolver
|
---|
91 | * @param {InputFileSystem} fs the file system
|
---|
92 | * @param {function(WebpackError=): void} callback callback function
|
---|
93 | * @returns {void}
|
---|
94 | */
|
---|
95 | build(options, compilation, resolver, fs, callback) {
|
---|
96 | this.buildMeta = {};
|
---|
97 | this.buildInfo = {
|
---|
98 | cacheable: true
|
---|
99 | };
|
---|
100 | callback();
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * @param {CodeGenerationContext} context context for code generation
|
---|
105 | * @returns {CodeGenerationResult} result
|
---|
106 | */
|
---|
107 | codeGeneration(context) {
|
---|
108 | if (this.url === undefined)
|
---|
109 | this.url = /** @type {Buffer} */ (this.urlBuffer).toString();
|
---|
110 | const sources = new Map();
|
---|
111 | sources.set(
|
---|
112 | "javascript",
|
---|
113 | new RawSource(`module.exports = ${JSON.stringify(this.url)};`)
|
---|
114 | );
|
---|
115 | const data = new Map();
|
---|
116 | data.set("url", {
|
---|
117 | javascript: this.url
|
---|
118 | });
|
---|
119 | const runtimeRequirements = new Set();
|
---|
120 | runtimeRequirements.add(RuntimeGlobals.module);
|
---|
121 | return { sources, runtimeRequirements, data };
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * @param {Hash} hash the hash used to track dependencies
|
---|
126 | * @param {UpdateHashContext} context context
|
---|
127 | * @returns {void}
|
---|
128 | */
|
---|
129 | updateHash(hash, context) {
|
---|
130 | hash.update(/** @type {Buffer} */ (this.urlBuffer));
|
---|
131 | super.updateHash(hash, context);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * @param {ObjectSerializerContext} context context
|
---|
136 | */
|
---|
137 | serialize(context) {
|
---|
138 | const { write } = context;
|
---|
139 |
|
---|
140 | write(this.urlBuffer);
|
---|
141 | write(this.identifierStr);
|
---|
142 | write(this.readableIdentifierStr);
|
---|
143 |
|
---|
144 | super.serialize(context);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * @param {ObjectDeserializerContext} context context
|
---|
149 | */
|
---|
150 | deserialize(context) {
|
---|
151 | const { read } = context;
|
---|
152 |
|
---|
153 | this.urlBuffer = read();
|
---|
154 | this.identifierStr = read();
|
---|
155 | this.readableIdentifierStr = read();
|
---|
156 |
|
---|
157 | super.deserialize(context);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | makeSerializable(RawDataUrlModule, "webpack/lib/asset/RawDataUrlModule");
|
---|
162 |
|
---|
163 | module.exports = RawDataUrlModule;
|
---|