Skip to content

Plugin fails when no CSS files exist #7

@jmlopez-rod

Description

@jmlopez-rod

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();
                    }
                });
            });
        }
    };
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions