Skip to content

Commit

Permalink
fix: process URLs with no query string (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Oct 9, 2022
1 parent 1ead295 commit 07df0fa
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/eight-timers-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'rollup-plugin-imagetools': patch
'vite-imagetools': patch
---

fix: process URLs with no query string
2 changes: 1 addition & 1 deletion packages/rollup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ ___

Which paths to include when processing images.

**`default`** '**\/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*'
**`default`** `['**\/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}', '**\/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*']`

___

Expand Down
11 changes: 10 additions & 1 deletion packages/rollup/src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe('rollup-plugin-imagetools', () => {
input: join(__dirname, '__fixtures__/index.js'),
plugins: [
testEntry(`
import Image from "./with-metadata.png?metadata"
import Image from "./with-metadata.png"
export default Image
`),
imagetools({
Expand Down Expand Up @@ -368,6 +368,15 @@ describe('rollup-plugin-imagetools', () => {
await expect(p).rejects.toBeDefined()
})

test('not an image', async () => {
const p = rollup({
input: join(__dirname, '__fixtures__/index.js'),
plugins: [testEntry(`import Image from "./pexels-allec-gomes-5195763.xml"`), imagetools()]
})

await expect(p).rejects.toBeDefined()
})

test('no directives', async () => {
const p = rollup({
input: join(__dirname, '__fixtures__/index.js'),
Expand Down
7 changes: 6 additions & 1 deletion packages/rollup/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import MagicString from 'magic-string'
import { basename, extname, resolve, dirname } from 'path'

const defaultOptions: RollupPluginOptions = {
include: '**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*',
include: [
'**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}',
'**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*'
],
exclude: '',
silent: false,
removeMetadata: true
Expand Down Expand Up @@ -54,6 +57,8 @@ export function imagetools(userOptions: Partial<RollupPluginOptions> = {}): Plug
directives = new URLSearchParams([...pluginOptions.defaultDirectives, ...srcURL.searchParams])
}

if (!directives.toString()) return null

const parameters = extractEntries(directives)
const imageConfigs =
pluginOptions.resolveConfigs?.(parameters, outputFormats) ?? resolveConfigs(parameters, outputFormats)
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ ___
What paths to exclude when processing images.
This defaults to the public dir to mirror vites behavior.

**`default`** 'public\/**\/*'
**`default`** `'public\/**\/*'`

___

Expand All @@ -104,7 +104,7 @@ ___

Which paths to include when processing images.

**`default`** '**\/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*'
**`default`** `['**\/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}', '**\/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*']`

___

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe('vite-imagetools', () => {
build: { write: false },
plugins: [
testEntry(`
import Image from "./with-metadata.png?metadata"
import Image from "./with-metadata.png"
window.__IMAGE__ = Image
`),
imagetools({
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import MagicString from 'magic-string'
import { VitePluginOptions } from './types'

const defaultOptions: VitePluginOptions = {
include: '**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*',
include: [
'**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}',
'**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*'
],
exclude: 'public/**/*',
silent: false,
removeMetadata: true
Expand Down Expand Up @@ -58,6 +61,8 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
directives = new URLSearchParams([...pluginOptions.defaultDirectives, ...srcURL.searchParams])
}

if (!directives.toString()) return null

const parameters = extractEntries(directives)
const imageConfigs =
pluginOptions.resolveConfigs?.(parameters, outputFormats) ?? resolveConfigs(parameters, outputFormats)
Expand Down

0 comments on commit 07df0fa

Please sign in to comment.