@@ -10,7 +10,7 @@ const { scheduleJob } = require(`./scheduler`)
10
10
const { createArgsDigest } = require ( `./process-file` )
11
11
const { reportError } = require ( `./report-error` )
12
12
const { getPluginOptions, healOptions } = require ( `./plugin-options` )
13
- const { memoizedTraceSVG } = require ( `./trace-svg` )
13
+ const { memoizedTraceSVG, notMemoizedtraceSVG } = require ( `./trace-svg` )
14
14
15
15
const imageSizeCache = new Map ( )
16
16
const getImageSize = file => {
@@ -202,40 +202,49 @@ async function generateBase64({ file, args, reporter }) {
202
202
return base64output
203
203
}
204
204
205
- const base64CacheKey = ( { file, args } ) => `${ file . id } ${ JSON . stringify ( args ) } `
205
+ const generateCacheKey = ( { file, args } ) => `${ file . id } ${ JSON . stringify ( args ) } `
206
206
207
- const memoizedBase64 = _ . memoize ( generateBase64 , base64CacheKey )
207
+ const memoizedBase64 = _ . memoize ( generateBase64 , generateCacheKey )
208
208
209
- const cachifiedBase64 = async ( { cache, ...arg } ) => {
210
- const cacheKey = base64CacheKey ( arg )
209
+ const cachifiedProcess = async ( { cache, ...arg } , genKey , processFn ) => {
210
+ const cachedKey = genKey ( arg )
211
+ const cached = await cache . get ( cachedKey )
211
212
212
- const cachedBase64 = await cache . get ( cacheKey )
213
- if ( cachedBase64 ) {
214
- return cachedBase64
213
+ if ( cached ) {
214
+ return cached
215
215
}
216
216
217
- const base64output = await generateBase64 ( arg )
217
+ const result = await processFn ( arg )
218
+ await cache . set ( cachedKey , result )
218
219
219
- await cache . set ( cacheKey , base64output )
220
-
221
- return base64output
220
+ return result
222
221
}
223
222
224
223
async function base64 ( arg ) {
225
224
if ( arg . cache ) {
226
225
// Not all tranformer plugins are going to provide cache
227
- return await cachifiedBase64 ( arg )
226
+ return await cachifiedProcess ( arg , generateCacheKey , generateBase64 )
228
227
}
229
228
230
229
return await memoizedBase64 ( arg )
231
230
}
232
231
233
- async function getTracedSVG ( options , file ) {
232
+ async function traceSVG ( args ) {
233
+ if ( args . cache ) {
234
+ // Not all tranformer plugins are going to provide cache
235
+ return await cachifiedProcess ( args , generateCacheKey , notMemoizedtraceSVG )
236
+ }
237
+ return await memoizedTraceSVG ( args )
238
+ }
239
+
240
+ async function getTracedSVG ( { file, options, cache, reporter } ) {
234
241
if ( options . generateTracedSVG && options . tracedSVG ) {
235
242
const tracedSVG = await traceSVG ( {
236
- file,
237
243
args : options . tracedSVG ,
238
244
fileArgs : options ,
245
+ file,
246
+ cache,
247
+ reporter,
239
248
} )
240
249
return tracedSVG
241
250
}
@@ -380,7 +389,7 @@ async function fluid({ file, args = {}, reporter, cache }) {
380
389
base64Image = await base64 ( { file, args : base64Args , reporter, cache } )
381
390
}
382
391
383
- const tracedSVG = await getTracedSVG ( options , file )
392
+ const tracedSVG = await getTracedSVG ( { options, file, cache , reporter } )
384
393
385
394
// Construct src and srcSet strings.
386
395
const originalImg = _ . maxBy ( images , image => image . width ) . src
@@ -503,7 +512,7 @@ async function fixed({ file, args = {}, reporter, cache }) {
503
512
} )
504
513
}
505
514
506
- const tracedSVG = await getTracedSVG ( options , file )
515
+ const tracedSVG = await getTracedSVG ( { options, file, reporter , cache } )
507
516
508
517
const fallbackSrc = images [ 0 ] . src
509
518
const srcSet = images
@@ -539,10 +548,6 @@ async function fixed({ file, args = {}, reporter, cache }) {
539
548
}
540
549
}
541
550
542
- async function traceSVG ( args ) {
543
- return await memoizedTraceSVG ( args )
544
- }
545
-
546
551
function toArray ( buf ) {
547
552
var arr = new Array ( buf . length )
548
553
0 commit comments