function Swatch(opts) {
    var self = this;

    if (!(self instanceof Swatch)) {
        return new Swatch(opts);
    }

    EventEmitter.call(self);

    var config = parseConfig(opts);

    self.config = xtend(config, {
        verbose: false,
        silent: false
    });

    var base = config.base || 'base';

    self.variations = validVariations(config).concat({
        id: base,
        chain: [config.basetree || 'base']
    });

    self.bundlers = {};
    self.buildPathCache = {};
    self.monitors = [];

    // Default 'error' listener
    // https://nodejs.org/docs/v0.12.9/api/events.html#events_class_events_eventemitter
    self.on('error', function(err, context) {
        if (self.config.silent !== true) {
            console.error('Error context: ' + JSON.stringify(context, null, 2) + '\n' + err.stack);
        }
    });
}
Example #2
0
function MendelBrowserify(baseBundle, opts) {
    if (!(this instanceof MendelBrowserify)) {
        return new MendelBrowserify(baseBundle, opts);
    }

    var self = this;
    var argv = baseBundle.argv || {};
    this.baseBundle = baseBundle;
    this.baseOptions = baseBundle._options;

    opts.basedir = defined(
        opts.basedir, argv.basedir, this.baseOptions.basedir
    );
    opts.outfile = defined(
        opts.outfile, argv.outfile, argv.o, this.baseOptions.outfile
    );

    opts = parseConfig(xtend(opts));
    this.opts = opts;

    if (opts.bundle && opts.bundles[opts.bundle]) {
        this.baseOptions = xtend(this.baseOptions, opts.bundles[opts.bundle]);
    }


    this._manifestPending = 0;
    this._manifestIndexes = {};
    this._manifestBundles = [];

    this.baseVariation = {
        id: opts.base || 'base',
        chain: [opts.basetree || 'base'],
    };
    this.variations = validVariations(xtend(this.baseOptions, opts));
    this.variationsWithBase = [this.baseVariation].concat(this.variations);


    this.prepareBundle(baseBundle, this.baseVariation);

    this.variations.forEach(function(variation) {
        var vopts = xtend(self.baseOptions);
        var browserify = baseBundle.constructor;
        var pipeline = baseBundle.pipeline.constructor;

        vopts.plugin = nonMendelPlugins(vopts.plugin);

        var variationBundle = browserify(vopts);

        self.prepareBundle(variationBundle, variation);

        proxy(browserify, baseBundle, variationBundle, {
            filters: [onlyPublicMethods],
            exclude: ['bundle']
        });

        proxy(pipeline, baseBundle.pipeline, variationBundle.pipeline, {
            filters: [onlyPublicMethods]
        });

        baseBundle.on('bundle', function onBaseBundleStart() {
            if (argv.list) {
                return self.listVariation(variationBundle);
            }

            if (self.opts.outfile) {
                self.writeVariation(variationBundle);
            } else {
                return variationBundle.bundle().pipe(process.stdout);
            }
        });
    });
}