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

Watch copied files #27

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ export default function copy(options = {}) {

return {
name: 'copy',
[hook]: async () => {
async [hook]() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was necessary because the this keyword was not available with the unbound arrow function syntax.

See this issue for more details: rollup/rollup#1518 (comment)

if (copyOnce && copied) {
return
}
@@ -88,6 +88,10 @@ export default function copy(options = {}) {
}

for (const { src, dest } of copyTargets) {
try {
this.addWatchFile(src)
} catch { /* Cannot call addWatchFile after the build has finished. */ }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addWatchFile method cannot be used after the build has finished, so is not available in all hooks. Rather than hard-coding a list of valid hooks that might change, I figured I'd just try/catch it. When it throws an error, it has the error message seen in the comment


await fs.copy(src, dest, restPluginOptions)

if (verbose) {
87 changes: 87 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -228,6 +228,93 @@ describe('Copy', () => {
expect(await fs.pathExists('dist/scss-multiple/nested-renamed')).toBe(true)
expect(await fs.pathExists('dist/scss-multiple/nested-renamed/scss-3.scss')).toBe(true)
})

test('Watches copied files in pre-build hook', async () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I based these off of the copyOnce test

const watcher = watch({
input: 'src/index.js',
output: {
dir: 'build',
format: 'esm'
},
plugins: [
copy({
hook: 'buildStart',
targets: [
{ src: 'src/assets/asset-1.js', dest: 'dist' }
]
})
]
})

await sleep(1000)

expect(await fs.pathExists('dist/asset-1.js')).toBe(true)

await fs.remove('dist')

expect(await fs.pathExists('dist/asset-1.js')).toBe(false)

await replace({
files: 'src/assets/asset-1.js',
from: 'asset1',
to: 'assetX'
})

await sleep(1000)

expect(await fs.pathExists('dist/asset-1.js')).toBe(true)

watcher.close()

await replace({
files: 'src/assets/asset-1.js',
from: 'assetX',
to: 'asset1'
})
})

test('Does not watch copied files in post-build hook', async () => {
const watcher = watch({
input: 'src/index.js',
output: {
dir: 'build',
format: 'esm'
},
plugins: [
copy({
targets: [
{ src: 'src/assets/asset-1.js', dest: 'dist' }
]
})
]
})

await sleep(1000)

expect(await fs.pathExists('dist/asset-1.js')).toBe(true)

await fs.remove('dist')

expect(await fs.pathExists('dist/asset-1.js')).toBe(false)

await replace({
files: 'src/assets/asset-1.js',
from: 'asset1',
to: 'assetX'
})

await sleep(1000)

expect(await fs.pathExists('dist/asset-1.js')).toBe(false)

watcher.close()

await replace({
files: 'src/assets/asset-1.js',
from: 'assetX',
to: 'asset1'
})
})
})

describe('Options', () => {