|
1 | 1 | const path = require('path')
|
| 2 | +const hash = require('hash-sum') |
2 | 3 | const { matchesPluginId } = require('@vue/cli-shared-utils')
|
3 | 4 |
|
4 | 5 | // Note: if a plugin-registered command needs to run in a specific default mode,
|
@@ -114,6 +115,48 @@ class PluginAPI {
|
114 | 115 | resolveChainableWebpackConfig () {
|
115 | 116 | return this.service.resolveChainableWebpackConfig()
|
116 | 117 | }
|
| 118 | + |
| 119 | + /** |
| 120 | + * Generate a cache identifier from a number of variables |
| 121 | + */ |
| 122 | + genCacheConfig (id, partialIdentifier, configFiles) { |
| 123 | + const fs = require('fs') |
| 124 | + const cacheDirectory = this.resolve(`node_modules/.cache/${id}`) |
| 125 | + |
| 126 | + const variables = { |
| 127 | + partialIdentifier, |
| 128 | + 'cli-service': require('../package.json').version, |
| 129 | + 'cache-loader': require('cache-loader/package.json').version, |
| 130 | + env: process.env.NODE_ENV, |
| 131 | + test: !!process.env.VUE_CLI_TEST, |
| 132 | + config: [ |
| 133 | + this.service.projectOptions.chainWebpack, |
| 134 | + this.service.projectOptions.configureWebpack |
| 135 | + ] |
| 136 | + } |
| 137 | + |
| 138 | + if (configFiles) { |
| 139 | + const readConfig = file => { |
| 140 | + const absolutePath = this.resolve(file) |
| 141 | + if (fs.existsSync(absolutePath)) { |
| 142 | + return fs.readFileSync(absolutePath, 'utf-8') |
| 143 | + } |
| 144 | + } |
| 145 | + if (!Array.isArray(configFiles)) { |
| 146 | + configFiles = [configFiles] |
| 147 | + } |
| 148 | + for (const file of configFiles) { |
| 149 | + const content = readConfig(file) |
| 150 | + if (content) { |
| 151 | + variables.configFiles = content |
| 152 | + break |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + const cacheIdentifier = hash(variables) |
| 158 | + return { cacheDirectory, cacheIdentifier } |
| 159 | + } |
117 | 160 | }
|
118 | 161 |
|
119 | 162 | module.exports = PluginAPI
|
0 commit comments