Skip to content

Commit

Permalink
feat: make caching optional
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Mar 8, 2021
1 parent 63e919f commit c26be86
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const defaultOptions: PluginOptions = {
}

export default function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
const pluginOptions = { ...defaultOptions, ...userOptions }


return {
name: 'imagetools',
Expand All @@ -30,7 +32,15 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): Pl
const pipelineConfigs = buildDirectiveOptions(src)

const outputMetadatas = await Promise.all(pipelineConfigs.map(async config => {
let { data, metadata } = await restoreFromCache(id, options.cache)
const cacheId = JSON.stringify(config) // each image is addressed by its configuration
let data, metadata

// Only try to load from cache when caching is enabled
if (pluginOptions.cache) {
const res = await restoreFromCache(cacheId, pluginOptions.cache)
data = res.data
metadata = res.metadata
}

if (!data) {
// build transformation pipeline
Expand All @@ -50,9 +60,9 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): Pl
metadata = Object.assign({}, await image.metadata(), metadata)

delete metadata.xmp
// cache the result

//await cachePut(options.cache,id,data,{ metadata })
// if caching is enabled cache the result for future builds
if (pluginOptions.cache) await cachePut(pluginOptions.cache, cacheId, data, { metadata })
}
}

Expand Down

0 comments on commit c26be86

Please sign in to comment.