Skip to content

Commit

Permalink
feat: Add default directives (#81)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JonasKruckenberg authored Apr 28, 2021
1 parent 45b4e02 commit 176fa6d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
10 changes: 7 additions & 3 deletions packages/rollup/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): 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}`
Expand All @@ -71,7 +75,7 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
break
}
}

return dataToEsm(outputFormat(outputMetadatas))
},
renderChunk(code) {
Expand Down
6 changes: 6 additions & 0 deletions packages/rollup/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export interface PluginOptions {
*/
exclude: Array<string | RegExp> | 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<string, string> | ((id: string) => Record<string, string>)

/**
* 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.
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): 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)
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export interface PluginOptions {
*/
exclude: Array<string | RegExp> | 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<string, string> | ((id: string) => Record<string, string>)

/**
* 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.
Expand Down

0 comments on commit 176fa6d

Please sign in to comment.