-
-
Notifications
You must be signed in to change notification settings - Fork 283
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
[Feature] copy files after webpack has finished creating the bundles #15
Comments
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 compilation.assets[relFileDest] = {
size: function() {
return stat.size;
},
source: function() {
return fs.readFileSync(absFileSrc);
}
}; 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 |
So here is a solution, I added a few lines to Here is the whole function function writeFileToAssets(opts) {
var compilation = opts.compilation;
var relFileDest = opts.relFileDest;
var absFileSrc = opts.absFileSrc;
var forceWrite = opts.forceWrite;
var lastGlobalUpdate = opts.lastGlobalUpdate;
if (compilation.assets[relFileDest] && !forceWrite) {
return Promise.resolve();
}
var currentAssets = _.keys(compilation.assets);
var inProduction = _.find(currentAssets, function(name) {
return _.endsWith(absFileSrc, name);
});
return fs.statAsync(absFileSrc)
.then(function(stat) {
if (compilation.assets[inProduction]) {
compilation.assets[relFileDest] = compilation.assets[inProduction];
} else if (stat.mtime.getTime() > lastGlobalUpdate) {
compilation.assets[relFileDest] = {
size: function() {
return stat.size;
},
source: function() {
return fs.readFileSync(absFileSrc);
}
};
}
});
} Would you mind incorporating this change? |
Checking if the file we are trying to copy is one of the current assets in webpack. If this is the case then we set the source of file to copy to the one already in the assets. Otherwise we proceed as before.
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. |
@tobinibot thanks for the link, I'll be using that one for sure. |
@tobinibot thanks, I too hit this exact problem, and am now using the shell plugin instead. |
@tobinibot How did you manage to copy files using the webpack-shell-plugin? I tried
|
@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
|
@Anima-t3d I have a similar thing as @photonstorm but I use
EDIT: Somehow the |
@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: var fs = require('fs-extra');
var read = require('fs-readdir-recursive');
var path = require('path');
// Setup the instructions on which folders to copy
var copyInstructions = [
// 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 files
copyInstructions.map(function (instruction) {
const {label, source, destination} = instruction;
var files = read(source);
/*if (label) {
console.log(label);
}*/
files.forEach(file => {
var sourcePath = source + '/' + file;
var destinationPath = destination + '/' + file;
fs.copy(path.resolve(sourcePath), path.resolve(destinationPath), function (err) {
/*if (label) {
console.log(label);
}*/
if (err) {
return console.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. |
@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 |
The |
Out of scope this plugin, please use |
FileManagerPlugin is broken too. |
@evilebottnawi thanks! you save my day!!! |
@evilebottnawi thanks! |
For me, filter files to change output work fine...
Hope help you for simple cases... |
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: Just use |
Is this not implemented yet? |
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?
The text was updated successfully, but these errors were encountered: