Skip to content
This repository was archived by the owner on Sep 7, 2026. It is now read-only.
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

Closed

Description

@jmlopez-rod

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?

Activity

  1. jmlopez-rod commented on Jan 29, 2016

    @jmlopez-rod
    Author

    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.

            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 absFileSrc is in the assets and make it read that source? I'll post when I find out.

  2. jmlopez-rod commented on Jan 29, 2016

    @jmlopez-rod
    Author

    So here is a solution, I added a few lines to writeFileToAssets so that it can check if the file is in production then to just point to that asset.

    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?

  3. tobinibot commented on Oct 29, 2016

    @tobinibot

    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.

  4. jmlopez-rod commented on Oct 30, 2016

    @jmlopez-rod
    Author

    @tobinibot thanks for the link, I'll be using that one for sure.

  5. photonstorm commented on Nov 24, 2016

    @photonstorm

    @tobinibot thanks, I too hit this exact problem, and am now using the shell plugin instead.

  6. Anima-t3d commented on Jan 11, 2017

    @Anima-t3d

    @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.

    .../node_modules/webpack-shell-plugin/lib/index.js:168
    throw error;
    ^
    1

  7. photonstorm commented on Jan 11, 2017

    @photonstorm

    @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);
    
    });
    
  8. jmlopez-rod commented on Jan 11, 2017

    @jmlopez-rod
    Author

    @Anima-t3d I have a similar thing as @photonstorm but I use onBuildExit:

    config.plugins.push(new WebpackShellPlugin({
        onBuildExit: [
          'echo "Transfering files ... "',
          'cp -r dist/* path/to/other/dist/',
          'cp -r src/* path/to/other/src/',
          'echo "DONE ... "',
        ],
      }));
    

    cp: http://man7.org/linux/man-pages/man1/cp.1.html

    EDIT:

    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.

  9. Anima-t3d commented on Jan 13, 2017

    @Anima-t3d

    @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.

  10. JeffreyWay commented on Mar 9, 2017

    @JeffreyWay

    @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.

  11. danchristian commented on Jun 23, 2017

    @danchristian

    @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.

  12. gregnb commented on Sep 2, 2017

    @gregnb

    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

  13. 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
  14. henryruhs commented on Oct 20, 2017

    @henryruhs

    The FileManagerPlugin replaced CleanWebpackPlugin and CopyWebpackPlugin for me! Go for it...

  15. alexander-akait commented on Feb 22, 2018

    @alexander-akait
    Member

    Out of scope this plugin, please use FileManagerPlugin or just run npm task after build

  16. airtonix commented on May 2, 2018

    @airtonix

    FileManagerPlugin is broken too.

  17. LiMeii commented on Aug 14, 2018

    @LiMeii

    @evilebottnawi thanks! you save my day!!!

  18. alvinhtml commented on Apr 1, 2019

    @alvinhtml
  19. Antoine-360pixel commented on Apr 16, 2019

    @Antoine-360pixel

    For me, filter files to change output work fine...

    {...}
    output: {
        path: path.revolve(__dirname, `build/static`),
        filename: (chunkData) => {
          return chunkData.chunk.name.includes('sw') ? 'bundle-[name].js': 'assets/bundle-[name].js';
        },
        sourceMapFilename: `[name].map`
      },
    {...}
    

    Hope help you for simple cases...

  20. anhnt-mageplaza commented on Mar 25, 2020

    @anhnt-mageplaza

    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.

  21. jasonmorita commented on Apr 17, 2020

    @jasonmorita

    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.

  22. zekageri commented on Sep 12, 2023

    @zekageri

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions