2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
8 const NormalModule = require("../NormalModule");
10 /** @typedef {import("../Compiler")} Compiler */
12 // data URL scheme: "data:text/javascript;charset=utf-8;base64,some-string"
13 // http://www.ietf.org/rfc/rfc2397.txt
14 const URIRegEx = /^data:([^;,]+)?((?:;[^;,]+)*?)(?:;(base64))?,(.*)$/i;
16 const decodeDataURI = uri => {
17 const match = URIRegEx.exec(uri);
18 if (!match) return null;
20 const isBase64 = match[3];
21 const body = match[4];
23 ? Buffer.from(body, "base64")
24 : Buffer.from(decodeURIComponent(body), "ascii");
30 * @param {Compiler} compiler the compiler instance
34 compiler.hooks.compilation.tap(
36 (compilation, { normalModuleFactory }) => {
37 normalModuleFactory.hooks.resolveForScheme
39 .tap("DataUriPlugin", resourceData => {
40 const match = URIRegEx.exec(resourceData.resource);
42 resourceData.data.mimetype = match[1] || "";
43 resourceData.data.parameters = match[2] || "";
44 resourceData.data.encoding = match[3] || false;
45 resourceData.data.encodedContent = match[4] || "";
48 NormalModule.getCompilationHooks(compilation)
49 .readResourceForScheme.for("data")
50 .tap("DataUriPlugin", resource => decodeDataURI(resource));
56 module.exports = DataUriPlugin;