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

In 6.x files are transformed/copied multiple times per build #505

Closed
brendon opened this issue Jun 15, 2020 · 11 comments · Fixed by #507
Closed

In 6.x files are transformed/copied multiple times per build #505

brendon opened this issue Jun 15, 2020 · 11 comments · Fixed by #507

Comments

@brendon
Copy link

brendon commented Jun 15, 2020

  • Operating System: MacOS 10.15.5 (19F101)
  • Node Version: v14.4.0
  • NPM Version: N/A (Yarn 1.22.4)
  • webpack Version: [email protected]
  • copy-webpack-plugin Version: 6.x

Expected Behavior

Files should be transformed/copied only once per build

Actual Behavior

In 5.x the files are transformed once per build (verified by logging when transform is called). In 6.x the transform function is called a large number of times (perhaps 6-10 times per file.

I noticed that removing new VueLoaderPlugin() from the plugins list reduced the multiple calls down to two per file. My suspicion is that in 6.x you're hooking into an event that runs even on child builds rather than on the entry point build. new VueLoaderPlugin() seems to kick off a lot of child builds depending on the number of Vue components in the application.

Reverting to 5.x fixed the issue.

Code

// webpack.config.js

...
transform(content, filePath) {
  console.log(filePath)
}
...
new VueLoaderPlugin()

How Do We Reproduce?

The only hint I have is to do with new VueLoaderPlugin(). Hopefully this is enough of a pointer, but if not, let me know and I'll look into creating a minimal reproducible.

@brendon
Copy link
Author

brendon commented Jun 16, 2020

This seems to be the likely cause:

https://github.com/webpack-contrib/copy-webpack-plugin/pull/459/files#diff-1fdf421c05c1140f6d71444ea2b27638L25-L27

Switching from tapping into emit that one assumes only gets triggered once, to tapping into compilation that presumably runs for each master and child build.

@alexander-akait
Copy link
Member

alexander-akait commented Jun 16, 2020

In 5.x the files are transformed once per build (verified by logging when transform is called). In 6.x the transform function is called a large number of times (perhaps 6-10 times per file.

No, emit and addtionalAssets hooks called on each recompilation

Changing hook was written in CHANGELOG, without it, the plugin does not work correctly

@alexander-akait
Copy link
Member

In webpack@5, using 5 version of copy-webpack-plugin was output invalid result and will be broken

@brendon
Copy link
Author

brendon commented Jun 16, 2020

Please checkout this repo: https://github.com/brendon/broken-copy

run yarn install then yarn build and you'll see that the files are transformed/copied multiple times per build (I'm logging to the console when transform is executed). The reason for this is that MiniCssExtractPlugin creates a compilation for each component's styles.

I'd instinctively expect CopyWebpackPlugin to copy just once per compile. With your update it looks like you're telling it to copy once per compilation of which there can be many per compile.

What would be the way forward on this one?

@alexander-akait
Copy link
Member

alexander-akait commented Jun 16, 2020

I'd instinctively expect CopyWebpackPlugin to copy just once per compile. With your update it looks like you're telling it to copy once per compilation of which there can be many per compile.

Because each compilation requires all assets

What is a problem? You may need an asset inside MiniCssExtractPlugin (not only for this plugin), there are a lot of use cases. You can enable cache for transform.

@brendon
Copy link
Author

brendon commented Jun 16, 2020

Ok, turning on caching fixed the problem in this test repo, but I didn't experience the same result in my actual application. I returned a promise from transform for each file (processing JPEGs and PNGs with tinify).

I tried to simulate a delay with a setTimeout but it operates correctly (the first transforms of each file are waited before checking the cache for subsequent transforms):

    new CopyWebpackPlugin({
      patterns: [{
        from: `public`,
        transform(content, filePath) {
          console.log(filePath)
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              resolve(content)
            }, 5000)
          })
        },
        cacheTransform: true
      }]
    })

I must have been doing something wrong. I'll revisit this in my own code now that I understand how it works a bit more deeply.

