Skip to content

Commit

Permalink
fix: allow async output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodevx committed Feb 4, 2023
1 parent e1ea30e commit 3baa612
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cool-books-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-imagetools': patch
---

fix: allow async output formats
26 changes: 26 additions & 0 deletions packages/vite/src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,32 @@ describe('vite-imagetools', () => {
expect(window.__IMAGE__).toBe('/assets/with-metadata-404f605d.png 600w')
})

test('async output format', async () => {
const bundle = (await build({
root: join(__dirname, '__fixtures__'),
logLevel: 'warn',
build: { write: false },
plugins: [
testEntry(`
import Image from "./with-metadata.png?run"
window.__IMAGE__ = Image
`),
imagetools({
extendOutputFormats: (defaults) => ({
...defaults,
run: () => () => new Promise((resolve) => setTimeout(() => resolve('success'), 500))
})
})
]
})) 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('success')
})

describe('utils', () => {
test('createBasePath', () => {
expect(createBasePath('')).toBe('/@imagetools/')
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
}
}

return dataToEsm(outputFormat(outputMetadatas), {
return dataToEsm(await outputFormat(outputMetadatas), {
namedExports: viteConfig.json?.namedExports ?? true,
compact: !!viteConfig.build.minify ?? false,
preferConst: true
Expand Down

0 comments on commit 3baa612

Please sign in to comment.