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