Skip to content

Commit

Permalink
refactor: caching identifier generation
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 28, 2018
1 parent d25dd9f commit ff8acd1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 46 deletions.
58 changes: 24 additions & 34 deletions packages/@vue/cli-plugin-eslint/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
module.exports = (api, { lintOnSave }) => {
if (lintOnSave) {
module.exports = (api, options) => {
if (options.lintOnSave) {
const extensions = require('./eslintOptions').extensions(api)
const cacheIdentifier = genCacheIdentifier(api.resolve('.'))

// eslint-loader doesn't bust cache when eslint config changes
// so we have to manually generate a cache identifier that takes the config
// into account.
const { genCacheConfig } = require('@vue/cli-shared-utils')
const { cacheIdentifier } = genCacheConfig(
api,
options,
[
'eslint-loader',
'eslint'
],
[
'.eslintrc.js',
'.eslintrc.yaml',
'.eslintrc.yml',
'.eslintrc.json',
'.eslintrc',
'package.json'

This comment has been minimized.

Copy link
@Akryum

Akryum May 28, 2018

Member

Could a plugin customize this list? For example, eslint-plugin-graphql needs to take into account the GraphQL Schema as well in the cache.

This comment has been minimized.

Copy link
@yyx990803

yyx990803 May 28, 2018

Author Member

You can override cacheIdentifier by tapping eslint-loader.

This comment has been minimized.

This comment has been minimized.

Copy link
@yyx990803

yyx990803 May 28, 2018

Author Member

Yeah that should work

]
)

api.chainWebpack(webpackConfig => {
webpackConfig.module
Expand All @@ -18,7 +38,7 @@ module.exports = (api, { lintOnSave }) => {
extensions,
cache: true,
cacheIdentifier,
emitWarning: lintOnSave !== 'error',
emitWarning: options.lintOnSave !== 'error',
formatter: require('eslint/lib/formatters/codeframe')
})
})
Expand All @@ -38,33 +58,3 @@ module.exports = (api, { lintOnSave }) => {
require('./lint')(args, api)
})
}

// eslint-loader doesn't bust cache when eslint config changes
// so we have to manually generate a cache identifier that takes the config
// into account.
function genCacheIdentifier (context) {
const fs = require('fs')
const path = require('path')
const files = [
'.eslintrc.js',
'.eslintrc.yaml',
'.eslintrc.yml',
'.eslintrc.json',
'.eslintrc',
'package.json'
]

const configTimeStamp = (() => {
for (const file of files) {
if (fs.existsSync(path.join(context, file))) {
return fs.statSync(file).mtimeMs
}
}
})()

return JSON.stringify({
'eslint-loader': require('eslint-loader/package.json').version,
'eslint': require('eslint/package.json').version,
'config': configTimeStamp
})
}
43 changes: 31 additions & 12 deletions packages/@vue/cli-shared-utils/lib/cache.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
const fs = require('fs')
const path = require('path')
const hash = require('hash-sum')

exports.genCacheConfig = (api, options, id, configFile) => {
const cacheDirectory = process.env.VUE_CLI_TEST
? path.resolve(__dirname, `../../../../node_modules/.cache/${id}`)
: api.resolve(`node_modules/.cache/${id}`)
exports.genCacheConfig = (api, options, deps, configFiles) => {
if (!Array.isArray(deps)) {
deps = [deps]
}
const id = deps[0]
const cacheDirectory = api.resolve(`node_modules/.cache/${id}`)

const variables = {
[id]: require(`${id}/package.json`).version,
'cache-loader': require('cache-loader/package.json').version,
env: process.env.NODE_ENV,
test: !!process.env.VUE_CLI_TEST,
config: (options.chainWebpack || '').toString() + (options.configureWebpack || '').toString()
config: [options.chainWebpack, options.configureWebpack]
}

for (const dep of deps) {
variables[dep] = require(`${dep}/package.json`).version
}

const readConfig = file => {
const absolutePath = api.resolve(file)
if (fs.existsSync(absolutePath)) {
return fs.readFileSync(absolutePath, 'utf-8')
}
}
if (configFile) {
const file = api.resolve(configFile)
if (fs.existsSync(file)) {
variables.configFile = fs.readFileSync(configFile, 'utf-8')

if (configFiles) {
if (!Array.isArray(configFiles)) {
configFiles = [configFiles]
}
for (const file of configFiles) {
const content = readConfig(file)
if (content) {
variables.configFiles = content
break
}
}
}
const cacheIdentifier = JSON.stringify(variables)

const cacheIdentifier = hash(variables)
return { cacheDirectory, cacheIdentifier }
}
1 change: 1 addition & 0 deletions packages/@vue/cli-shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"chalk": "^2.3.0",
"cmd-shim": "^2.0.2",
"execa": "^0.9.0",
"hash-sum": "^1.0.2",
"joi": "^12.0.0",
"opn": "^5.2.0",
"ora": "^1.3.0",
Expand Down

0 comments on commit ff8acd1

Please sign in to comment.