webpack/lib/schemes/DataUriPlugin.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-07-03 20:45:49 +08:00
/*
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");
2020-07-03 20:45:49 +08:00
/** @typedef {import("../Compiler")} Compiler */
2025-04-23 20:03:37 +08:00
const PLUGIN_NAME = "DataUriPlugin";
2020-07-03 20:45:49 +08:00
class DataUriPlugin {
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
2025-04-23 20:03:37 +08:00
PLUGIN_NAME,
2020-07-03 20:45:49 +08:00
(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] || "";
2025-05-13 21:03:58 +08:00
resourceData.data.encoding = /** @type {"base64" | false} */ (
match[3] || false
);
resourceData.data.encodedContent = match[4] || "";
}
2020-07-03 20:45:49 +08:00
});
NormalModule.getCompilationHooks(compilation)
.readResourceForScheme.for("data")
.tap(PLUGIN_NAME, (resource) => decodeDataURI(resource));
2020-07-03 20:45:49 +08:00
}
);
}
}
module.exports = DataUriPlugin;