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

fix: only have the stream close after writing Webpack filesystem caches #244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions index.js
Expand Up @@ -190,12 +190,28 @@ module.exports = function (options, wp, done) {
}
self.emit('compilation-error', compilationError);
}
if (!isInWatchMode) {
self.queue(null);
}
done(err, stats);
if (isInWatchMode && !isSilent) {
fancyLog('webpack is watching for changes');
if (isInWatchMode) {
done(err, stats);
if (!isSilent) {
fancyLog('webpack is watching for changes');
}
} else {
function closeStream() {
self.queue(null);
done(err, stats);
}

if (compiler.close) {
// For Webpack v5 and above:
// From https://webpack.js.org/api/node/#run: we need to close
// the compiler so that low-priority (e.g., caching) work happens
compiler.close(closeStream);
} else {
// Webpack v4 doesn't have compiler.close, so just immediately end
// the stream
// (see https://github.com/webpack/webpack/blob/v4.0.0/lib/Compiler.js)
closeStream();
}
}
};

Expand Down
32 changes: 31 additions & 1 deletion test/test.js
Expand Up @@ -3,6 +3,7 @@ const webpack = require('../');
const path = require('path');
const fs = require('vinyl-fs');
const named = require('vinyl-named');
const nodeFS = require('fs');

const base = path.resolve(__dirname, 'fixtures');

Expand Down Expand Up @@ -93,7 +94,7 @@ test('stream multiple entry points', function (t) {
test('empty input stream', function (t) {
t.plan(1);

const entry = fs.src('test/path/to/nothing', { allowEmpty: true });
const entry = fs.src('test/path/to/nothing', {allowEmpty: true});
const stream = webpack({
config: {},
quiet: true
Expand Down Expand Up @@ -199,3 +200,32 @@ test('error formatting', function (t) {
});
entry.pipe(stream);
});

test('writes filesystem cache files', function (t) {
t.plan(2);
const entry = fs.src('test/fixtures/entry.js');
const cacheDirectory = path.join(__dirname, 'testCache');

nodeFS.rmdirSync(cacheDirectory, {recursive: true, force: true});
t.equal(nodeFS.existsSync(cacheDirectory), false, 'cache directory should not exist at start');

const stream = webpack({
config: {
mode: 'development',
output: {filename: 'bundle.js'},
cache: {
type: 'filesystem',
idleTimeoutForInitialStore: 0,
cacheDirectory
},
},
quiet: true
});

stream.on('end', function () {
t.equal(nodeFS.existsSync(cacheDirectory), true, 'should have created a cache directory after compile');
nodeFS.rmdirSync(cacheDirectory, {recursive: true, force: true});
});

entry.pipe(stream);
});