Skip to content

Commit

Permalink
fix: fix caching dependency (close #1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 29, 2018
1 parent 071b58b commit 9846cd5
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 61 deletions.
7 changes: 5 additions & 2 deletions packages/@vue/cli-plugin-babel/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = (api, options) => {
const { genCacheConfig } = require('@vue/cli-shared-utils')
const useThreads = process.env.NODE_ENV === 'production' && options.parallel
const cliServicePath = require('path').dirname(require.resolve('@vue/cli-service'))

Expand Down Expand Up @@ -27,7 +26,11 @@ module.exports = (api, options) => {
.end()
.use('cache-loader')
.loader('cache-loader')
.options(genCacheConfig(api, options, 'babel-loader', 'babel.config.js'))
.options(api.genCacheConfig('babel-loader', {
'@babel/core': require('@babel/core/package.json').version,
'@vue/babel-preset-app': require('@vue/babel-preset-app').version,
'babel-loader': require('babel-loader/package.json').version
}, 'babel.config.js'))
.end()

if (useThreads) {
Expand Down
14 changes: 6 additions & 8 deletions packages/@vue/cli-plugin-eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ module.exports = (api, options) => {
// 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'
],
const { cacheIdentifier } = api.genCacheConfig(
'eslint-loader',
{
'eslint-loader': require('eslint-loader/package.json').version,
'eslint': require('eslint/package.json').version
},
[
'.eslintrc.js',
'.eslintrc.yaml',
Expand Down
6 changes: 4 additions & 2 deletions packages/@vue/cli-plugin-typescript/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = (api, options) => {
const fs = require('fs')
const { genCacheConfig } = require('@vue/cli-shared-utils')
const useThreads = process.env.NODE_ENV === 'production' && options.parallel

api.chainWebpack(config => {
Expand All @@ -23,7 +22,10 @@ module.exports = (api, options) => {

addLoader({
loader: 'cache-loader',
options: genCacheConfig(api, options, 'ts-loader', 'tsconfig.json')
options: api.genCacheConfig('ts-loader', {
'ts-loader': require('ts-loader/package.json').version,
'typescript': require('typescript/package.json').versiono
}, 'tsconfig.json')
})

if (useThreads) {
Expand Down
43 changes: 43 additions & 0 deletions packages/@vue/cli-service/lib/PluginAPI.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path')
const hash = require('hash-sum')
const { matchesPluginId } = require('@vue/cli-shared-utils')

// Note: if a plugin-registered command needs to run in a specific default mode,
Expand Down Expand Up @@ -114,6 +115,48 @@ class PluginAPI {
resolveChainableWebpackConfig () {
return this.service.resolveChainableWebpackConfig()
}

/**
* Generate a cache identifier from a number of variables
*/
genCacheConfig (id, partialIdentifier, configFiles) {
const fs = require('fs')
const cacheDirectory = this.resolve(`node_modules/.cache/${id}`)

const variables = {
partialIdentifier,
'cli-service': require('../package.json').version,
'cache-loader': require('cache-loader/package.json').version,
env: process.env.NODE_ENV,
test: !!process.env.VUE_CLI_TEST,
config: [
this.service.projectOptions.chainWebpack,
this.service.projectOptions.configureWebpack
]
}

if (configFiles) {
const readConfig = file => {
const absolutePath = this.resolve(file)
if (fs.existsSync(absolutePath)) {
return fs.readFileSync(absolutePath, 'utf-8')
}
}
if (!Array.isArray(configFiles)) {
configFiles = [configFiles]
}
for (const file of configFiles) {
const content = readConfig(file)
if (content) {
variables.configFiles = content
break
}
}
}

const cacheIdentifier = hash(variables)
return { cacheDirectory, cacheIdentifier }
}
}

module.exports = PluginAPI
8 changes: 5 additions & 3 deletions packages/@vue/cli-service/lib/config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ module.exports = (api, options) => {
// js is handled by cli-plugin-bable ---------------------------------------

// vue-loader --------------------------------------------------------------

const { genCacheConfig } = require('@vue/cli-shared-utils')
const vueLoaderCacheConfig = genCacheConfig(api, options, 'vue-loader')
const vueLoaderCacheConfig = api.genCacheConfig('vue-loader', {
'vue-loader': require('vue-loader/package.json').version,
'@vue/component-compiler-utils': require('@vue/component-compiler-utils/package.json').version,
'vue-template-compiler': require('vue-template-compiler/package.json').version
})

webpackConfig.module
.rule('vue')
Expand Down
1 change: 1 addition & 0 deletions packages/@vue/cli-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"fs-extra": "^6.0.1",
"get-value": "^3.0.1",
"globby": "^8.0.1",
"hash-sum": "^1.0.2",
"html-webpack-plugin": "^3.2.0",
"launch-editor-middleware": "^2.2.1",
"lodash.defaultsdeep": "^4.6.0",
Expand Down
1 change: 0 additions & 1 deletion packages/@vue/cli-shared-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'env',
'exit',
'ipc',
'cache',
'logger',
'openBrowser',
'pluginResolution',
Expand Down
44 changes: 0 additions & 44 deletions packages/@vue/cli-shared-utils/lib/cache.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/@vue/cli-shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"chalk": "^2.3.0",
"cmd-shim": "^2.0.2",
"execa": "^0.10.0",
"hash-sum": "^1.0.2",
"joi": "^12.0.0",
"node-ipc": "^9.1.1",
"opn": "^5.2.0",
Expand Down

0 comments on commit 9846cd5

Please sign in to comment.