Skip to content

Commit d300f8a

Browse files
committed
feat: add path rules
1 parent 0ebcb03 commit d300f8a

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

src/constants.ts

-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
import type { ImagePlaceholderParams } from './types'
2-
3-
export const DEFAULT_PREFIX = 'image/placeholder'
4-
5-
export const DEFAULT_PARAMS: ImagePlaceholderParams = {
6-
width: 300,
7-
type: 'png',
8-
}
9-
101
export const imageMimeType = {
112
jpg: 'image/jpg',
123
jpeg: 'image/jpeg',

src/pathRules.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@ import { pathToRegexp } from 'path-to-regexp'
22

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

5+
const pattern =
6+
/([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))?\)?)/
7+
.source
8+
9+
const background = `/(bg|background)/:background${pattern}`
10+
const text = '/(text|t)/:text'
11+
const textColor = `/(textColor|color|c)/:textColor${pattern}`
12+
const last = '/:width(\\d+)?/:height(\\d+)?{.:type}?'
13+
514
export function generatePathRules(prefix: string) {
615
return [
7-
'/bg/:background/text/:text/:width?/:height?{.:type}?',
8-
'/text/:text/bg/:background/:width?/:height?{.:type}?',
9-
'/text/:text/:width?/:height?{.:type}?',
10-
'/bg/:background/:width?/:height?{.:type}?',
11-
'/:width?/:height?{.:type}?',
12-
].map((_) => `${prefix}${_}`)
16+
`${background}${text}${textColor}`,
17+
`${background}${textColor}${text}`,
18+
`${text}${background}${textColor}`,
19+
`${text}${textColor}${background}`,
20+
`${textColor}${background}${text}`,
21+
`${textColor}${text}${background}`,
22+
`${background}${text}`,
23+
`${text}${background}`,
24+
`${background}${textColor}`,
25+
`${textColor}${background}`,
26+
`${text}${textColor}`,
27+
`${textColor}${text}`,
28+
background,
29+
text,
30+
textColor,
31+
'',
32+
].map((_) => `${prefix}${_}${last}`)
1333
}
1434

1535
export function createPathRuleMatch(prefix: string): FindPathRule {

src/pathToImage.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { match } from 'path-to-regexp'
33
import type { Create, CreateText } from 'sharp'
44
import sharp from 'sharp'
55
import { bufferCache } from './cache'
6-
import { DEFAULT_PARAMS } from './constants'
76
import type { FindPathRule } from './pathRules'
87
import type {
98
ImageCacheItem,
@@ -41,7 +40,7 @@ export async function pathToImage(
4140

4241
const params = urlMatch.params as ImagePlaceholderParams
4342
const query = urlQuery as ImagePlaceholderQuery
44-
const imgType = params.type || DEFAULT_PARAMS.type!
43+
const imgType = params.type || options.type
4544
const width = Number(params.width) || 300
4645
const height = Number(params.height) || Math.ceil(width * options.ratio)
4746

@@ -74,7 +73,7 @@ export async function pathToImage(
7473
text: textOptions,
7574
quality: options.quality,
7675
compressionLevel: options.compressionLevel,
77-
textColor: formatColor(query.textColor, true) || options.textColor,
76+
textColor: formatColor(params.textColor, true) || options.textColor,
7877
})
7978
const result: ImageCacheItem = {
8079
type: imgType,

src/plugin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import MagicString from 'magic-string'
22
import type { Plugin, ResolvedConfig } from 'vite'
33
import { isCSSRequest } from 'vite'
44
import { contentCache } from './cache'
5-
import { DEFAULT_PREFIX } from './constants'
65
import type { FindPathRule } from './pathRules'
76
import { createPathRuleMatch } from './pathRules'
87
import { pathToImage } from './pathToImage'
@@ -15,7 +14,7 @@ export const parseOptions = (
1514
): Required<ImagePlaceholderOptions> => {
1615
options = Object.assign(
1716
{
18-
prefix: DEFAULT_PREFIX,
17+
prefix: 'image/placeholder',
1918
background: '#ccc',
2019
textColor: '#333',
2120
width: 300,
@@ -78,6 +77,7 @@ function placeholderServerPlugin(
7877

7978
res.setHeader('Accept-Ranges', 'bytes')
8079
res.setHeader('Content-Type', getMimeType(image.type))
80+
res.setHeader('Cache-Control', 'max-age=300')
8181
res.end(image.buffer)
8282
})
8383
},

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export interface ImagePlaceholderParams {
117117
width?: number
118118
height?: number
119119
text?: string
120+
textColor?: string
120121
background?: string
121122
type?: ImageType
122123
}
@@ -125,7 +126,6 @@ export interface ImagePlaceholderQuery {
125126
noise?: 0 | 1
126127
noiseMean?: number
127128
noiseSigma?: number
128-
textColor?: string
129129
}
130130

131131
export interface ImageCacheItem {

0 commit comments

Comments
 (0)