123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- "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];
- if (isBase64) {
- return Buffer.from(body, "base64");
- }
-
-
- try {
- return Buffer.from(decodeURIComponent(body), "ascii");
- } catch (_) {
- return Buffer.from(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;
|