From c4198f46c64130eab86ca1b22020455de8f8ea00 Mon Sep 17 00:00:00 2001 From: JonasKruckenberg Date: Mon, 8 Mar 2021 17:01:53 +0100 Subject: [PATCH] feat: make directives and output formats extensible --- src/index.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index cfa9c696..0e3ebff1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,10 @@ export default function imagetools(userOptions: Partial = {}): Pl const pluginOptions = { ...defaultOptions, ...userOptions } + const directives = [...Object.values(builtinDiretcives), ...pluginOptions.customDirectives] + + const outputFormats = [...Object.values(builtinOutputFormats), ...pluginOptions.customOutputFormats] + return { name: 'imagetools', enforce: 'pre', @@ -43,8 +47,8 @@ export default function imagetools(userOptions: Partial = {}): Pl } if (!data) { - // build transformation pipeline - const { transforms, metadata: _metadata, parametersUsed } = buildTransforms(config,directives) + // build the transformation pipeline + const { transforms, metadata: _metadata, parametersUsed } = buildTransforms(config, directives) metadata = { src: src.pathname, ..._metadata } @@ -83,8 +87,9 @@ export default function imagetools(userOptions: Partial = {}): Pl return metadata })) - const output = Object.values(outputFormats) - .map(f => f(src,outputMetadatas)) + // go through all output formats to find the one to use + const output = outputFormats + .map(f => f(src, outputMetadatas)) .find(res => !!res) return dataToEsm(output)