Skip to content

Commit

Permalink
feat: Add hook option to control when plugin is ran
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Nov 4, 2024
1 parent c3a1474 commit 145ea55
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
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'
})
32 changes: 32 additions & 0 deletions test/fixtures/vite.hook.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { defineConfig, normalizePath } from 'vite'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import { promises as fs } from 'node:fs'
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);
}
}
}
9 changes: 9 additions & 0 deletions test/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ describe('build', () => {
}
})
}

describe('hook', () => {
test('should support hook option', async () => {
expect(

Check failure on line 143 in test/tests.test.ts

View workflow job for this annotation

GitHub Actions / run lint

Expected an assignment or function call and instead saw an expression
await build(getConfig('vite.hook.config.ts'))
).to.not.throw

Check failure on line 145 in test/tests.test.ts

View workflow job for this annotation

GitHub Actions / run lint

Matchers must be called to assert
})
})

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 145ea55

Please sign in to comment.