Skip to content

Commit

Permalink
feat: add an option to disable cache
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Sep 29, 2019
1 parent 21ac524 commit 6afe8fa
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 34 deletions.
25 changes: 17 additions & 8 deletions packages/saber/lib/WebpackUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@ module.exports = class WebpackUtils {
this.api = api
}

get shouldCache() {
return this.api.config.build.cache !== false
}

getCacheOptions(loader, obj) {
return {
cacheDirectory: this.api.resolveCache(path.join('cache', loader)),
cacheIdentifier: obj && JSON.stringify(obj)
}
return this.shouldCache
? {
cacheDirectory: this.api.resolveCache(path.join('cache', loader)),
cacheIdentifier:
obj && JSON.stringify(typeof obj === 'function' ? obj() : obj)
}
: {}
}

addCacheSupport(rule, loader, obj) {
rule
.use('cache-loader')
.loader(require.resolve('cache-loader'))
.options(this.getCacheOptions(loader, obj))
if (this.shouldCache) {
rule
.use('cache-loader')
.loader(require.resolve('cache-loader'))
.options(this.getCacheOptions(loader, obj))
}
}
}
16 changes: 13 additions & 3 deletions packages/saber/lib/cli-commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ module.exports = function(cli) {
cli
.command(
'build [app-path]',
'Compile the application and generate static HTML files'
'Compile the application and generate static HTML files',
{
ignoreOptionDefaultValue: true
}
)
.alias('generate')
.option('--skip-compilation', 'Skip the webpack compilation process')
.option('--inspect-webpack', 'Inspect webpack config in your editor')
.option('--no-cache', 'Disable cache')
.action((cwd = '.', options) => {
setNodeEnv('production')

Expand All @@ -18,9 +22,15 @@ module.exports = function(cli) {
)
}

const { skipCompilation } = options
const { skipCompilation, cache } = options
delete options.skipCompilation
return require('..')(Object.assign({ cwd, dev: false }, options))
delete options.cache

return require('..')(Object.assign({ cwd, dev: false }, options), {
build: {
cache
}
})
.build({ skipCompilation })
.catch(handleError)
})
Expand Down
6 changes: 4 additions & 2 deletions packages/saber/lib/cli-commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ module.exports = function(cli) {
.option('--port <port>', 'Server port', { default: 3000 })
.option('--host <host>', 'Server host', { default: '0.0.0.0' })
.option('--inspect-webpack', 'Inspect webpack config in your editor')
.option('--no-cache', 'Disable cache')
.action((cwd = '.', options) => {
setNodeEnv('development')

const { host, port, lazy } = options
const { host, port, lazy, cache } = options
delete options.host
delete options.port
delete options.lazy
Expand All @@ -23,7 +24,8 @@ module.exports = function(cli) {
port
},
build: {
lazy
lazy,
cache
}
})
.serve()
Expand Down
6 changes: 4 additions & 2 deletions packages/saber/lib/utils/validateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ module.exports = (config, { dev }) => {
loaderOptions: 'object?',
cssSourceMap: 'boolean?',
lazy: 'boolean?',
outDir: 'string?'
outDir: 'string?',
cache: 'boolean?'
},
{
publicUrl: '/',
extractCSS: false,
loaderOptions: {},
cssSourceMap: false,
lazy: false,
outDir: 'public'
outDir: 'public',
cache: true
}
)

Expand Down
36 changes: 19 additions & 17 deletions packages/saber/lib/webpack/babel-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ module.exports = babelLoader.custom(babel => {
const custom = opts.customLoaderOptions
const filename = path.join(custom.cwd, 'noop.js')
const loader = Object.assign(
{
cacheCompression: false,
cacheDirectory: path.join(
custom.distDir,
'cache',
'saber-babel-loader'
),
cacheIdentifier: JSON.stringify({
key: CACHE_KEY,
type: custom.type,
config: babel.loadPartialConfig({
filename,
cwd: custom.cwd,
sourceFileName: filename
}).options
})
},
custom.shouldCache
? {
cacheCompression: false,
cacheDirectory: path.join(
custom.distDir,
'cache',
'saber-babel-loader'
),
cacheIdentifier: JSON.stringify({
key: CACHE_KEY,
type: custom.type,
config: babel.loadPartialConfig({
filename,
cwd: custom.cwd,
sourceFileName: filename
}).options
})
}
: {},
opts
)
delete loader.customLoaderOptions
Expand Down
1 change: 1 addition & 0 deletions packages/saber/lib/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module.exports = (api, { type }) => {
customLoaderOptions: {
distDir: api.resolveCache(),
cwd: api.resolveCwd(),
shouldCache: api.webpackUtils.shouldCache,
type
}
})
Expand Down
4 changes: 2 additions & 2 deletions packages/saber/vue-renderer/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ class VueRenderer {
transformAssetUrls: {},
prettify: false
},
api.webpackUtils.getCacheOptions('vue-loader', {
api.webpackUtils.getCacheOptions('vue-loader', () => ({
// Increse `key` to invalid cache
key: 0,
type,
'vue-loader': require('vue-loader/package.json').version,
'vue-template-compiler': require('vue-template-compiler/package.json')
.version
})
}))
)
)

Expand Down
8 changes: 8 additions & 0 deletions website/pages/docs/saber-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ interface LoaderOptions {
}
```

### cache

- Type: `boolean`
- Default: `true`
- CLI flag: `--no-cache`

Set to `false` to disable webpack cache.

## plugins

- Type: `Array<Plugin>`
Expand Down

0 comments on commit 6afe8fa

Please sign in to comment.