Skip to content

Commit

Permalink
fix: update cache identifier for loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Sep 28, 2019
1 parent d67e1a9 commit c48d303
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/saber/babel.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/babel/optional-preset')
module.exports = require('./lib/babel/preset')
8 changes: 4 additions & 4 deletions packages/saber/lib/WebpackUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ module.exports = class WebpackUtils {
this.api = api
}

getCacheOptions(dir, obj) {
getCacheOptions(loader, obj) {
return {
cacheDirectory: this.api.resolveCache(path.join('cache', dir)),
cacheDirectory: this.api.resolveCache(path.join('cache', loader)),
cacheIdentifier: obj && JSON.stringify(obj)
}
}

addCacheSupport(rule, dir, obj) {
addCacheSupport(rule, loader, obj) {
rule
.use('cache-loader')
.loader(require.resolve('cache-loader'))
.options(this.getCacheOptions(dir, obj))
.options(this.getCacheOptions(loader, obj))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const path = require('path')
const env = process.env.BABEL_ENV || process.env.NODE_ENV
const isEnvTest = env === 'test'

module.exports = () => {
module.exports = (_, { isServer } = {}) => {
const presets = [
[
require('@babel/preset-env'),
isEnvTest
isEnvTest || isServer
? {
targets: {
node: 'current'
Expand Down
5 changes: 0 additions & 5 deletions packages/saber/lib/babel/required-preset.js

This file was deleted.

63 changes: 42 additions & 21 deletions packages/saber/lib/webpack/babel-loader.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
const path = require('path')
const babelLoader = require('babel-loader')
const { log } = require('saber-log')
const logUpdate = require('log-update')

// increment '0' to invalidate cache
const CACHE_KEY = `babel-cache-0`

module.exports = babelLoader.custom(babel => {
const configs = new Set()
const requiredPreset = babel.createConfigItem(
require('../babel/required-preset'),
{
type: 'preset'
}
)
const optionalPreset = babel.createConfigItem(
require('../babel/optional-preset'),
{
type: 'preset'
}
)

return {
customOptions(opts) {
const custom = opts.customLoaderOptions
delete 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
})
},
opts
)
delete loader.customLoaderOptions

return { loader: opts, custom }
return { loader, custom }
},
config(cfg) {
config(cfg, { customOptions }) {
const options = Object.assign({}, cfg.options)

options.presets.push(requiredPreset)

if (cfg.hasFilesystemConfig()) {
for (const file of [cfg.babelrc, cfg.config]) {
if (file && !configs.has(file)) {
Expand All @@ -39,11 +49,22 @@ module.exports = babelLoader.custom(babel => {
}
}
}
} else {
// Add our optional preset if the no "babelrc" found.
options.presets.push(optionalPreset)
}

options.presets.unshift(
babel.createConfigItem(
[
require('../babel/preset'),
{
isServer: customOptions.type === 'server'
}
],
{
type: 'preset'
}
)
)

return options
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/saber/lib/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ module.exports = (api, { type }) => {
.use('babel-loader')
.loader(require.resolve('./babel-loader'))
.options({
cacheDirectory: api.resolveCache('cache/babel-loader')
customLoaderOptions: {
distDir: api.resolveCache(),
cwd: api.resolveCwd(),
type
}
})

config.plugin('timefix').use(require('time-fix-plugin'))
Expand Down
1 change: 1 addition & 0 deletions packages/saber/vue-renderer/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class VueRenderer {
prettify: false
},
api.webpackUtils.getCacheOptions('vue-loader', {
type,
'vue-loader': require('vue-loader/package.json').version,
'vue-template-compiler': require('vue-template-compiler/package.json')
.version
Expand Down

0 comments on commit c48d303

Please sign in to comment.