From 305c18816ca6c4275c2755ae6b48d90a8cc85bd1 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 5 Oct 2020 15:14:30 +0530 Subject: [PATCH] fix: cache issue (#1862) --- packages/webpack-cli/lib/utils/Compiler.js | 11 ++++++++-- scripts/cleanupTest.js | 13 +++++++++++- test/cache/cache.test.js | 16 +++++++++++++++ test/cache/src/main.js | 1 + test/cache/webpack.config.js | 24 ++++++++++++++++++++++ test/core-flags/cache-flags.test.js | 8 ++++++-- 6 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 test/cache/cache.test.js create mode 100644 test/cache/src/main.js create mode 100644 test/cache/webpack.config.js diff --git a/packages/webpack-cli/lib/utils/Compiler.js b/packages/webpack-cli/lib/utils/Compiler.js index 0479354a624..91edf001640 100644 --- a/packages/webpack-cli/lib/utils/Compiler.js +++ b/packages/webpack-cli/lib/utils/Compiler.js @@ -87,8 +87,15 @@ class Compiler { // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve) => { await this.compiler.run((err, stats) => { - const content = this.compilerCallback(err, stats, lastHash, options, outputOptions); - resolve(content); + if (this.compiler.close) { + this.compiler.close(() => { + const content = this.compilerCallback(err, stats, lastHash, options, outputOptions); + resolve(content); + }); + } else { + const content = this.compilerCallback(err, stats, lastHash, options, outputOptions); + resolve(content); + } }); }); } diff --git a/scripts/cleanupTest.js b/scripts/cleanupTest.js index 7be3d2c29e9..0f1638df0cf 100644 --- a/scripts/cleanupTest.js +++ b/scripts/cleanupTest.js @@ -3,7 +3,18 @@ const rimraf = require('rimraf'); const { join } = require('path'); const collectTestFolders = require('./utils'); -const outputDirectories = ['bin', 'binary', 'dist', 'test', 'test-assets', 'test-plugin', 'test-loader', 'stats.json']; +const outputDirectories = [ + 'bin', + 'binary', + 'dist', + 'test', + 'test-assets', + 'test-plugin', + 'test-loader', + 'test-cache-path', + 'test-locate-cache', + 'stats.json', +]; const folderStrategy = (stats, file) => { return stats.isDirectory() && outputDirectories.includes(file); diff --git a/test/cache/cache.test.js b/test/cache/cache.test.js new file mode 100644 index 00000000000..8eebde0b56a --- /dev/null +++ b/test/cache/cache.test.js @@ -0,0 +1,16 @@ +'use strict'; + +const { run, isWebpack5 } = require('../utils/test-utils'); + +describe('cache related tests', () => { + it('should log warning in case of single compiler', () => { + let { stderr, stdout } = run(__dirname, ['-c', 'webpack.config.js'], false); + // run 2nd compilation + ({ stderr, stdout } = run(__dirname, ['-c', 'webpack.config.js'], false)); + + if (isWebpack5) { + expect(stderr).toContain('starting to restore cache content'); + expect(stdout).toContain('[cached] 1 module'); + } + }); +}); diff --git a/test/cache/src/main.js b/test/cache/src/main.js new file mode 100644 index 00000000000..fec83457547 --- /dev/null +++ b/test/cache/src/main.js @@ -0,0 +1 @@ +console.log('tehghgst cache'); diff --git a/test/cache/webpack.config.js b/test/cache/webpack.config.js new file mode 100644 index 00000000000..96c9da4834b --- /dev/null +++ b/test/cache/webpack.config.js @@ -0,0 +1,24 @@ +const path = require('path'); + +module.exports = { + cache: { + type: 'filesystem', + buildDependencies: { + config: [__filename], + }, + }, + infrastructureLogging: { + debug: /webpack\.cache/, + }, + entry: { + app: './src/main.js', + }, + devtool: 'inline-source-map', + plugins: [], + output: { + filename: '[name].bundle.js', + chunkFilename: '[name].bundle.js', + path: path.resolve(__dirname, 'dist'), + publicPath: '/', + }, +}; diff --git a/test/core-flags/cache-flags.test.js b/test/core-flags/cache-flags.test.js index c9a3f4bf47f..69cfcd29829 100644 --- a/test/core-flags/cache-flags.test.js +++ b/test/core-flags/cache-flags.test.js @@ -1,6 +1,8 @@ 'use strict'; const { run } = require('../utils/test-utils'); +const { existsSync } = require('fs'); +const { resolve } = require('path'); describe('cache related flags from core', () => { it('should be successful with --cache ', () => { @@ -25,17 +27,19 @@ describe('cache related flags from core', () => { }); it('should set cache.cacheDirectory with --cache-cache-directory', () => { - const { stderr, stdout } = run(__dirname, ['--cache-type', 'filesystem', '--cache-cache-directory', '/test-cache-path']); + const { stderr, stdout } = run(__dirname, ['--cache-type', 'filesystem', '--cache-cache-directory', './test-cache-path']); expect(stderr).toBeFalsy(); expect(stdout).toContain('test-cache-path'); + expect(existsSync(resolve(__dirname, './test-cache-path'))).toBeTruthy(); }); it('should set cache.cacheLocation with --cache-cache-locations', () => { - const { stderr, stdout } = run(__dirname, ['--cache-type', 'filesystem', '--cache-cache-location', '/test-locate-cache']); + const { stderr, stdout } = run(__dirname, ['--cache-type', 'filesystem', '--cache-cache-location', './test-locate-cache']); expect(stderr).toBeFalsy(); expect(stdout).toContain('test-locate-cache'); + expect(existsSync(resolve(__dirname, './test-locate-cache'))).toBeTruthy(); }); it('should set cache.hashAlgorithm with --cache-hash-algorithm', () => {