source: imaps-frontend/node_modules/webpack/lib/schemes/DataUriPlugin.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.9 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const NormalModule = require("../NormalModule");
9
10/** @typedef {import("../Compiler")} Compiler */
11
12// data URL scheme: "data:text/javascript;charset=utf-8;base64,some-string"
13// http://www.ietf.org/rfc/rfc2397.txt
14const URIRegEx = /^data:([^;,]+)?((?:;[^;,]+)*?)(?:;(base64)?)?,(.*)$/i;
15
16/**
17 * @param {string} uri data URI
18 * @returns {Buffer | null} decoded data
19 */
20const decodeDataURI = uri => {
21 const match = URIRegEx.exec(uri);
22 if (!match) return null;
23
24 const isBase64 = match[3];
25 const body = match[4];
26
27 if (isBase64) {
28 return Buffer.from(body, "base64");
29 }
30
31 // CSS allows to use `data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg"><rect width="100%" height="100%" style="stroke: rgb(223,224,225); stroke-width: 2px; fill: none; stroke-dasharray: 6px 3px" /></svg>`
32 // so we return original body if we can't `decodeURIComponent`
33 try {
34 return Buffer.from(decodeURIComponent(body), "ascii");
35 } catch (_) {
36 return Buffer.from(body, "ascii");
37 }
38};
39
40class DataUriPlugin {
41 /**
42 * Apply the plugin
43 * @param {Compiler} compiler the compiler instance
44 * @returns {void}
45 */
46 apply(compiler) {
47 compiler.hooks.compilation.tap(
48 "DataUriPlugin",
49 (compilation, { normalModuleFactory }) => {
50 normalModuleFactory.hooks.resolveForScheme
51 .for("data")
52 .tap("DataUriPlugin", resourceData => {
53 const match = URIRegEx.exec(resourceData.resource);
54 if (match) {
55 resourceData.data.mimetype = match[1] || "";
56 resourceData.data.parameters = match[2] || "";
57 resourceData.data.encoding = match[3] || false;
58 resourceData.data.encodedContent = match[4] || "";
59 }
60 });
61 NormalModule.getCompilationHooks(compilation)
62 .readResourceForScheme.for("data")
63 .tap("DataUriPlugin", resource => decodeDataURI(resource));
64 }
65 );
66 }
67}
68
69module.exports = DataUriPlugin;
Note: See TracBrowser for help on using the repository browser.