Copy-webpack-plugin: Do not use fs to copy files; declare target files as assets to webpack

Created on 25 Jan 2016  路  9Comments  路  Source: webpack-contrib/copy-webpack-plugin

The current implementation is doing the heavy lifting of creating directory structure and copying the files. A better approach would be to simply add the target files to the webpack assets collection (e.g. html-webpack-plugin ./lib/compiler.js does just that). Leave it up to webpack to copy the assets.

With this setup, for those who want copy-webpack-plugin to work with webpack-dev-server you can use write-file-webpack-plugin.

Bug

Most helpful comment

Can you make me a contributor? I would happily with bugs as I come across them.

All 9 comments

One of the downsides of the current setup is that all files are copied upon any file change. Using the above approach in combination with write-file-webpack-plugin would leverage file hashing to detect changes and only write when needed.

Another thing to consider is that at the moment, files declared in copy-webpack-plugin do not appear in stats.compilation.assets making it hard to debug/ track copy-webpack-plugin changes.

I only use fs to write files if the to destination is an absolute path; adding to compilation.assets is the default. When you include files in compilation.assets, they're relative to the target build directory, but an absolute path may be outside the target build directory.

Also, absolute path files are copied on any file change. I check to see if there are updates before writing files to compilation.assets. I can add a check for absolute paths too.

It sounds like you're using an absolute path to describe files that are in your target build directory. I'll change it so fs is only used iff the destination is absolute and outside the target build directory. That should fix your problem.

Files are usually included in stats.compilation.assets on the first build. However, when webpack --watch is used, files appear on the first build, but subsequent builds don't show the file changes, even though only changed files are copied. I'm not sure what's going on there, I'll open another issue to track it.

I only use fs to write files if the to destination is an absolute path; adding to compilation.assets is the default. When you include files in compilation.assets, they're relative to the target build directory, but an absolute path may be outside the target build directory.

Can you not use a relative path that refers to a directory outside the build directory? Writing logic for resolving such path is pretty simple, assuming webpack allows it.

Looks like webpack does support relative paths in compilation.assets. I'll merge a fix

:+1:

stats.compilation.assets asset path should be relative to compiler.options.output.path, not process.cwd().

With current implementation, when assetPath is ../dist/index.html and output.path is /Users/gajus/Documents/dev/applaudience/showtime-dashboard/client/dist/static, the copied files ends up in /Users/gajus/Documents/dev/applaudience/showtime-dashboard/client/dist/dist/index.html.

@kevlened any chance on inspecting this last issue?

Can you make me a contributor? I would happily with bugs as I come across them.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RPDeshaies picture RPDeshaies  路  5Comments

serut picture serut  路  3Comments

alex88 picture alex88  路  4Comments

gh67uyyghj picture gh67uyyghj  路  4Comments

ietabhi picture ietabhi  路  5Comments