diff --git a/build-config.md b/build-config.md index de0562e..86ed1db 100644 --- a/build-config.md +++ b/build-config.md @@ -304,6 +304,14 @@ const apiUrl = "http://foobar.com/api" + 'test' builder 默认提供粗粒度的 source map 以提升构建效率,效果上只是简单地将打包后代码按模块分开;开启 `highQualitySourceMap` 后,builder 会提供到源代码的映射,第三方依赖包自带的 source map 信息(如果有)也会被消费,以便为第三方库提供基于源代码的调试体验。 +- **`optimization.filesystemCache`** + + 类型:`boolean` + + 是否启用文件系统缓存,用于提升二次启动的打包速度,对于大型的前端仓库提升效果尤其明显。 + + 这里传入 `true` 表示启用文件系统缓存,`false` 则不启用。 + ## **`devProxy`** 类型:`object` diff --git a/preset-configs/config.schema.json b/preset-configs/config.schema.json index 3191805..d2e3c70 100644 --- a/preset-configs/config.schema.json +++ b/preset-configs/config.schema.json @@ -158,6 +158,10 @@ "highQualitySourceMap": { "type": "boolean", "description": "是否提供高质量的 source map。\nbuilder 默认提供粗粒度的 source map 以提升构建效率,效果上只是简单地将打包后代码按模块分开;开启 `highQualitySourceMap` 后,builder 会提供到源代码的映射,第三方依赖包自带的 source map 信息(如果有)也会被消费,以便为第三方库提供基于源代码的调试体验。" + }, + "filesystemCache": { + "type": "boolean", + "description": "是否启用文件系统缓存,用于提升二次启动的打包速度,对于大型的前端仓库提升效果尤其明显。\n这里传入 `true` 表示启用文件系统缓存,`false` 则不启用。" } } }, diff --git a/preset-configs/default.json b/preset-configs/default.json index 5ed56f8..87cea36 100644 --- a/preset-configs/default.json +++ b/preset-configs/default.json @@ -64,7 +64,8 @@ "extractVendor": true, "compressImage": false, "transformDeps": false, - "highQualitySourceMap": false + "highQualitySourceMap": false, + "filesystemCache": true }, "devProxy": {}, "deploy": { diff --git a/src/bin.ts b/src/bin.ts index 3802639..2bddb9f 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -3,7 +3,7 @@ import { setAutoFreeze } from 'immer' import yargs from 'yargs' -import { setBuildRoot, setBuildConfigFilePath, setNeedCache } from './utils/paths' +import { setBuildRoot, setBuildConfigFilePath } from './utils/paths' import { Env, setEnv } from './utils/build-env' import logger from './utils/logger' import { setNeedAnalyze } from './utils/build-conf' @@ -41,11 +41,6 @@ const options: Record = { type: 'boolean', desc: 'Output more info', default: false - }, - 'unstable-cache': { - type: 'boolean', - desc: 'Cache the generated webpack modules and chunks to filesystem for improve build speed. ', - default: false } } @@ -128,10 +123,6 @@ function applyArgv(argv: yargs.Arguments) { setEnv(argv.BUILD_ENV as Env) } } - - if (argv['unstable-cache']) { - setNeedCache() - } } function handleError(e: unknown) { diff --git a/src/clean.ts b/src/clean.ts index dbc2728..aa7ed41 100644 --- a/src/clean.ts +++ b/src/clean.ts @@ -9,20 +9,12 @@ import { getDistPath } from './utils/paths' import logger from './utils/logger' import { logLifecycle } from './utils' import { findBuildConfig } from './utils/build-conf' -import { getNeedCache, getCachePath } from './utils/paths' async function clean() { const buildConfig = await findBuildConfig() const dist = getDistPath(buildConfig) logger.debug(`clean dist: ${dist}`) await del(dist, { force: true }) - - // delete webpack persistent cache directory - if (getNeedCache()) { - const cacheDir = getCachePath() - logger.debug(`clean cache: ${cacheDir}`) - await del(cacheDir, { force: true }) - } } export default logLifecycle('Clean', clean, logger) diff --git a/src/utils/build-conf.ts b/src/utils/build-conf.ts index b29e04b..b0ae0f8 100644 --- a/src/utils/build-conf.ts +++ b/src/utils/build-conf.ts @@ -53,6 +53,8 @@ export interface Optimization { addPolyfill: AddPolyfill /** 是否提供高质量的 source map */ highQualitySourceMap: boolean + /** 是否启用文件系统缓存 */ + filesystemCache: boolean } export interface EnvVariables { diff --git a/src/utils/paths.ts b/src/utils/paths.ts index 66ea12d..51dd62c 100644 --- a/src/utils/paths.ts +++ b/src/utils/paths.ts @@ -61,14 +61,3 @@ export function getTestDistPath(conf: BuildConfig) { export function getCachePath() { return abs('node_modules/.cache/webpack') } - -/** whether need filesystem cache */ -let cache = false - -export function getNeedCache() { - return cache -} - -export function setNeedCache() { - cache = true -} diff --git a/src/webpack/index.ts b/src/webpack/index.ts index 3cf81e1..23a2f00 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -10,7 +10,7 @@ import CssMinimizerPlugin from 'css-minimizer-webpack-plugin' import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer' import WebpackBarPlugin from 'webpackbar' import ImageMinimizerPlugin from 'image-minimizer-webpack-plugin' -import { getBuildRoot, abs, getStaticPath, getDistPath, getSrcPath, getNeedCache } from '../utils/paths' +import { getBuildRoot, abs, getStaticPath, getDistPath, getSrcPath } from '../utils/paths' import { BuildConfig, findBuildConfig, getNeedAnalyze } from '../utils/build-conf' import { addTransforms } from './transform' import { Env, getEnv } from '../utils/build-env' @@ -155,7 +155,7 @@ export async function getConfig(): Promise { })) } - if (getNeedCache()) { + if (isDev && buildConfig.optimization.filesystemCache) { config = enableFilesystemCache(config) }