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: introduce silent option #52

Merged
merged 3 commits into from
Apr 19, 2023
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: 3 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { copyAll, outputCopyLog } from './utils'

export const buildPlugin = ({
targets,
flatten
flatten,
silent
}: ResolvedViteStaticCopyOptions): Plugin => {
let config: ResolvedConfig
let copyCount: number | undefined
Expand All @@ -24,7 +25,7 @@ export const buildPlugin = ({
)
},
closeBundle() {
outputCopyLog(config.logger, copyCount)
if (!silent) outputCopyLog(config.logger, copyCount)
}
}
}
7 changes: 7 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export type ViteStaticCopyOptions = {
* @default true
*/
flatten?: boolean
/**
* Suppress console output.
* @default false
*/
silent?: boolean
watch?: {
/**
* Watch options
Expand All @@ -93,6 +98,7 @@ export type ViteStaticCopyOptions = {
export type ResolvedViteStaticCopyOptions = {
targets: Target[]
flatten: boolean
silent: boolean
watch: {
options: WatchOptions
reloadPageOnChange: boolean
Expand All @@ -104,6 +110,7 @@ export const resolveOptions = (
): ResolvedViteStaticCopyOptions => ({
targets: options.targets,
flatten: options.flatten ?? true,
silent: options.silent ?? false,
watch: {
options: options.watch?.options ?? {},
reloadPageOnChange: options.watch?.reloadPageOnChange ?? false
Expand Down
57 changes: 33 additions & 24 deletions src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export type FileMap = Map<string, FileMapValue[]>
export const servePlugin = ({
targets,
flatten,
watch
watch,
silent
}: ResolvedViteStaticCopyOptions): Plugin => {
let config: ResolvedConfig
let watcher: chokidar.FSWatcher
Expand Down Expand Up @@ -66,43 +67,51 @@ export const servePlugin = ({
}
)
watcher.on('add', async path => {
config.logger.info(
formatConsole(`${pc.green('detected new file')} ${path}`),
{
timestamp: true
}
)
if (!silent) {
config.logger.info(
formatConsole(`${pc.green('detected new file')} ${path}`),
{
timestamp: true
}
)
}
await collectFileMapDebounce()
if (watch.reloadPageOnChange) {
reloadPage()
}
})
if (watch.reloadPageOnChange) {
watcher.on('change', path => {
config.logger.info(
formatConsole(`${pc.green('file changed')} ${path}`),
{
timestamp: true
}
)
if (!silent) {
config.logger.info(
formatConsole(`${pc.green('file changed')} ${path}`),
{
timestamp: true
}
)
}
reloadPage()
})
watcher.on('unlink', path => {
config.logger.info(
formatConsole(`${pc.green('file deleted')} ${path}`),
{
timestamp: true
}
)
if (!silent) {
config.logger.info(
formatConsole(`${pc.green('file deleted')} ${path}`),
{
timestamp: true
}
)
}
reloadPage()
})
}

httpServer?.once('listening', () => {
setTimeout(() => {
outputCollectedLog(config.logger, fileMap)
}, 0)
})
if (!silent) {
httpServer?.once('listening', () => {
setTimeout(() => {
outputCollectedLog(config.logger, fileMap)
}, 0)
})
}

return () => {
// insert serveStaticCopyMiddleware before transformMiddleware
Expand Down