Skip to content

Commit

Permalink
feat: add path rules
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Jan 13, 2023
1 parent 0ebcb03 commit d300f8a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
9 changes: 0 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import type { ImagePlaceholderParams } from './types'

export const DEFAULT_PREFIX = 'image/placeholder'

export const DEFAULT_PARAMS: ImagePlaceholderParams = {
width: 300,
type: 'png',
}

export const imageMimeType = {
jpg: 'image/jpg',
jpeg: 'image/jpeg',
Expand Down
32 changes: 26 additions & 6 deletions src/pathRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@ import { pathToRegexp } from 'path-to-regexp'

export type FindPathRule = (pathname: string) => string | undefined

const pattern =
/([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8}|(?:rgba?)?\(?\d{1,3},\s*\d{1,3},\s*\d{1,3}(?:,\s*(?:\d?\.?\d))?\)?)/
.source

const background = `/(bg|background)/:background${pattern}`
const text = '/(text|t)/:text'
const textColor = `/(textColor|color|c)/:textColor${pattern}`
const last = '/:width(\\d+)?/:height(\\d+)?{.:type}?'

export function generatePathRules(prefix: string) {
return [
'/bg/:background/text/:text/:width?/:height?{.:type}?',
'/text/:text/bg/:background/:width?/:height?{.:type}?',
'/text/:text/:width?/:height?{.:type}?',
'/bg/:background/:width?/:height?{.:type}?',
'/:width?/:height?{.:type}?',
].map((_) => `${prefix}${_}`)
`${background}${text}${textColor}`,
`${background}${textColor}${text}`,
`${text}${background}${textColor}`,
`${text}${textColor}${background}`,
`${textColor}${background}${text}`,
`${textColor}${text}${background}`,
`${background}${text}`,
`${text}${background}`,
`${background}${textColor}`,
`${textColor}${background}`,
`${text}${textColor}`,
`${textColor}${text}`,
background,
text,
textColor,
'',
].map((_) => `${prefix}${_}${last}`)
}

export function createPathRuleMatch(prefix: string): FindPathRule {
Expand Down
5 changes: 2 additions & 3 deletions src/pathToImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { match } from 'path-to-regexp'
import type { Create, CreateText } from 'sharp'
import sharp from 'sharp'
import { bufferCache } from './cache'
import { DEFAULT_PARAMS } from './constants'
import type { FindPathRule } from './pathRules'
import type {
ImageCacheItem,
Expand Down Expand Up @@ -41,7 +40,7 @@ export async function pathToImage(

const params = urlMatch.params as ImagePlaceholderParams
const query = urlQuery as ImagePlaceholderQuery
const imgType = params.type || DEFAULT_PARAMS.type!
const imgType = params.type || options.type
const width = Number(params.width) || 300
const height = Number(params.height) || Math.ceil(width * options.ratio)

Expand Down Expand Up @@ -74,7 +73,7 @@ export async function pathToImage(
text: textOptions,
quality: options.quality,
compressionLevel: options.compressionLevel,
textColor: formatColor(query.textColor, true) || options.textColor,
textColor: formatColor(params.textColor, true) || options.textColor,
})
const result: ImageCacheItem = {
type: imgType,
Expand Down
4 changes: 2 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import MagicString from 'magic-string'
import type { Plugin, ResolvedConfig } from 'vite'
import { isCSSRequest } from 'vite'
import { contentCache } from './cache'
import { DEFAULT_PREFIX } from './constants'
import type { FindPathRule } from './pathRules'
import { createPathRuleMatch } from './pathRules'
import { pathToImage } from './pathToImage'
Expand All @@ -15,7 +14,7 @@ export const parseOptions = (
): Required<ImagePlaceholderOptions> => {
options = Object.assign(
{
prefix: DEFAULT_PREFIX,
prefix: 'image/placeholder',
background: '#ccc',
textColor: '#333',
width: 300,
Expand Down Expand Up @@ -78,6 +77,7 @@ function placeholderServerPlugin(

res.setHeader('Accept-Ranges', 'bytes')
res.setHeader('Content-Type', getMimeType(image.type))
res.setHeader('Cache-Control', 'max-age=300')
res.end(image.buffer)
})
},
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export interface ImagePlaceholderParams {
width?: number
height?: number
text?: string
textColor?: string
background?: string
type?: ImageType
}
Expand All @@ -125,7 +126,6 @@ export interface ImagePlaceholderQuery {
noise?: 0 | 1
noiseMean?: number
noiseSigma?: number
textColor?: string
}

export interface ImageCacheItem {
Expand Down

0 comments on commit d300f8a

Please sign in to comment.