Skip to content

Commit 836d57d

Browse files
fix: memory leak when writeToDisk used
1 parent aec1bf7 commit 836d57d

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

lib/fs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ module.exports = {
1515

1616
for (const compiler of compilers) {
1717
compiler.hooks.emit.tap('WebpackDevMiddleware', (compilation) => {
18+
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
19+
return;
20+
}
21+
1822
compiler.hooks.assetEmitted.tapAsync(
1923
'WebpackDevMiddleware',
2024
(file, content, callback) => {
@@ -74,6 +78,7 @@ module.exports = {
7478
});
7579
}
7680
);
81+
compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
7782
});
7883
}
7984
},

test/server.test.js

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,20 +531,26 @@ describe('Server', () => {
531531

532532
function writeToDisk(value, done) {
533533
app = express();
534+
534535
const compiler = webpack(webpackConfig);
536+
535537
instance = middleware(compiler, {
536538
stats: 'errors-only',
537539
logLevel,
538540
writeToDisk: value,
539541
});
542+
540543
app.use(instance);
541544
app.use((req, res) => {
542545
res.sendStatus(200);
543546
});
547+
544548
listen = listenShorthand(done);
549+
550+
return { compiler, instance };
545551
}
546552

547-
describe('write to disk', () => {
553+
describe('write to disk with true', () => {
548554
beforeAll((done) => {
549555
writeToDisk(true, done);
550556
});
@@ -568,6 +574,28 @@ describe('Server', () => {
568574
});
569575
});
570576

577+
describe('write to disk with false', () => {
578+
beforeAll((done) => {
579+
writeToDisk(false, done);
580+
});
581+
afterAll(close);
582+
583+
it('should not find the bundle file on disk', (done) => {
584+
request(app)
585+
.get('/foo/bar')
586+
.expect(200, () => {
587+
const bundlePath = path.join(
588+
__dirname,
589+
'./fixtures/server-test/bundle.js'
590+
);
591+
592+
expect(fs.existsSync(bundlePath)).toBe(false);
593+
594+
done();
595+
});
596+
});
597+
});
598+
571599
describe('write to disk with filter', () => {
572600
beforeAll((done) => {
573601
writeToDisk((filePath) => /bundle\.js$/.test(filePath), done);
@@ -705,4 +733,84 @@ describe('Server', () => {
705733
});
706734
});
707735
});
736+
737+
describe('write to disk with true hooks', () => {
738+
let compiler = null;
739+
740+
beforeAll((done) => {
741+
({ compiler, instance } = writeToDisk(true, done));
742+
});
743+
afterAll(close);
744+
745+
it('should not find the bundle file on disk', (done) => {
746+
request(app)
747+
.get('/foo/bar')
748+
.expect(200, () => {
749+
const bundlePath = path.join(
750+
__dirname,
751+
'./fixtures/server-test/bundle.js'
752+
);
753+
754+
expect(
755+
compiler.hooks.assetEmitted.taps.filter(
756+
(hook) => hook.name === 'WebpackDevMiddleware'
757+
).length
758+
).toBe(1);
759+
expect(fs.existsSync(bundlePath)).toBe(true);
760+
761+
fs.unlinkSync(bundlePath);
762+
763+
instance.invalidate();
764+
765+
compiler.hooks.done.tap('WebpackDevMiddlewareWriteToDiskTest', () => {
766+
expect(
767+
compiler.hooks.assetEmitted.taps.filter(
768+
(hook) => hook.name === 'WebpackDevMiddleware'
769+
).length
770+
).toBe(1);
771+
772+
done();
773+
});
774+
});
775+
});
776+
});
777+
778+
describe('write to disk with false hooks', () => {
779+
let compiler = null;
780+
781+
beforeAll((done) => {
782+
({ compiler } = writeToDisk(false, done));
783+
});
784+
afterAll(close);
785+
786+
it('should not find the bundle file on disk', (done) => {
787+
request(app)
788+
.get('/foo/bar')
789+
.expect(200, () => {
790+
const bundlePath = path.join(
791+
__dirname,
792+
'./fixtures/server-test/bundle.js'
793+
);
794+
795+
expect(
796+
compiler.hooks.assetEmitted.taps.filter(
797+
(hook) => hook.name === 'WebpackDevMiddleware'
798+
).length
799+
).toBe(0);
800+
expect(fs.existsSync(bundlePath)).toBe(false);
801+
802+
instance.invalidate();
803+
804+
compiler.hooks.done.tap('WebpackDevMiddlewareWriteToDiskTest', () => {
805+
expect(
806+
compiler.hooks.assetEmitted.taps.filter(
807+
(hook) => hook.name === 'WebpackDevMiddleware'
808+
).length
809+
).toBe(0);
810+
811+
done();
812+
});
813+
});
814+
});
815+
});
708816
});

0 commit comments

Comments
 (0)