webpack
Version:
Packs ECMAScript/CommonJs/AMD modules for the browser. Allows you to split your codebase into multiple bundles, which can be loaded on demand. Supports loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.
48 lines (40 loc) • 1.28 kB
JavaScript
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
;
const NormalModule = require("../NormalModule");
const { URIRegEx, decodeDataURI } = require("../util/dataURL");
/** @typedef {import("../Compiler")} Compiler */
const PLUGIN_NAME = "DataUriPlugin";
class DataUriPlugin {
/**
* Applies the plugin by registering its hooks on the compiler.
* @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;