paulmelnikow / rollup-plugin-cpy

Rollup plugin to easily copy files and folders
MIT License
7 stars 1 forks source link

Files are copied too late #70

Open thepassle opened 4 years ago

thepassle commented 4 years ago

Hi,

We're running into an issue when using rollup-plugin-cpy with rollup-plugin-workbox. The workbox plugin will run at the writeBundle hook, and so does the cpy plugin. The problem here is that if it takes longer for the cpy plugin to copy files, workbox will be done before files are copied, meaning that workbox will not add some files to the precache manifest.

See some example code here:

module.exports = {
  input: 'index.js',
  output: {
    format: 'esm',
    dir: 'dist'
  },
  plugins: [
    copy({
      files: 'img/*.png',
      dest: 'dist',
    }),
    injectManifest({
      swDest: path.join(__dirname, 'dist', 'sw.js'),
      swSrc: path.join(__dirname, 'sw.js'),
      globDirectory: path.join(__dirname, 'dist'),
      globPatterns: ['**/*.{html,js,css,png}'],
    }),
  ],
};

Result:

The service worker file was written to /Users/au87xu/rollup-workbox-img-bug/dist/sw.js
The service worker will precache 1 URLs, totaling 26 B.

Only 1 URL is precached, because workbox finishes before the files are copied. I'd expect 2 URLS to be precached; dist/index.js and dist/img.png.

As I understand correctly from reading the source code of rollup-plugin-cpy, nothing in rollup-plugin-cpy depends on anything during the entire rollup process, all it does is simply copy files from a source directory, to the build directory.

This means that we could potentially solve this problem by starting the copy process to the buildStart hook, so we have a guarantee that the files will be copied by the time the writeBundle hook starts, because (from my understanding) rollup will await the buildStart hooks before moving on.

This would mean a small code change on line 44:

+     async writeBundle() {
-      async buildStart() {

Would love to hear your thoughts about this, i'd be happy to make a PR 😊

thepassle commented 4 years ago

Update: I see https://github.com/vladshcherbin/rollup-plugin-copy#hook actually lets you specify the hook where you want to copy the files, which would be an even nicer solution.