提交 b92c902c 编写于 作者: fxy060608's avatar fxy060608

feat(cli): add json cache

上级 6de497bc
......@@ -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,
......
......@@ -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')
})
}
......
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 = {
......
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)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册