diff --git a/packages/uni-cli-shared/lib/cache.js b/packages/uni-cli-shared/lib/cache.js index 7b526016b476ef9dcafd05bea6e930996432ed72..5a19b3a35fcdd544fcc9551b509f6a303da7702a 100644 --- a/packages/uni-cli-shared/lib/cache.js +++ b/packages/uni-cli-shared/lib/cache.js @@ -4,11 +4,11 @@ * 3.script-loader 修改缓存 usingComponents 节点 * 5.webpack plugin 中获取被修改的 page.json,component.json 并 emitFile */ -const jsonFileMap = new Map() +let jsonFileMap = new Map() const changedJsonFileSet = new Set() -const componentSet = new Set() +let componentSet = new Set() -const pageSet = new Set() +let pageSet = new Set() let globalUsingComponents = Object.create(null) let appJsonUsingComponents = Object.create(null) @@ -235,6 +235,32 @@ module.exports = { getJsonFileMap () { return jsonFileMap }, + store () { + const files = Array.from(jsonFileMap.entries()) + const pages = Array.from(pageSet) + const components = Array.from(componentSet) + const methods = componentSpecialMethods + return JSON.stringify({ + files, + pages, + components, + methods, + globalUsingComponents, + appJsonUsingComponents + }) + }, + restore (jsonCache) { + jsonFileMap = new Map(jsonCache.files) + pageSet = new Set(jsonCache.pages) + componentSet = new Set(jsonCache.components) + componentSpecialMethods = jsonCache.methods + globalUsingComponents = jsonCache.globalUsingComponents + appJsonUsingComponents = jsonCache.appJsonUsingComponents + // restore 时,所有 file 均触发 change + for (let name of jsonFileMap.keys()) { + changedJsonFileSet.add(name) + } + }, getJsonFile, getPagesJson, getComponentSet, diff --git a/packages/vue-cli-plugin-uni/lib/cache-loader.js b/packages/vue-cli-plugin-uni/lib/cache-loader.js index a2ad4622a226ad9296baf58cf476827e4230d4d8..0b659b2aeccebe54f4a626f3c71295bae4c03d6e 100644 --- a/packages/vue-cli-plugin-uni/lib/cache-loader.js +++ b/packages/vue-cli-plugin-uni/lib/cache-loader.js @@ -51,7 +51,6 @@ function read (key, callback) { const mpTemplates = data['mpTemplates'] if (mpTemplates) { Object.keys(mpTemplates).forEach(name => { - console.log('read=>write', name) fs.writeFileSync(name, mpTemplates[name], 'utf-8') }) } diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index a7c1a5736f75ba2ae32b6e3d2bce86a3cb888c67..5c5f04c235dbb50ed7b22ca752ca8557e2ca4964 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -1,4 +1,6 @@ +const fs = require('fs') const path = require('path') +const mkdirp = require('mkdirp') // 初始化环境变量 const defaultInputDir = '../../../../src' @@ -119,6 +121,8 @@ if (process.env.UNI_PLATFORM === 'app-plus') { isNVueCompiler = false } if (platformOptions.renderer === 'native') { + // 纯原生目前不提供 cache + process.env.UNI_USING_CACHE = false process.env.UNI_USING_NATIVE = true process.env.UNI_USING_V8 = true process.env.UNI_OUTPUT_TMP_DIR = '' @@ -258,6 +262,27 @@ if (runByHBuilderX) { } } +if (process.env.UNI_USING_CACHE) { // 使用 cache, 拷贝 cache 的 json + const cacheJsonPath = path.resolve( + process.env.UNI_CLI_CONTEXT, + 'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM, + 'cache.json' + ) + 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)) + } + } +} + runByHBuilderX && console.log(`正在编译中...`) module.exports = { 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 148ff8454d02ac99045d6c0dc5c5fa630bce49f8..1352ac9444674533d1df545ca3d8b586fbf877f8 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js @@ -1,3 +1,4 @@ +const fs = require('fs') const path = require('path') const { @@ -82,7 +83,7 @@ function analyzeUsingComponents () { // pages[name] = usingComponentsMap[name] // } // return pages - // }, {}) + // }, {}) } module.exports = function generateJson (compilation) { @@ -153,7 +154,7 @@ module.exports = function generateJson (compilation) { !['app.js', 'manifest.js', 'project.config.js', 'project.swan.js'].includes(jsFile) && !compilation.assets[jsFile] ) { - compilation.assets[jsFile] = { + const jsFileAsset = { size () { return Buffer.byteLength(EMPTY_COMPONENT, 'utf8') }, @@ -161,8 +162,9 @@ module.exports = function generateJson (compilation) { return EMPTY_COMPONENT } } + compilation.assets[jsFile] = jsFileAsset } - compilation.assets[name] = { + const jsonAsset = { size () { return Buffer.byteLength(source, 'utf8') }, @@ -170,5 +172,22 @@ module.exports = function generateJson (compilation) { return source } } + + compilation.assets[name] = jsonAsset + } + 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()) + }, 50) } }