You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 7, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 7, 2026. It is now read-only.
[Feature] copy files after webpack has finished creating the bundles #15
I was hoping that I could use this plugin to create copies of the generated bundles to other directories. It seems however that the copying starts while webpack is creating these bundles. As a result I end up with copies of the files with no content.
Is there a way to add an option so that the copying of the files happen after webpack has created the output files?
Ok, so after digging around I'm starting to believe that there is no easy way to do this with the current version of the plugin. In writeFileToAssets the file source gets attached to compilation.assets.
The problem is, that the source may not yet be there since webpack hasn't written it to the file yet so the source will be empty. So the next thing to try would be, can we check if absFileSrc is in the assets and make it read that source? I'll post when I find out.
@tobinibot How did you manage to copy files using the webpack-shell-plugin? I tried cp path/from path/to which works when I run it in terminal, but gives an error during shell execution.
@Anima-t3d don't know if it's any use to you, but this is the script I use (obviously the paths are specific to my project):
In my webpack.config:
plugins: [
new WebpackShellPlugin({
onBuildEnd: 'node copy-to-examples.js'
})
],
And this is my copy-to-examples file:
var fs = require('fs-extra');
var source = './dist/phaser.js';
var dest = '../../phaser3-examples/public/js/phaser.js';
fs.copy(source, dest, function (err) {
if (err)
{
return console.error(err);
}
console.log('Copied to ' + dest);
});
Reacted by Anima-t3d, Joe Rinehart, Víctor Velarde, Muhammad Ubaid Raza, Víctor Gámez, hfnabhyasi and Leo
Somehow the cp command changed on me and it does not like the * in the path. I think the webpack shell plugin is wrapping each argument in quotes and so the actual commands provided in the script are not the same as if you were to execute them from the command line. To fix this I did something similar to what photonstorm did, that is, I moved my commands to a bash file and then I called bash file from the shell plugin.
Reacted by Anima-t3d, Sam Critchley, Mayrink, Josh Weir, David Parker, hfnabhyasi and Leo Fisher
@jmlopez-rod due to copying to a relative path in the parent of where I run webpack I ended up using something more similar to what @photonstorm described:
varfs=require('fs-extra');varread=require('fs-readdir-recursive');varpath=require('path');// Setup the instructions on which folders to copyvarcopyInstructions=[// copy from one src sub folder to another{label: 'Copy external scripts',source: './src/react/scripts/externals',destination: './src/app/static/r/scripts'},// copy the aggregated src sub folder to dist{label: 'Copy static to dist',source: './src/app/static/',destination: 'dist/static'}];// Copy all filescopyInstructions.map(function(instruction){const{label, source, destination}=instruction;varfiles=read(source);/*if (label) { console.log(label); }*/files.forEach(file=>{varsourcePath=source+'/'+file;vardestinationPath=destination+'/'+file;fs.copy(path.resolve(sourcePath),path.resolve(destinationPath),function(err){/*if (label) { console.log(label); }*/if(err){returnconsole.error(err);}console.log(`Copied ${file} to ${path.resolve(destinationPath)}`);});});});
I did not manage to get the path right when using the cp command.
@kevlened Is there any chance of this being added?
I'd love to be able to copy a generated file that is only created after webpack finishes its build. Right now, I can't with your plugin.
Reacted by Raitis Stengrevics, Daniel Gibbs, Adrian Geană, Ben Westrate, Anima-t3d, Radik, Evangelos Parchas, Anthony Smith, Harsh kurra, Chase Giunta and 20 more
@kevlened This feature would be great, currently my production build fails due to this plug-in looking in an empty 'dist' folder as the HtmlWebpackPlugin hasn't finished building the files. Having the ability to copy a generated file post build would be ideal.
I also needed a feature like this and created plugin that could copy, delete, and move files/directories when a build starts or when a build finishes. https://github.com/gregnb/filemanager-webpack-plugin if you're interested
Reacted by Robert Reinhard, laggingreflex, Henry Ruhs, Denis Žoljom, James Richards, 火风鼎, Hans Zhang, cypherix93, szouc, Ron and 22 more
changed the title [-]copy files after webpack has finished creating the bundles[/-][+][Feature] copy files after webpack has finished creating the bundles[/+]on Oct 5, 2017
The FileManagerPlugin replaced CleanWebpackPlugin and CopyWebpackPlugin for me! Go for it...
Reacted by Denis Žoljom, Hans Zhang, Eric McCarthy, mykhailo.romaniuk, stefanunz, Harry Mitchinson, Wouter Coebergh, gregn, Jean-Denis Vauguet, HexliaN9 and 21 more
Reacted by hfnabhyasi, 花生Peadar, manjunathgk22, Nacho Codoñer and Bassim El Bakkali El Gazuani
Reacted by Mateusz Klimek, hfnabhyasi, Jaime Gonzalez and Nacho Codoñer
I also needed a feature like this and created plugin that could copy, delete, and move files/directories when a build starts or when a build finishes. https://github.com/gregnb/filemanager-webpack-plugin if you're interested
Thank you. This is the solution for me. For any one who is asking why we need this. I have a public folder hosting on Firebase Hosting. I have a Yarn workspace which bundles the file at its dist folder (dist folder with other static files). Then, I moved the folder to public.
I have run into this a couple of times and forget how I solved it each time then re-solve it like this: "build": "webpack && cp ./dist/<file> ./<target dir>"
Just use cp in npm scripts if your use-case is simple.
Reacted by memob0x, hendrik t, Eugene Gluhotorenko, Benedek Fauszt and Elise Hein
Is this not implemented yet?
I have a setup where i zip all my files with CompressionPlugin but i want to deleteOriginalAssets after zipping.
Since CompressionPlugin has no way to exclude files or folders from deletion after zipping i tought that i copy them after the build ended but it seems i can't do that either.
Activity
jmlopez-rod commentedon Jan 29, 2016
Ok, so after digging around I'm starting to believe that there is no easy way to do this with the current version of the plugin. In
writeFileToAssetsthe file source gets attached tocompilation.assets.The problem is, that the source may not yet be there since webpack hasn't written it to the file yet so the source will be empty. So the next thing to try would be, can we check if
absFileSrcis in the assets and make it read that source? I'll post when I find out.jmlopez-rod commentedon Jan 29, 2016
So here is a solution, I added a few lines to
writeFileToAssetsso that it can check if the file is in production then to just point to that asset.Here is the whole function
Would you mind incorporating this change?
copy files in webpack stream: Issue webpack#15
tobinibot commentedon Oct 29, 2016
In case it helps anyone else, I was interested in the same functionality as @jmlopez-rod and I ended up using https://www.npmjs.com/package/webpack-shell-plugin to accomplish it.
jmlopez-rod commentedon Oct 30, 2016
@tobinibot thanks for the link, I'll be using that one for sure.
photonstorm commentedon Nov 24, 2016
@tobinibot thanks, I too hit this exact problem, and am now using the shell plugin instead.
Anima-t3d commentedon Jan 11, 2017
@tobinibot How did you manage to copy files using the webpack-shell-plugin? I tried
cp path/from path/towhich works when I run it in terminal, but gives an error during shell execution.photonstorm commentedon Jan 11, 2017
@Anima-t3d don't know if it's any use to you, but this is the script I use (obviously the paths are specific to my project):
In my webpack.config:
And this is my
copy-to-examplesfile:jmlopez-rod commentedon Jan 11, 2017
@Anima-t3d I have a similar thing as @photonstorm but I use
onBuildExit:cp: http://man7.org/linux/man-pages/man1/cp.1.htmlEDIT:
Somehow the
cpcommand changed on me and it does not like the*in the path. I think the webpack shell plugin is wrapping each argument in quotes and so the actual commands provided in the script are not the same as if you were to execute them from the command line. To fix this I did something similar to what photonstorm did, that is, I moved my commands to a bash file and then I called bash file from the shell plugin.Anima-t3d commentedon Jan 13, 2017
@jmlopez-rod due to copying to a relative path in the parent of where I run webpack I ended up using something more similar to what @photonstorm described:
I did not manage to get the path right when using the cp command.
JeffreyWay commentedon Mar 9, 2017
@kevlened Is there any chance of this being added?
I'd love to be able to copy a generated file that is only created after webpack finishes its build. Right now, I can't with your plugin.
danchristian commentedon Jun 23, 2017
@kevlened This feature would be great, currently my production build fails due to this plug-in looking in an empty 'dist' folder as the HtmlWebpackPlugin hasn't finished building the files. Having the ability to copy a generated file post build would be ideal.
gregnb commentedon Sep 2, 2017
I also needed a feature like this and created plugin that could copy, delete, and move files/directories when a build starts or when a build finishes. https://github.com/gregnb/filemanager-webpack-plugin if you're interested
[-]copy files after webpack has finished creating the bundles[/-][+][Feature] copy files after webpack has finished creating the bundles[/+]henryruhs commentedon Oct 20, 2017
The
FileManagerPluginreplacedCleanWebpackPluginandCopyWebpackPluginfor me! Go for it...alexander-akait commentedon Feb 22, 2018
Out of scope this plugin, please use
FileManagerPluginor just run npm task after buildairtonix commentedon May 2, 2018
FileManagerPlugin is broken too.
LiMeii commentedon Aug 14, 2018
@evilebottnawi thanks! you save my day!!!
alvinhtml commentedon Apr 1, 2019
@evilebottnawi thanks!
Antoine-360pixel commentedon Apr 16, 2019
For me, filter files to change output work fine...
Hope help you for simple cases...
anhnt-mageplaza commentedon Mar 25, 2020
Thank you. This is the solution for me. For any one who is asking why we need this. I have a public folder hosting on Firebase Hosting. I have a Yarn workspace which bundles the file at its dist folder (dist folder with other static files). Then, I moved the folder to public.
jasonmorita commentedon Apr 17, 2020
I have run into this a couple of times and forget how I solved it each time then re-solve it like this:
"build": "webpack && cp ./dist/<file> ./<target dir>"Just use
cpin npm scripts if your use-case is simple.zekageri commentedon Sep 12, 2023
Is this not implemented yet?
I have a setup where i zip all my files with CompressionPlugin but i want to deleteOriginalAssets after zipping.
Since CompressionPlugin has no way to exclude files or folders from deletion after zipping i tought that i copy them after the build ended but it seems i can't do that either.