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 a new option to the watch, to copy files to dest on change #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export type ViteStaticCopyOptions = {
* @default false
*/
reloadPageOnChange?: boolean
/**
* Copy files to the dist on change
* @default false
*/
copyToDest?: boolean
}
}

Expand All @@ -96,6 +101,7 @@ export type ResolvedViteStaticCopyOptions = {
watch: {
options: WatchOptions
reloadPageOnChange: boolean
copyToDest: boolean
}
}

Expand All @@ -106,6 +112,7 @@ export const resolveOptions = (
flatten: options.flatten ?? true,
watch: {
options: options.watch?.options ?? {},
reloadPageOnChange: options.watch?.reloadPageOnChange ?? false
reloadPageOnChange: options.watch?.reloadPageOnChange ?? false,
copyToDest: options.watch?.copyToDest ?? false
}
})
85 changes: 82 additions & 3 deletions src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
collectCopyTargets,
updateFileMapFromTargets,
outputCollectedLog,
formatConsole
formatConsole,
copyAll,
copyFile,
removeFile,
outputCopyLog
} from './utils'
import { debounce } from 'throttle-debounce'
import chokidar from 'chokidar'
Expand Down Expand Up @@ -56,6 +60,14 @@ export const servePlugin = ({
ws.send({ type: 'full-reload', path: '*' })
}

if (watch.copyToDest) {
copyAll(config.root, config.build.outDir, targets, flatten).then(
copyCount => {
outputCopyLog(config.logger, copyCount)
}
)
}

// cannot use server.watcher since disableGlobbing is true
watcher = chokidar.watch(
targets.flatMap(target => target.src),
Expand All @@ -73,11 +85,78 @@ export const servePlugin = ({
}
)
await collectFileMapDebounce()
if (watch.reloadPageOnChange) {
if (watch.copyToDest) {
const dest = await copyFile(
path,
config.root,
config.build.outDir,
targets,
flatten
)
config.logger.info(
formatConsole(
`${pc.green('file added and copied')} ${path} ${pc.green(
'->'
)} ${dest.join(pc.green(', '))}`
),
{
timestamp: true
}
)
if (watch.reloadPageOnChange) {
reloadPage()
}
}
if (watch.reloadPageOnChange && !watch.copyToDest) {
reloadPage()
}
})
if (watch.reloadPageOnChange) {
if (watch.copyToDest) {
watcher.on('change', async path => {
const dest = await copyFile(
path,
config.root,
config.build.outDir,
targets,
flatten
)
if (watch.reloadPageOnChange) {
reloadPage()
}
config.logger.info(
formatConsole(
`${pc.green('file changed and copied')} ${path} ${pc.green(
'->'
)} ${dest.join(pc.green(', '))}`
),
{
timestamp: true
}
)
})
watcher.on('unlink', async path => {
const dest = await removeFile(
path,
config.root,
config.build.outDir,
targets,
flatten
)
if (dest.length) {
config.logger.info(
formatConsole(
`${pc.green('file deleted and removed')} ${path} ${pc.green(
'and'
)} ${dest.join(pc.green(', '))}`
),
{
timestamp: true
}
)
}
})
}
if (watch.reloadPageOnChange && !watch.copyToDest) {
watcher.on('change', path => {
config.logger.info(
formatConsole(`${pc.green('file changed')} ${path}`),
Expand Down
70 changes: 70 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,76 @@ export const copyAll = async (
return copyTargets.length
}

export const copyFile = async (
file: string,
rootSrc: string,
rootDest: string,
targets: Target[],
flatten: boolean
) => {
const copyTargets = await collectCopyTargets(rootSrc, targets, flatten)
const changedFiles: string[] = []
for (const copyTarget of copyTargets) {
const { src, dest, transform, preserveTimestamps, dereference } = copyTarget
const relative = path.relative(file, src)
if (
relative.length == 0 ||
(relative.startsWith('..') && relative.endsWith('..'))
) {
const resolvedSrc = path.resolve(rootSrc, file)
let resolvedDest = path.resolve(rootSrc, rootDest, dest)
const transformOption = resolveTransformOption(transform)
if (fs.statSync(resolvedDest).isDirectory()) {
resolvedDest = resolvedDest + '/' + path.relative(src, file)
}

if (transformOption) {
await transformCopy(transformOption, resolvedSrc, resolvedDest)
} else {
await fs.copy(resolvedSrc, resolvedDest, {
preserveTimestamps,
dereference
})
}

changedFiles.push(
path.normalize(rootDest + '/' + dest + '/' + path.relative(src, file))
)
}
}
return changedFiles
}

export const removeFile = async (
file: string,
rootSrc: string,
rootDest: string,
targets: Target[],
flatten: boolean
) => {
const copyTargets = await collectCopyTargets(rootSrc, targets, flatten)
const changedFiles: string[] = []
for (const copyTarget of copyTargets) {
const { src, dest } = copyTarget
const relative = path.relative(file, src)
if (
relative.length == 0 ||
(relative.startsWith('..') && relative.endsWith('..'))
) {
let resolvedDest = path.resolve(rootSrc, rootDest, dest)
if (fs.statSync(resolvedDest).isDirectory()) {
resolvedDest = resolvedDest + '/' + path.relative(src, file)
}
await fs.remove(resolvedDest)

changedFiles.push(
path.normalize(rootDest + '/' + dest + '/' + path.relative(src, file))
)
}
}
return changedFiles
}

export const updateFileMapFromTargets = (
targets: SimpleTarget[],
fileMap: FileMap
Expand Down