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

refactor(cli): json cache

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