fix: 解决 webpack 缓存问题 (#166)

* fix: 解决 webpack 缓存问题

* refactor: 优化清理代码
This commit is contained in:
qlin 2023-01-06 17:30:22 +08:00 committed by GitHub
parent a1e1bb7b04
commit bc9897a2b0
3 changed files with 117 additions and 78 deletions

View File

@ -51,11 +51,13 @@
"css-loader": "^5.0.1",
"css-minimizer-webpack-plugin": "^3.0.0",
"envinfo": "^7.7.3",
"fs-extra": "^10.0.0",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.0.0",
"html-webpack-tags-plugin": "^3.0.0",
"less": "3.9.0",
"less-loader": "^8.0.0",
"get-folder-size": "^2.0.1",
"mini-css-extract-plugin": "^1.3.5",
"mockjs": "^1.1.0",
"postcss": "8.3.0",

View File

@ -4,6 +4,24 @@
*/
const assert = require('assert');
const path = require('path');
async function handleCacheClean(cwd) {
return new Promise((resolve, reject) => {
const cachePath = path.join(cwd, '.cache/webpack');
require('get-folder-size')(cachePath, (err, size) => {
if (err) {
return reject(err);
}
// 大于 5G 清除缓存,修复 webpack 缓存无限增长问题
// https://github.com/webpack/webpack/issues/13291
if (size > 5 * 1024 * 1024 * 1024) {
require('fs-extra').removeSync(cachePath);
}
resolve(size);
});
});
}
export default (api) => {
const {
@ -43,6 +61,7 @@ export default (api) => {
const createRouteMiddleware = require('./createRouteMiddleware').default;
const generateFiles = require('../../../utils/generateFiles').default;
const { watchPkg } = require('./watchPkg');
await handleCacheClean(api.paths.cwd);
const defaultPort = process.env.PORT || args.port || api.config.devServer?.port;
port = await portfinder.getPortPromise({

View File

@ -7114,6 +7114,11 @@ functions-have-names@^1.2.2:
resolved "https://registry.npmmirror.com/functions-have-names/-/functions-have-names-1.2.3.tgz#0404fe4ee2ba2f607f0e0ec3c80bae994133b834"
integrity sha512-xckBUXyTIqT97tq2x2AMb+g163b5JFysYk0x4qxNFwbfQkmNZoiRHb6sPzI9/QV33WeuvVYBUIiD4NzNIyqaRQ==
gar@^1.0.4:
version "1.0.4"
resolved "https://registry.npmmirror.com/gar/-/gar-1.0.4.tgz#f777bc7db425c0572fdeb52676172ca1ae9888b8"
integrity sha512-w4n9cPWyP7aHxKxYHFQMegj7WIAsL/YX/C4Bs5Rr8s1H9M1rNtRWRsw+ovYMkXDQ5S4ZbYHsHAPmevPjPgw44w==
gauge@~2.7.3:
version "2.7.4"
resolved "https://registry.npmmirror.com/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7"
@ -7138,6 +7143,14 @@ get-caller-file@^2.0.1, get-caller-file@^2.0.5:
resolved "https://registry.npmmirror.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
get-folder-size@^2.0.1:
version "2.0.1"
resolved "https://registry.npmmirror.com/get-folder-size/-/get-folder-size-2.0.1.tgz#3fe0524dd3bad05257ef1311331417bcd020a497"
integrity sha512-+CEb+GDCM7tkOS2wdMKTn9vU7DgnKUTuDlehkNJKNSovdCOVxs14OfKCk4cvSaR3za4gj+OBdl9opPN9xrJ0zA==
dependencies:
gar "^1.0.4"
tiny-each-async "2.0.3"
get-intrinsic@^1.0.2, get-intrinsic@^1.1.0, get-intrinsic@^1.1.1, get-intrinsic@^1.1.3:
version "1.1.3"
resolved "https://registry.npmmirror.com/get-intrinsic/-/get-intrinsic-1.1.3.tgz#063c84329ad93e83893c7f4f243ef63ffa351385"
@ -13018,6 +13031,11 @@ thunky@^1.0.2:
resolved "https://registry.npmmirror.com/thunky/-/thunky-1.1.0.tgz#5abaf714a9405db0504732bbccd2cedd9ef9537d"
integrity sha512-eHY7nBftgThBqOyHGVN+l8gF0BucP09fMo0oO/Lb0w1OF80dJv+lDVpXG60WMQvkcxAkNybKsrEIE3ZtKGmPrA==
tiny-each-async@2.0.3:
version "2.0.3"
resolved "https://registry.npmmirror.com/tiny-each-async/-/tiny-each-async-2.0.3.tgz#8ebbbfd6d6295f1370003fbb37162afe5a0a51d1"
integrity sha512-5ROII7nElnAirvFn8g7H7MtpfV1daMcyfTGQwsn/x2VtyV+VPiO5CjReCJtWLvoKTDEDmZocf3cNPraiMnBXLA==
tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.npmmirror.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"