Skip to content

Commit

Permalink
fix: inline [('"] to ("
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Jan 10, 2023
1 parent b0d2a22 commit a0a56c3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { defineConfig } from 'vite'
import imagePlaceholder from '../src/index'

export default defineConfig(() => ({
plugins: [imagePlaceholder({ inline: false })],
plugins: [imagePlaceholder({ inline: true })],
}))
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"scripts": {
"build": "tsup",
"dev": "DEBUG=vite:plugin-image-placeholder vite example --config ./example/vite.config.ts",
"build:example": "DEBUG=vite:plugin-image-placeholder vite build example --config ./example/vite.config.ts",
"docs:build": "vitepress build docs",
"docs:dev": "vitepress dev docs",
"docs:preview": "vitepress preview docs",
Expand Down
28 changes: 16 additions & 12 deletions src/pathToImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,23 @@ export async function pathToImage(
rgba: true,
}

const imgBuf = await createImage({
type: imgType,
create: createOptions,
text: textOptions,
quality: options.quality,
compressionLevel: options.compressionLevel,
})
const result: ImageCacheItem = {
type: imgType,
buffer: imgBuf,
try {
const imgBuf = await createImage({
type: imgType,
create: createOptions,
text: textOptions,
quality: options.quality,
compressionLevel: options.compressionLevel,
})
const result: ImageCacheItem = {
type: imgType,
buffer: imgBuf,
}
bufferCache.set(url, result)
return result
} catch (e) {
console.error(e)
}
bufferCache.set(url, result)
return result
}

export async function createImage({
Expand Down
40 changes: 22 additions & 18 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const parseOptions = (
textColor: '#333',
width: 300,
type: 'png',
quality: 100,
quality: 80,
compressionLevel: 6,
inline: false,
} as ImagePlaceholderOptions,
Expand Down Expand Up @@ -49,19 +49,13 @@ function placeholderServerPlugin(
const url = req.url!
if (!url.startsWith(opts.prefix)) return next()

try {
const image = await pathToImage(url, pathRules, opts)
const image = await pathToImage(url, pathRules, opts)

if (!image) return next()
if (!image) return next()

res.setHeader('Accept-Ranges', 'bytes')
res.setHeader('Content-Type', getMimeType(image.type))
res.end(image.buffer)
return
} catch (e) {
console.error(e)
}
next()
res.setHeader('Accept-Ranges', 'bytes')
res.setHeader('Content-Type', getMimeType(image.type))
res.end(image.buffer)
})
},
}
Expand Down Expand Up @@ -139,19 +133,24 @@ function placeholderInlinePlugin(
let match
// eslint-disable-next-line no-cond-assign
while ((match = RE_PATTERN.exec(code))) {
const url = match[1] || match[2] || match[3] || match[4]
const url = match[4] || match[3] || match[2] || match[1]
const dynamic = match[0].includes('(') ? ['("', '")'] : ['"', '"']
const start = match.index
const end = start + match[0].length
if (contentCache.has(url)) {
hasReplaced = true
s.update(start, end, `"${contentCache.get(url)}"`)
s.update(
start,
end,
`${dynamic[0]}${contentCache.get(url)}${dynamic[1]}`,
)
} else {
const image = await pathToImage(url, pathRules, opts)
if (image) {
hasReplaced = true
const content = bufferToBase64(image)
contentCache.set(url, content)
s.update(start, end, `"${content}"`)
s.update(start, end, `${dynamic[0]}${content}${dynamic[1]}`)
}
}
}
Expand All @@ -169,17 +168,22 @@ function placeholderInlinePlugin(
let match
// eslint-disable-next-line no-cond-assign
while ((match = RE_PATTERN.exec(html))) {
const url = match[1] || match[2] || match[3] || match[4]
const url = match[4] || match[3] || match[2] || match[1]
const dynamic = match[0].includes('(') ? ['("', '")'] : ['"', '"']
const start = match.index
const end = start + match[0].length
if (contentCache.has(url)) {
s.update(start, end, `"${contentCache.get(url)}"`)
s.update(
start,
end,
`${dynamic[0]}${contentCache.get(url)}${dynamic[1]}`,
)
} else {
const image = await pathToImage(url, pathRules, opts)
if (image) {
const content = bufferToBase64(image)
contentCache.set(url, content)
s.update(start, end, `"${content}"`)
s.update(start, end, `${dynamic[0]}${content}${dynamic[1]}`)
}
}
}
Expand Down

0 comments on commit a0a56c3

Please sign in to comment.