6

I've seen this question: Run command after webpack build and I modified the sample code provided there.

To describe my scenario, I'm trying to take the assets output by webpack and deploy them to a mounted network drive. I'm using ncp to copy a directory and the actual copy works fine, it just seems that when webpack calls either the after-emit event or the done event, it is not actually done emitting or writing the files to the file system. My copy ends up copying either empty files or partially written files.

Here is the entire plugin:

'use strict';
var ncp = require('ncp').ncp;

function WebPackDeployAfterBuild(options) {
    var defaultOptions = {

    };

    this.options = Object.assign(defaultOptions, options);
}

WebPackDeployAfterBuild.prototype.apply = function(compiler) {
    const options = this.options;

    compiler.plugin("done", (stats) => {
            console.log(Object.keys(stats.compilation.assets));
            console.log("\nExecuting Deploy on done...");
            ncp(options.from, options.to, function(err) {
                if (err) {
                    console.error("Err in ncp");
                }
                console.log(`Finished deploying ${options.from} to ${options.to}`);
            });
    });

    compiler.plugin("after-emit", (compilation, callback) => {
        console.log(Object.keys(compilation.assets));
        console.log("\nExecuting Deploy on after-emit...");
        ncp(options.from, options.to, function(err) {
            if (err) {
                console.error("Err in ncp");
            }
            console.log(`Finished deploying ${options.from} to ${options.to}`);
            callback();
        });
    });
};

module.exports = WebPackDeployAfterBuild;

Here is the usage in my webpack.config.js file:

config.plugins.push(
        new WebPackDeployAfterBuild({
            from: path.resolve(__dirname, './myOutputPath'),
            to: 'Q:/'
        })
    )

I've included both the done and after-emit handlers, but in practice I've been commenting one out. I only need to run the event once, but both of these events are firing too soon. Is there another event I can use?

E: Forgot to mention, I am also running this on a webpack --watch task. I want to save a file, have webpack process it and immediately deploy it to my server while I develop. If there is a better dev workflow, I'm open to suggestions.

E2: Here's a log of what happens:

2ms optimize chunk assets
7ms optimize assets
 95% emit
[ 'scripts/app.bundle.js', 'scripts/vendor.bundle.js' ]

Executing Deploy After build...
11ms emit
Hash: a005eef9ae47a167f791
Version: webpack 1.13.2
Time: 278ms
                Asset     Size  Chunks             Chunk Names
scripts/app.bundle.js  61.1 kB       0  [emitted]  scripts/app
    + 20 hidden modules
Child html-webpack-plugin for "index.html":
        + 3 hidden modules
Finished deploying C:\Users\redacted\Source\myprojectOutput to Q:/

You can see that the logging I do occurs before the 11ms emit message, I would expect it to be after that...

Community
  • 1
  • 1
Sean
  • 300
  • 3
  • 12
  • 2
    Did you find a solution to this? Those events still run before assets are written to the file system in webpack 2. – rob3c Dec 13 '16 at 20:38

0 Answers0