Skip to content

Commit

Permalink
Cache webpack to use on subsequent compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jmurzy committed Mar 16, 2016
1 parent a02cb77 commit f82b9bc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ var defaultStatsOptions = {
errorDetails: false
};

var cache = {};

module.exports = function (options, wp, done) {
if (cache.wp !== wp || cache.options !== options) {
cache = {};
}

cache.options = options;
cache.wp = wp;

options = clone(options) || {};
if (typeof done !== 'function') {
var callingDone = false;
Expand Down Expand Up @@ -103,7 +112,11 @@ module.exports = function (options, wp, done) {
return self.emit('end');
}

var compiler = webpack(options, function (err, stats) {
// Cache compiler for future use
var compiler = cache.compiler || webpack(options);
cache.compiler = compiler;

compiler.run(function (err, stats) {
if (err) {
self.emit('error', new gutil.PluginError('webpack-stream', err));
}
Expand Down Expand Up @@ -140,7 +153,9 @@ module.exports = function (options, wp, done) {
}));
}

var fs = compiler.outputFileSystem = new MemoryFileSystem();
cache.mfs = cache.mfs || new MemoryFileSystem();

var fs = compiler.outputFileSystem = cache.mfs;

compiler.plugin('after-emit', function (compilation, callback) {
Object.keys(compilation.assets).forEach(function (outname) {
Expand Down
45 changes: 45 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,51 @@ test('multiple entry points', function (t) {
stream.end();
});

test('subsequent runs served from cache', function (t) {
t.plan(4);

var config = {
entry: {
'one': path.join(base, 'entry.js'),
'two': path.join(base, 'anotherentrypoint.js')
},
output: {
filename: '[name].bundle.js'
},
quiet: true
};

var stream = webpack(config);
stream.on('data', function (file) {
var basename = path.basename(file.path);
var contents = file.contents.toString();
switch (basename) {
case 'one.bundle.js':
t.ok(/__webpack_require__/i.test(contents), 'should contain "__webpack_require__"');
t.ok(/var one = true;/i.test(contents), 'should contain "var one = true;"');
break;
case 'two.bundle.js':
t.ok(/var anotherentrypoint = true;/i.test(contents), 'should contain "var anotherentrypoint = true;"');
break;
}
});

stream.on('end', function () {
var cachedStream = webpack(config);
var data = null;

cachedStream.on('data', function (file) {
data = file;
});

cachedStream.on('end', function () {
t.ok(data === null, 'should not write any output');
});
});

stream.end();
});

test('stream multiple entry points', function (t) {
t.plan(3);
var entries = fs.src(['test/fixtures/entry.js', 'test/fixtures/anotherentrypoint.js']);
Expand Down

0 comments on commit f82b9bc

Please sign in to comment.