Skip to content
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

Plugin fails when no CSS files exist #7

Open
jmlopez-rod opened this issue Jun 15, 2016 · 0 comments
Open

Plugin fails when no CSS files exist #7

jmlopez-rod opened this issue Jun 15, 2016 · 0 comments

Comments

@jmlopez-rod
Copy link

When no CSS files exist in a bundle, the callback is never called. This leads to webpack finishing silently without any output. Everything works well once we have at least one bundle that contains some css.

A possible fix (see the finished variable):

var bless = require('bless');
var RawSource = require('webpack/lib/RawSource');
var _ = require('lodash');

module.exports = function(options, pattern) {
    pattern = pattern || /\.css$/;
    options = options || {};

    return {
        apply: function(compiler) {
            compiler.plugin("this-compilation", function(compilation) {
                compilation.plugin("optimize-assets", function(assets, callback) {
                    var pending = 0;
                    var finished = false;

                    function done(err) {
                        pending--;
                        if (err && pending >= 0) {
                            pending = 0;
                            callback(err);
                            finished = true;
                        } else if (pending === 0) {
                            callback();
                            finished = true;
                        }
                    }

                    Object.keys(assets)
                        .filter(pattern.test.bind(pattern))
                        .forEach(function(name) {
                            pending++;
                            new bless.Parser({ output: name, options : options })
                                .parse(assets[name].source(), function(err, files) {
                                    if (err) {
                                        done(err);
                                        return;
                                    }
                                    delete assets[name];
                                    files.forEach(function(file) {
                                        assets[file.filename] = new RawSource(file.content);
                                    });
                                    done();
                                });
                        });

                    if (!finished) {
                        callback();
                    }
                });
            });
        }
    };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant