WXL
2025-12-27 05e6b08007a86b5b10c680babc9c3bcc3a1a201b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
const NormalModule = require("../NormalModule");
const { URIRegEx, decodeDataURI } = require("../util/dataURL");
 
/** @typedef {import("../Compiler")} Compiler */
 
const PLUGIN_NAME = "DataUriPlugin";
 
class DataUriPlugin {
    /**
     * Apply the plugin
     * @param {Compiler} compiler the compiler instance
     * @returns {void}
     */
    apply(compiler) {
        compiler.hooks.compilation.tap(
            PLUGIN_NAME,
            (compilation, { normalModuleFactory }) => {
                normalModuleFactory.hooks.resolveForScheme
                    .for("data")
                    .tap(PLUGIN_NAME, (resourceData) => {
                        const match = URIRegEx.exec(resourceData.resource);
                        if (match) {
                            resourceData.data.mimetype = match[1] || "";
                            resourceData.data.parameters = match[2] || "";
                            resourceData.data.encoding = /** @type {"base64" | false} */ (
                                match[3] || false
                            );
                            resourceData.data.encodedContent = match[4] || "";
                        }
                    });
 
                NormalModule.getCompilationHooks(compilation)
                    .readResourceForScheme.for("data")
                    .tap(PLUGIN_NAME, (resource) => decodeDataURI(resource));
            }
        );
    }
}
 
module.exports = DataUriPlugin;