Skip to content

Commit ff8acd1

Browse files
committed
refactor: caching identifier generation
1 parent d25dd9f commit ff8acd1

File tree

3 files changed

+56
-46
lines changed

3 files changed

+56
-46
lines changed
Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1-
module.exports = (api, { lintOnSave }) => {
2-
if (lintOnSave) {
1+
module.exports = (api, options) => {
2+
if (options.lintOnSave) {
33
const extensions = require('./eslintOptions').extensions(api)
4-
const cacheIdentifier = genCacheIdentifier(api.resolve('.'))
4+
5+
// eslint-loader doesn't bust cache when eslint config changes
6+
// so we have to manually generate a cache identifier that takes the config
7+
// into account.
8+
const { genCacheConfig } = require('@vue/cli-shared-utils')
9+
const { cacheIdentifier } = genCacheConfig(
10+
api,
11+
options,
12+
[
13+
'eslint-loader',
14+
'eslint'
15+
],
16+
[
17+
'.eslintrc.js',
18+
'.eslintrc.yaml',
19+
'.eslintrc.yml',
20+
'.eslintrc.json',
21+
'.eslintrc',
22+
'package.json'
23+
]
24+
)
525

626
api.chainWebpack(webpackConfig => {
727
webpackConfig.module
@@ -18,7 +38,7 @@ module.exports = (api, { lintOnSave }) => {
1838
extensions,
1939
cache: true,
2040
cacheIdentifier,
21-
emitWarning: lintOnSave !== 'error',
41+
emitWarning: options.lintOnSave !== 'error',
2242
formatter: require('eslint/lib/formatters/codeframe')
2343
})
2444
})
@@ -38,33 +58,3 @@ module.exports = (api, { lintOnSave }) => {
3858
require('./lint')(args, api)
3959
})
4060
}
41-
42-
// eslint-loader doesn't bust cache when eslint config changes
43-
// so we have to manually generate a cache identifier that takes the config
44-
// into account.
45-
function genCacheIdentifier (context) {
46-
const fs = require('fs')
47-
const path = require('path')
48-
const files = [
49-
'.eslintrc.js',
50-
'.eslintrc.yaml',
51-
'.eslintrc.yml',
52-
'.eslintrc.json',
53-
'.eslintrc',
54-
'package.json'
55-
]
56-
57-
const configTimeStamp = (() => {
58-
for (const file of files) {
59-
if (fs.existsSync(path.join(context, file))) {
60-
return fs.statSync(file).mtimeMs
61-
}
62-
}
63-
})()
64-
65-
return JSON.stringify({
66-
'eslint-loader': require('eslint-loader/package.json').version,
67-
'eslint': require('eslint/package.json').version,
68-
'config': configTimeStamp
69-
})
70-
}
Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
const fs = require('fs')
2-
const path = require('path')
2+
const hash = require('hash-sum')
33

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

911
const variables = {
10-
[id]: require(`${id}/package.json`).version,
1112
'cache-loader': require('cache-loader/package.json').version,
1213
env: process.env.NODE_ENV,
1314
test: !!process.env.VUE_CLI_TEST,
14-
config: (options.chainWebpack || '').toString() + (options.configureWebpack || '').toString()
15+
config: [options.chainWebpack, options.configureWebpack]
16+
}
17+
18+
for (const dep of deps) {
19+
variables[dep] = require(`${dep}/package.json`).version
20+
}
21+
22+
const readConfig = file => {
23+
const absolutePath = api.resolve(file)
24+
if (fs.existsSync(absolutePath)) {
25+
return fs.readFileSync(absolutePath, 'utf-8')
26+
}
1527
}
16-
if (configFile) {
17-
const file = api.resolve(configFile)
18-
if (fs.existsSync(file)) {
19-
variables.configFile = fs.readFileSync(configFile, 'utf-8')
28+
29+
if (configFiles) {
30+
if (!Array.isArray(configFiles)) {
31+
configFiles = [configFiles]
32+
}
33+
for (const file of configFiles) {
34+
const content = readConfig(file)
35+
if (content) {
36+
variables.configFiles = content
37+
break
38+
}
2039
}
2140
}
22-
const cacheIdentifier = JSON.stringify(variables)
2341

42+
const cacheIdentifier = hash(variables)
2443
return { cacheDirectory, cacheIdentifier }
2544
}

packages/@vue/cli-shared-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"chalk": "^2.3.0",
2323
"cmd-shim": "^2.0.2",
2424
"execa": "^0.9.0",
25+
"hash-sum": "^1.0.2",
2526
"joi": "^12.0.0",
2627
"opn": "^5.2.0",
2728
"ora": "^1.3.0",

0 commit comments

Comments
 (0)