Skip to content

Commit

Permalink
fix: let vite create final paths (#412)
Browse files Browse the repository at this point in the history
* fix: let vite create final paths

* re-add "renderChunk" to support srcset

* Revert "re-add "renderChunk" to support srcset"

This reverts commit 6beca2f.

* remove magic-string

* add test

Co-authored-by: Philipp Zerelles <philipp@zerelles.com>
  • Loading branch information
benmccann and pzerelles committed Oct 10, 2022
1 parent 9e89ea3 commit d11b927
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-games-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-imagetools': patch
---

Let vite create correct paths in build result
3 changes: 1 addition & 2 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
},
"dependencies": {
"@rollup/pluginutils": "^4.1.2",
"imagetools-core": "workspace:^",
"magic-string": "^0.26.0"
"imagetools-core": "workspace:^"
},
"devDependencies": {
"@rollup/plugin-typescript": "^9.0.0",
Expand Down
22 changes: 22 additions & 0 deletions packages/vite/src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,4 +538,26 @@ describe('vite-imagetools', () => {
expect(window.__IMAGE__).toHaveProperty('format')
expect(window.__IMAGE__).not.toHaveProperty('height')
})

test('srcset', async () => {
const bundle = (await build({
root: join(__dirname, '__fixtures__'),
logLevel: 'warn',
build: { write: false },
plugins: [
testEntry(`
import Image from "./with-metadata.png?srcset"
window.__IMAGE__ = Image
`),
imagetools()
]
})) as RollupOutput | RollupOutput[]

const files = getFiles(bundle, '**.js') as OutputChunk[]
const { window } = new JSDOM(``, { runScripts: 'outside-only' })
window.eval(files[0].code)

expect(window.__IMAGE__).toBe('/assets/with-metadata.9ff75690.png 600w')
})

})
31 changes: 2 additions & 29 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from 'imagetools-core'
import { basename, extname, posix } from 'path'
import { createFilter, dataToEsm } from '@rollup/pluginutils'
import MagicString from 'magic-string'
import { VitePluginOptions } from './types'

const defaultOptions: VitePluginOptions = {
Expand Down Expand Up @@ -55,7 +54,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin

let directives = srcURL.searchParams

if(typeof pluginOptions.defaultDirectives === "function") {
if (typeof pluginOptions.defaultDirectives === 'function') {
directives = new URLSearchParams([...pluginOptions.defaultDirectives(srcURL), ...srcURL.searchParams])
} else if (pluginOptions.defaultDirectives) {
directives = new URLSearchParams([...pluginOptions.defaultDirectives, ...srcURL.searchParams])
Expand Down Expand Up @@ -88,7 +87,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
type: 'asset'
})

metadata.src = `__VITE_IMAGE_ASSET__${fileHandle}__`
metadata.src = `__VITE_ASSET__${fileHandle}__`
} else {
metadata.src = posix.join('/@imagetools', id)
}
Expand Down Expand Up @@ -139,32 +138,6 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin

next()
})
},

renderChunk(code) {
const assetUrlRE = /__VITE_IMAGE_ASSET__([a-z\d]{8})__(?:_(.*?)__)?/g

let match
let s
while ((match = assetUrlRE.exec(code))) {
s = s || (s = new MagicString(code))
const [full, hash, postfix = ''] = match

const file = this.getFileName(hash)

const outputFilepath = viteConfig.base + file + postfix

s.overwrite(match.index, match.index + full.length, outputFilepath)
}

if (s) {
return {
code: s.toString(),
map: viteConfig.build.sourcemap ? s.generateMap({ hires: true }) : null
}
} else {
return null
}
}
}
}
2 changes: 0 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d11b927

Please sign in to comment.