diff --git a/packages/uni-cli-shared/lib/cache.js b/packages/uni-cli-shared/lib/cache.js index 5a19b3a35fcdd544fcc9551b509f6a303da7702a..8241a0b93f5f0b83f08c017f8d7fd344f16f0b27 100644 --- a/packages/uni-cli-shared/lib/cache.js +++ b/packages/uni-cli-shared/lib/cache.js @@ -1,3 +1,6 @@ +const fs = require('fs') +const path = require('path') +const crypto = require('crypto') /** * 1.page-loader 缓存基础的 app.json page.json project.config.json * 2.main-loader 缓存 app.json 中的 usingComponents 节点 @@ -228,6 +231,27 @@ function getSpecialMethods (name) { return componentSpecialMethods[name] || [] } +const pagesJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'pages.json') + +const cacheTypes = ['babel-loader', 'css-loader', 'uni-template-compiler', 'vue-loader'] + +function clearCache () { + const fsExtra = require('fs-extra') + cacheTypes.forEach(cacheType => { + fsExtra.emptyDirSync(path.resolve( + process.env.UNI_CLI_CONTEXT, + 'node_modules/.cache/' + cacheType + '/' + process.env.UNI_PLATFORM + )) + }) +} + +function digest (str) { + return crypto + .createHash('md5') + .update(str) + .digest('hex') +} + module.exports = { getPageSet () { return pageSet @@ -235,21 +259,50 @@ module.exports = { getJsonFileMap () { return jsonFileMap }, + // 先简单处理,该方案不好, + // 后续为 pages-loader 增加 cache-loader, + // 然后其他修改 json 的地方也要定制 cache-loader store () { + const filepath = path.resolve( + process.env.UNI_CLI_CONTEXT, + 'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM, + digest(process.env.UNI_INPUT_DIR) + '.json' + ) + const files = Array.from(jsonFileMap.entries()) const pages = Array.from(pageSet) const components = Array.from(componentSet) const methods = componentSpecialMethods - return JSON.stringify({ + fs.writeFileSync(filepath, JSON.stringify({ + mtimeMs: fs.statSync(pagesJsonPath).mtimeMs, files, pages, components, methods, globalUsingComponents, appJsonUsingComponents - }) + })) }, - restore (jsonCache) { + restore () { + const filepath = path.resolve( + process.env.UNI_CLI_CONTEXT, + 'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM, + digest(process.env.UNI_INPUT_DIR) + '.json' + ) + if (!fs.existsSync(filepath)) { + try { + clearCache() + } catch (e) {} + return + } + const mtimeMs = fs.statSync(pagesJsonPath).mtimeMs + const jsonCache = require(filepath) + if (jsonCache.mtimeMs !== mtimeMs) { + try { + clearCache() + } catch (e) {} + return + } jsonFileMap = new Map(jsonCache.files) pageSet = new Set(jsonCache.pages) componentSet = new Set(jsonCache.components) @@ -260,6 +313,7 @@ module.exports = { for (let name of jsonFileMap.keys()) { changedJsonFileSet.add(name) } + return true }, getJsonFile, getPagesJson, diff --git a/packages/vue-cli-plugin-hbuilderx/module-alias.js b/packages/vue-cli-plugin-hbuilderx/module-alias.js index 259886c16e1595d4046e9ad639dd6e2400b5320d..385c148395f0a81b5be4e79dca624bd3824858f3 100644 --- a/packages/vue-cli-plugin-hbuilderx/module-alias.js +++ b/packages/vue-cli-plugin-hbuilderx/module-alias.js @@ -20,12 +20,18 @@ moduleAlias.addAlias('./templateLoader', (fromPath, request, alias) => { return request }) // vue cache -moduleAlias.addAlias('./loaders/pitcher', (fromPath, request, alias) => { - if (fromPath.indexOf('vue-loader') !== -1) { - return path.resolve(__dirname, 'packages/vue-loader/lib/loaders/pitcher') - } - return request -}) +if ( // 非 h5 ,非 v3,非 native + process.env.UNI_PLATFORM !== 'h5' && + !process.env.UNI_USING_V3 && + !process.env.UNI_USING_NATIVE +) { + moduleAlias.addAlias('./loaders/pitcher', (fromPath, request, alias) => { + if (fromPath.indexOf('vue-loader') !== -1) { + return path.resolve(__dirname, 'packages/vue-loader/lib/loaders/pitcher') + } + return request + }) +} if (isInHBuilderX) { moduleAlias.addAlias('typescript', path.resolve(process.env.UNI_HBUILDERX_PLUGINS, diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index 5c5f04c235dbb50ed7b22ca752ca8557e2ca4964..f41a526cc7c1025cb1c124ddb2b3e4e2c914303f 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -262,24 +262,20 @@ if (runByHBuilderX) { } } -if (process.env.UNI_USING_CACHE) { // 使用 cache, 拷贝 cache 的 json - const cacheJsonPath = path.resolve( +if ( + process.env.UNI_USING_CACHE && + process.env.UNI_PLATFORM !== 'h5' && + !process.env.UNI_USING_V3 && + !process.env.UNI_USING_NATIVE +) { // 使用 cache, 拷贝 cache 的 json + const cacheJsonDir = path.resolve( process.env.UNI_CLI_CONTEXT, - 'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM, - 'cache.json' + 'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM ) - const cacheJsonDir = path.dirname(cacheJsonPath) - if (!fs.existsSync(cacheJsonDir)) { // 创建 cache 目录 mkdirp(cacheJsonDir) } else { - if (fs.existsSync(cacheJsonPath)) { - // 设置 json 缓存 - const { - restore - } = require('@dcloudio/uni-cli-shared/lib/cache') - restore(require(cacheJsonPath)) - } + require('@dcloudio/uni-cli-shared/lib/cache').restore() } } diff --git a/packages/vue-cli-plugin-uni/lib/h5/index.js b/packages/vue-cli-plugin-uni/lib/h5/index.js index 7a16c26b46f01190f78f175755a68a4aa83e66e7..db9ebde24ae18282255dccf72e6d9fd575def9b9 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/index.js +++ b/packages/vue-cli-plugin-uni/lib/h5/index.js @@ -7,7 +7,7 @@ const { getPlatformCssnano } = require('@dcloudio/uni-cli-shared') -const modifyVueLoader = require('./vue-loader') +const modifyVueLoader = require('../vue-loader') const WebpackHtmlAppendPlugin = require('../../packages/webpack-html-append-plugin') diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js index 1352ac9444674533d1df545ca3d8b586fbf877f8..c7d7ca3d774209292fe439b2a0342dbefa0060f5 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js @@ -1,4 +1,3 @@ -const fs = require('fs') const path = require('path') const { @@ -177,17 +176,7 @@ module.exports = function generateJson (compilation) { } if (process.env.UNI_USING_CACHE && jsonFileMap.size) { setTimeout(() => { - const { - store - } = require('@dcloudio/uni-cli-shared/lib/cache') - - const filepath = path.resolve( - process.env.UNI_CLI_CONTEXT, - 'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM, - 'cache.json' - ) - // 异步写入 cache,避免影响热更新性能 - fs.writeFileSync(filepath, store()) + require('@dcloudio/uni-cli-shared/lib/cache').store() }, 50) } }