Thank you for your time :)

@alexander-akait
Copy link
Member

Oh, my mistake, I think it is a problem, anyway cache is good idea here too

@brendon
Copy link
Author

brendon commented Jun 17, 2020

Definitely. I'll have another go with 6.x now to see if the problem persists.

What do you think the problem is? :)

@brendon
Copy link
Author

brendon commented Jun 18, 2020

I've had another go and it doesn't wait for the promises to resolve before checking the cache for the next iteration. I could be misunderstanding the method I'm using as I don't understand TypeScript in this scenario:

https://github.com/tinify/tinify-nodejs/blob/master/src/tinify/Result.ts#L37

But I assume it means that it'll return a promise because that's in the method definition and also this._data in that code is a promise.

@alexander-akait
Copy link
Member

@brendon yes, it is return promise

@brendon
Copy link
Author

brendon commented Jun 18, 2020

Thanks @evilebottnawi, very much appreciated! :)

clydin added a commit to clydin/angular-cli that referenced this issue Jun 30, 2020
Prior versions of copy-webpack-plugin 6.x contained a memory usage regression that could cause gigabytes of memory to be used during a build.  Version 6.0.3 contains a fix that prevents this issue.
Referenced Issue: webpack-contrib/copy-webpack-plugin#505
clydin added a commit to clydin/angular-cli that referenced this issue Jun 30, 2020
Prior versions of copy-webpack-plugin 6.x contained a memory usage regression that could cause gigabytes of memory to be used during a build.  Version 6.0.3 contains a fix that prevents this issue.
Referenced Issue: webpack-contrib/copy-webpack-plugin#505

(cherry picked from commit 23ae457)
filipesilva pushed a commit to angular/angular-cli that referenced this issue Jul 1, 2020
Prior versions of copy-webpack-plugin 6.x contained a memory usage regression that could cause gigabytes of memory to be used during a build.  Version 6.0.3 contains a fix that prevents this issue.
Referenced Issue: webpack-contrib/copy-webpack-plugin#505

(cherry picked from commit 23ae457)
filipesilva pushed a commit to angular/angular-cli that referenced this issue Jul 1, 2020
Prior versions of copy-webpack-plugin 6.x contained a memory usage regression that could cause gigabytes of memory to be used during a build.  Version 6.0.3 contains a fix that prevents this issue.
Referenced Issue: webpack-contrib/copy-webpack-plugin#505
clydin added a commit to clydin/angular-cli that referenced this issue Jul 1, 2020
Prior versions of copy-webpack-plugin 6.x contained a memory usage regression that could cause gigabytes of memory to be used during a build.  Version 6.0.3 contains a fix that prevents this issue.
Referenced Issue: webpack-contrib/copy-webpack-plugin#505

(cherry picked from commit 78064a8)
mgechev pushed a commit to angular/angular-cli that referenced this issue Jul 1, 2020
Prior versions of copy-webpack-plugin 6.x contained a memory usage regression that could cause gigabytes of memory to be used during a build.  Version 6.0.3 contains a fix that prevents this issue.
Referenced Issue: webpack-contrib/copy-webpack-plugin#505

(cherry picked from commit 78064a8)
villelahdenvuo pushed a commit to villelahdenvuo/angular-cli that referenced this issue Jul 6, 2020
Prior versions of copy-webpack-plugin 6.x contained a memory usage regression that could cause gigabytes of memory to be used during a build.  Version 6.0.3 contains a fix that prevents this issue.
Referenced Issue: webpack-contrib/copy-webpack-plugin#505
alan-agius4 added a commit to angular/angular-cli that referenced this issue Jul 6, 2020
Prior versions of copy-webpack-plugin 6.x contained a memory usage regression that could cause gigabytes of memory to be used during a build. Version 6.0.3 contains a fix that prevents this issue.
Referenced Issue: webpack-contrib/copy-webpack-plugin#505

8.3.x variant of #18088
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants