Skip to content

Files are copied too late #70

@thepassle

Description

@thepassle

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 😊

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions