Skip to content

Commit af9bdf9

Browse files
authored
fix: use rollup's addWatchFile API to watch target sources
fixes vladshcherbin#5 closes vladshcherbin#30
1 parent 1fa1c25 commit af9bdf9

File tree

1 file changed

+37
-47
lines changed

1 file changed

+37
-47
lines changed

src/index.js

+37-47
Original file line numberDiff line numberDiff line change
@@ -57,84 +57,74 @@ export default function copy(options = {}) {
5757

5858
return {
5959
name: 'copy',
60-
[hook]: async () => {
61-
if (copyOnce && copied) {
62-
return
63-
}
64-
65-
const copyTargets = []
66-
67-
if (Array.isArray(targets) && targets.length) {
60+
buildStart() {
61+
if (Array.isArray(targets)) {
6862
for (const target of targets) {
6963
if (!isObject(target)) {
70-
throw new Error(`${stringify(target)} target must be an object`)
64+
throw new Error(`${stringify(target)} target must be an object`);
7165
}
7266

73-
const { dest, rename, src, transform, ...restTargetOptions } = target
67+
const {
68+
src,
69+
dest,
70+
rename,
71+
...restTargetOptions
72+
} = target;
7473

7574
if (!src || !dest) {
76-
throw new Error(`${stringify(target)} target must have "src" and "dest" properties`)
75+
throw new Error(`${stringify(target)} target must have "src" and "dest" properties`);
7776
}
7877

7978
if (rename && typeof rename !== 'string' && typeof rename !== 'function') {
80-
throw new Error(`${stringify(target)} target's "rename" property must be a string or a function`)
79+
throw new Error(`${stringify(target)} target's "rename" property must be a string or a function`);
8180
}
8281

83-
const matchedPaths = await globby(src, {
82+
const matchedPaths = globby.sync(src, {
8483
expandDirectories: false,
8584
onlyFiles: false,
8685
...restPluginOptions,
8786
...restTargetOptions
88-
})
87+
});
8988

9089
if (matchedPaths.length) {
91-
for (const matchedPath of matchedPaths) {
92-
const generatedCopyTargets = Array.isArray(dest)
93-
? await Promise.all(dest.map((destination) => generateCopyTarget(
94-
matchedPath,
95-
destination,
96-
{ flatten, rename, transform }
97-
)))
98-
: [await generateCopyTarget(matchedPath, dest, { flatten, rename, transform })]
99-
100-
copyTargets.push(...generatedCopyTargets)
101-
}
90+
matchedPaths.forEach(matchedPath => {
91+
const generatedCopyTargets = Array.isArray(dest) ? dest.map(destination => generateCopyTarget(matchedPath, destination, rename)) : [generateCopyTarget(matchedPath, dest, rename)];
92+
copyTargets.push(...generatedCopyTargets);
93+
});
10294
}
10395
}
10496
}
10597

98+
const targetSources = copyTargets.map(({ src }) => path.resolve(src));
99+
targetSources.forEach(target => {
100+
this.addWatchFile(target);
101+
});
102+
},
103+
104+
[hook]: async () => {
105+
if (copyOnce && copied) {
106+
return;
107+
}
108+
106109
if (copyTargets.length) {
107110
if (verbose) {
108-
console.log(green('copied:'))
111+
console.log(green('copied:'));
109112
}
110113

111-
for (const copyTarget of copyTargets) {
112-
const { contents, dest, src, transformed } = copyTarget
113-
114-
if (transformed) {
115-
await fs.outputFile(dest, contents, restPluginOptions)
116-
} else {
117-
await fs.copy(src, dest, restPluginOptions)
118-
}
114+
for (const {
115+
src,
116+
dest
117+
} of copyTargets) {
118+
await fs.copy(src, dest, restPluginOptions);
119119

120120
if (verbose) {
121-
let message = green(` ${bold(src)}${bold(dest)}`)
122-
const flags = Object.entries(copyTarget)
123-
.filter(([key, value]) => ['renamed', 'transformed'].includes(key) && value)
124-
.map(([key]) => key.charAt(0).toUpperCase())
125-
126-
if (flags.length) {
127-
message = `${message} ${yellow(`[${flags.join(', ')}]`)}`
128-
}
129-
130-
console.log(message)
121+
console.log(green(` ${bold(src)}${bold(dest)}`));
131122
}
132123
}
133124
} else if (verbose) {
134-
console.log(yellow('no items to copy'))
125+
console.log(yellow('no items to copy'));
135126
}
136127

137-
copied = true
128+
copied = true;
138129
}
139-
}
140130
}

0 commit comments

Comments
 (0)