Skip to content

Commit 5251e42

Browse files
committed
Add copySync option
1 parent b34e8ba commit 5251e42

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ interface CopyOptions extends globby.GlobbyOptions, fs.WriteFileOptions, fs.Copy
3131
*/
3232
readonly copyOnce?: boolean;
3333

34+
/**
35+
* Copy items synchronous.
36+
* @default false
37+
*/
38+
readonly copySync?: boolean;
39+
3440
/**
3541
* Remove the directory structure of copied files.
3642
* @default true

readme.md

+13
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ copy({
184184
targets: [{ src: 'assets/*', dest: 'dist/public' }],
185185
copyOnce: true
186186
})
187+
188+
```
189+
#### copySync
190+
191+
Type: `boolean` | Default: `false`
192+
193+
Copy items synchronous.
194+
195+
```js
196+
copy({
197+
targets: [{ src: 'assets/*', dest: 'dist/public' }],
198+
copySync: true
199+
})
187200
```
188201

189202
#### flatten

src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ async function generateCopyTarget(src, dest, { flatten, rename, transform }) {
4646
export default function copy(options = {}) {
4747
const {
4848
copyOnce = false,
49+
copySync = false,
4950
flatten = true,
5051
hook = 'buildEnd',
5152
targets = [],
@@ -113,8 +114,10 @@ export default function copy(options = {}) {
113114

114115
if (transformed) {
115116
await fs.outputFile(dest, contents, restPluginOptions)
117+
} else if (!copySync) {
118+
await fs.copy(src, dest, restPluginOptions)
116119
} else {
117-
await fs.copySync(src, dest, restPluginOptions)
120+
fs.copySync(src, dest, restPluginOptions)
118121
}
119122

120123
if (verbose) {

tests/index.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,22 @@ describe('Options', () => {
431431
})
432432
})
433433

434+
test('Copy sync', async () => {
435+
await build({
436+
targets: [{
437+
src: [
438+
'src/assets/asset-1.js',
439+
'src/assets/asset-2.js'
440+
],
441+
dest: 'dist'
442+
}],
443+
copySync: true
444+
})
445+
446+
expect(await fs.pathExists('dist/asset-1.js')).toBe(true)
447+
expect(await fs.pathExists('dist/asset-2.js')).toBe(true)
448+
})
449+
434450
test('Flatten', async () => {
435451
await build({
436452
targets: [{

0 commit comments

Comments
 (0)