Skip to content

Commit

Permalink
默认开启文件系统缓存 (#160)
Browse files Browse the repository at this point in the history
* feat: 默认开启文件系统缓存

* fix: docs

* fix: use filesystem cache

* fix: version
  • Loading branch information
liaoyu authored Jan 14, 2022
1 parent 5d3b53c commit 9440316
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 32 deletions.
8 changes: 8 additions & 0 deletions build-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions preset-configs/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@
"highQualitySourceMap": {
"type": "boolean",
"description": "是否提供高质量的 source map。\nbuilder 默认提供粗粒度的 source map 以提升构建效率,效果上只是简单地将打包后代码按模块分开;开启 `highQualitySourceMap` 后,builder 会提供到源代码的映射,第三方依赖包自带的 source map 信息(如果有)也会被消费,以便为第三方库提供基于源代码的调试体验。"
},
"filesystemCache": {
"type": "boolean",
"description": "是否启用文件系统缓存,用于提升二次启动的打包速度,对于大型的前端仓库提升效果尤其明显。\n这里传入 `true` 表示启用文件系统缓存,`false` 则不启用。"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion preset-configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"extractVendor": true,
"compressImage": false,
"transformDeps": false,
"highQualitySourceMap": false
"highQualitySourceMap": false,
"filesystemCache": true
},
"devProxy": {},
"deploy": {
Expand Down
11 changes: 1 addition & 10 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -41,11 +41,6 @@ const options: Record<string, yargs.Options> = {
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
}
}

Expand Down Expand Up @@ -128,10 +123,6 @@ function applyArgv(argv: yargs.Arguments) {
setEnv(argv.BUILD_ENV as Env)
}
}

if (argv['unstable-cache']) {
setNeedCache()
}
}

function handleError(e: unknown) {
Expand Down
8 changes: 0 additions & 8 deletions src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions src/utils/build-conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export interface Optimization {
addPolyfill: AddPolyfill
/** 是否提供高质量的 source map */
highQualitySourceMap: boolean
/** 是否启用文件系统缓存 */
filesystemCache: boolean
}

export interface EnvVariables {
Expand Down
11 changes: 0 additions & 11 deletions src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -155,7 +155,7 @@ export async function getConfig(): Promise<Configuration> {
}))
}

if (getNeedCache()) {
if (isDev && buildConfig.optimization.filesystemCache) {
config = enableFilesystemCache(config)
}

Expand Down

0 comments on commit 9440316

Please sign in to comment.