Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hook option to control when plugin is ran #133

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading