From 176fa6d048ad3142e60c75a8253cabd9b28e3a49 Mon Sep 17 00:00:00 2001 From: Jonas Kruckenberg Date: Wed, 28 Apr 2021 11:43:36 +0200 Subject: [PATCH] feat: Add default directives (#81) * feat: Support for default directives This feature lets you specify directives that should be applied by default to every image, enabling a workflow similar to traditional image optimization plugins. The `defaultDirectives`option can either be an object of directives or a function returning an object. * test: add unit test for default directives --- packages/rollup/src/index.ts | 10 +++++++--- packages/rollup/src/types.ts | 6 ++++++ packages/vite/src/index.ts | 8 ++++++-- packages/vite/src/types.ts | 6 ++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/rollup/src/index.ts b/packages/rollup/src/index.ts index 6840a76c..d9d195b6 100644 --- a/packages/rollup/src/index.ts +++ b/packages/rollup/src/index.ts @@ -45,9 +45,13 @@ export function imagetools(userOptions: Partial = {}): Plugin { const outputMetadatas = [] for (const config of imageConfigs) { - const { transforms, warnings } = generateTransforms(config, transformFactories) + const defaultConfig = typeof pluginOptions.defaultDirectives === 'function' + ? pluginOptions.defaultDirectives(id) + : pluginOptions.defaultDirectives + + const { transforms, warnings } = generateTransforms({ ...defaultConfig, ...config }, transformFactories) warnings.forEach(warning => this.warn(warning)) - + const { image, metadata } = await applyTransforms(transforms, img, pluginOptions.removeMetadata) const fileName = basename(src.pathname, extname(src.pathname)) + `.${metadata.format}` @@ -71,7 +75,7 @@ export function imagetools(userOptions: Partial = {}): Plugin { break } } - + return dataToEsm(outputFormat(outputMetadatas)) }, renderChunk(code) { diff --git a/packages/rollup/src/types.ts b/packages/rollup/src/types.ts index aec41fdb..1e29a914 100644 --- a/packages/rollup/src/types.ts +++ b/packages/rollup/src/types.ts @@ -12,6 +12,12 @@ export interface PluginOptions { */ exclude: Array | string | RegExp + /** + * This option allows you to specify directives that should be applied _by default_ to every image. + * You can also provide a function, in which case the function gets passed the asset ID and should return an object of directives + */ + defaultDirectives?: Record | ((id: string) => Record) + /** * You can use this option to extend the builtin list of import transforms. * This list will be merged with the builtin transforms before applying them to the input image. diff --git a/packages/vite/src/index.ts b/packages/vite/src/index.ts index 059751a8..790888fa 100644 --- a/packages/vite/src/index.ts +++ b/packages/vite/src/index.ts @@ -46,10 +46,14 @@ export function imagetools(userOptions: Partial = {}): Plugin { const outputMetadatas = [] - for (const config of imageConfigs) { + for (let config of imageConfigs) { const id = generateImageID(src, config) - const { transforms } = generateTransforms(config, transformFactories) + const defaultConfig = typeof pluginOptions.defaultDirectives === 'function' + ? pluginOptions.defaultDirectives(id) + : pluginOptions.defaultDirectives + + const { transforms } = generateTransforms({ ...defaultConfig, ...config }, transformFactories) const { image, metadata } = await applyTransforms(transforms, img, pluginOptions.removeMetadata) generatedImages.set(id, image) diff --git a/packages/vite/src/types.ts b/packages/vite/src/types.ts index b56d0e92..aa408001 100644 --- a/packages/vite/src/types.ts +++ b/packages/vite/src/types.ts @@ -15,6 +15,12 @@ export interface PluginOptions { */ exclude: Array | string | RegExp + /** + * This option allows you to specify directives that should be applied _by default_ to every image. + * You can also provide a function, in which case the function gets passed the asset ID and should return an object of directives + */ + defaultDirectives?: Record | ((id: string) => Record) + /** * You can use this option to extend the builtin list of import transforms. * This list will be merged with the builtin transforms before applying them to the input image.