1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- "use strict";
- const NormalModule = require("../NormalModule");
- const URIRegEx = /^data:([^;,]+)?((?:;[^;,]+)*?)(?:;(base64))?,(.*)$/i;
- const decodeDataURI = uri => {
- const match = URIRegEx.exec(uri);
- if (!match) return null;
- const isBase64 = match[3];
- const body = match[4];
- return isBase64
- ? Buffer.from(body, "base64")
- : Buffer.from(decodeURIComponent(body), "ascii");
- };
- class DataUriPlugin {
-
- apply(compiler) {
- compiler.hooks.compilation.tap(
- "DataUriPlugin",
- (compilation, { normalModuleFactory }) => {
- normalModuleFactory.hooks.resolveForScheme
- .for("data")
- .tap("DataUriPlugin", resourceData => {
- const match = URIRegEx.exec(resourceData.resource);
- if (match) {
- resourceData.data.mimetype = match[1] || "";
- resourceData.data.parameters = match[2] || "";
- resourceData.data.encoding = match[3] || false;
- resourceData.data.encodedContent = match[4] || "";
- }
- });
- NormalModule.getCompilationHooks(compilation)
- .readResourceForScheme.for("data")
- .tap("DataUriPlugin", resource => decodeDataURI(resource));
- }
- );
- }
- }
- module.exports = DataUriPlugin;
|