Skip to content

Commit 7c23ad8

Browse files
schplittsxzz
andauthored
feat(copy): add support for watching copy source files (#721)
* feat: add support for watching copy source files * refactor: move shared logic to `resolveCopyEntries` * refactor * refactor * fix --------- Co-authored-by: Kevin Deng <sxzz@sxzz.moe>
1 parent f182324 commit 7c23ad8

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
lines changed

src/features/copy.ts

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,37 @@ export interface CopyEntry {
3636
export type CopyOptions = Arrayable<string | CopyEntry>
3737
export type CopyOptionsFn = (options: ResolvedConfig) => Awaitable<CopyOptions>
3838

39+
type ResolvedCopyEntry = CopyEntry & { from: string; to: string }
40+
3941
export async function copy(options: ResolvedConfig): Promise<void> {
4042
if (!options.copy) return
4143

44+
const resolved = await resolveCopyEntries(options)
45+
await Promise.all(
46+
resolved.map(({ from, to, verbose }) => {
47+
if (verbose) {
48+
options.logger.info(
49+
options.nameLabel,
50+
`Copying files from ${path.relative(options.cwd, from)} to ${path.relative(
51+
options.cwd,
52+
to,
53+
)}`,
54+
)
55+
}
56+
return fsCopy(from, to)
57+
}),
58+
)
59+
}
60+
61+
export async function resolveCopyEntries(
62+
options: ResolvedConfig,
63+
): Promise<ResolvedCopyEntry[]> {
4264
const copy = toArray(
4365
typeof options.copy === 'function'
4466
? await options.copy(options)
4567
: options.copy,
4668
)
47-
if (!copy.length) return
69+
if (!copy.length) return []
4870

4971
const resolved = (
5072
await Promise.all(
@@ -63,52 +85,44 @@ export async function copy(options: ResolvedConfig): Promise<void> {
6385
})
6486
}
6587

66-
return from.map((file) => resolveCopyEntry({ ...entry, from: file }))
88+
return from.map((file) =>
89+
resolveCopyEntry(
90+
{ ...entry, from: file },
91+
options.cwd,
92+
options.outDir,
93+
),
94+
)
6795
}),
6896
)
6997
).flat()
7098

7199
if (!resolved.length) {
72100
options.logger.warn(options.nameLabel, `No files matched for copying.`)
73-
return
74101
}
75102

76-
await Promise.all(
77-
resolved.map(({ from, to, verbose }) => {
78-
if (verbose) {
79-
options.logger.info(
80-
options.nameLabel,
81-
`Copying files from ${path.relative(options.cwd, from)} to ${path.relative(
82-
options.cwd,
83-
to,
84-
)}`,
85-
)
86-
}
87-
return fsCopy(from, to)
88-
}),
89-
)
103+
return resolved
104+
}
90105

91-
// https://github.com/vladshcherbin/rollup-plugin-copy/blob/master/src/index.js
92-
// MIT License
93-
function resolveCopyEntry(
94-
entry: CopyEntry & { from: string },
95-
): CopyEntry & { from: string; to: string } {
96-
const { flatten = true, rename } = entry
97-
const from = path.resolve(options.cwd, entry.from)
98-
const to = entry.to ? path.resolve(options.cwd, entry.to) : options.outDir
106+
// https://github.com/vladshcherbin/rollup-plugin-copy/blob/master/src/index.js
107+
// MIT License
108+
function resolveCopyEntry(
109+
entry: CopyEntry & { from: string },
110+
cwd: string,
111+
outDir: string,
112+
): CopyEntry & { from: string; to: string } {
113+
const { flatten = true, rename } = entry
114+
const from = path.resolve(cwd, entry.from)
115+
const to = entry.to ? path.resolve(cwd, entry.to) : outDir
99116

100-
const { base, dir } = path.parse(path.relative(options.cwd, from))
101-
const destFolder =
102-
flatten || (!flatten && !dir)
103-
? to
104-
: dir.replace(dir.split(path.sep)[0], to)
105-
const dest = path.join(
106-
destFolder,
107-
rename ? renameTarget(base, rename, from) : base,
108-
)
117+
const { base, dir } = path.parse(path.relative(cwd, from))
118+
const destFolder =
119+
flatten || (!flatten && !dir) ? to : dir.replace(dir.split(path.sep)[0], to)
120+
const dest = path.join(
121+
destFolder,
122+
rename ? renameTarget(base, rename, from) : base,
123+
)
109124

110-
return { ...entry, from, to: dest }
111-
}
125+
return { ...entry, from, to: dest }
112126
}
113127

114128
function renameTarget(

src/features/watch.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { addOutDirToChunks } from '../utils/chunks.ts'
22
import { resolveComma, toArray } from '../utils/general.ts'
3+
import { resolveCopyEntries } from './copy.ts'
34
import type { TsdownBundle } from '../config/types.ts'
45
import type { Plugin } from 'rolldown'
56

@@ -19,7 +20,7 @@ export function WatchPlugin(
1920
inputOptions.watch.exclude.push(...config.ignoreWatch)
2021
}
2122
: undefined,
22-
buildStart() {
23+
async buildStart() {
2324
config.tsconfig && this.addWatchFile(config.tsconfig)
2425
for (const file of configFiles) {
2526
this.addWatchFile(file)
@@ -32,6 +33,15 @@ export function WatchPlugin(
3233
if (config.pkg) {
3334
this.addWatchFile(config.pkg.packageJsonPath)
3435
}
36+
37+
// Watch copy source files
38+
if (config.copy) {
39+
const resolvedEntries = await resolveCopyEntries(config)
40+
41+
for (const entry of resolvedEntries) {
42+
this.addWatchFile(entry.from)
43+
}
44+
}
3545
},
3646
generateBundle: {
3747
order: 'post',

0 commit comments

Comments
 (0)