@@ -74,6 +74,41 @@ const mergeRelatedInfo = (a, b) => {
7474 return result ;
7575} ;
7676
77+ const encodeDataUri = ( encoding , source ) => {
78+ let encodedContent ;
79+
80+ switch ( encoding ) {
81+ case "base64" : {
82+ encodedContent = source . buffer ( ) . toString ( "base64" ) ;
83+ break ;
84+ }
85+ case false : {
86+ const content = source . source ( ) ;
87+
88+ if ( typeof content !== "string" ) {
89+ encodedContent = content . toString ( "utf-8" ) ;
90+ }
91+
92+ encodedContent = encodeURIComponent ( encodedContent ) . replace (
93+ / [ ! ' ( ) * ] / g,
94+ character => "%" + character . codePointAt ( 0 ) . toString ( 16 )
95+ ) ;
96+ break ;
97+ }
98+ default :
99+ throw new Error ( `Unsupported encoding '${ encoding } '` ) ;
100+ }
101+
102+ return encodedContent ;
103+ } ;
104+
105+ const decodeDataUriContent = ( encoding , content ) => {
106+ const isBase64 = encoding === "base64" ;
107+ return isBase64
108+ ? Buffer . from ( content , "base64" )
109+ : Buffer . from ( decodeURIComponent ( content ) , "ascii" ) ;
110+ } ;
111+
77112const JS_TYPES = new Set ( [ "javascript" ] ) ;
78113const JS_AND_ASSET_TYPES = new Set ( [ "javascript" , "asset" ] ) ;
79114
@@ -158,33 +193,18 @@ class AssetGenerator extends Generator {
158193 }
159194
160195 let encodedContent ;
196+
161197 if (
162198 module . resourceResolveData &&
163- module . resourceResolveData . encoding === encoding
199+ module . resourceResolveData . encoding === encoding &&
200+ decodeDataUriContent (
201+ module . resourceResolveData . encoding ,
202+ module . resourceResolveData . encodedContent
203+ ) . equals ( originalSource . buffer ( ) )
164204 ) {
165205 encodedContent = module . resourceResolveData . encodedContent ;
166206 } else {
167- switch ( encoding ) {
168- case "base64" : {
169- encodedContent = originalSource . buffer ( ) . toString ( "base64" ) ;
170- break ;
171- }
172- case false : {
173- const content = originalSource . source ( ) ;
174-
175- if ( typeof content !== "string" ) {
176- encodedContent = content . toString ( "utf-8" ) ;
177- }
178-
179- encodedContent = encodeURIComponent ( encodedContent ) . replace (
180- / [ ! ' ( ) * ] / g,
181- character => "%" + character . codePointAt ( 0 ) . toString ( 16 )
182- ) ;
183- break ;
184- }
185- default :
186- throw new Error ( `Unsupported encoding '${ encoding } '` ) ;
187- }
207+ encodedContent = encodeDataUri ( encoding , originalSource ) ;
188208 }
189209
190210 encodedSource = `data:${ mimeType } ${
0 commit comments