Skip to content

Commit

Permalink
feat: add hook option to control when plugin is ran (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian authored Nov 8, 2024
1 parent c3a1474 commit b9c09bd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-cougars-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-static-copy': minor
---

Allows user to optionally configure when the plugin is ran by passing in a Rollup hook name
5 changes: 3 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { copyAll, outputCopyLog } from './utils'
export const buildPlugin = ({
targets,
structured,
silent
silent,
hook
}: ResolvedViteStaticCopyOptions): Plugin => {
let config: ResolvedConfig
let output = false
Expand All @@ -20,7 +21,7 @@ export const buildPlugin = ({
// reset for watch mode
output = false
},
async writeBundle() {
async [hook]() {
// run copy only once even if multiple bundles are generated
if (output) return
output = true
Expand Down
9 changes: 8 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export type ViteStaticCopyOptions = {
*/
reloadPageOnChange?: boolean
}
/**
* Rollup hook the plugin should use during build.
* @default 'writeBundle'
*/
hook?: string
}

export type ResolvedViteStaticCopyOptions = {
Expand All @@ -115,6 +120,7 @@ export type ResolvedViteStaticCopyOptions = {
options: WatchOptions
reloadPageOnChange: boolean
}
hook: string
}

export const resolveOptions = (
Expand All @@ -126,5 +132,6 @@ export const resolveOptions = (
watch: {
options: options.watch?.options ?? {},
reloadPageOnChange: options.watch?.reloadPageOnChange ?? false
}
},
hook: options.hook ?? 'writeBundle'
})
34 changes: 34 additions & 0 deletions test/fixtures/vite.hook.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { defineConfig, normalizePath } from 'vite'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import fs from 'node:fs/promises'
import path from 'node:path'
import url from 'node:url'

const _dirname = path.dirname(url.fileURLToPath(import.meta.url))

export default defineConfig({
plugins: [
viteStaticCopy({
targets: [
{
src: 'foo.txt',
dest: 'hook1'
}
],
hook: 'generateBundle'
}),
testHookPlugin()
]
})

function testHookPlugin() {
return {
name: 'test-hook-plugin',
async writeBundle() {
const filePath = normalizePath(
path.resolve(_dirname, 'dist', 'hook1', 'foo.txt')
)
await fs.access(filePath)
}
}
}
11 changes: 11 additions & 0 deletions test/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ describe('build', () => {
}
})
}

test('should support hook option', async () => {
let result = ''
try {
await build(getConfig('vite.hook.config.ts'))
} catch (error: unknown) {
result = (error as Error).message
}
expect(result).toBe('')
})

describe('on error', () => {
test('should throw error when it does not find the file on given src', async () => {
let result = ''
Expand Down

0 comments on commit b9c09bd

Please sign in to comment.