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

feat(cli): add uni_modules.js

上级 829df827
......@@ -9,10 +9,13 @@ const {
} = require('./util')
const {
getJson,
parseJson
} = require('./json')
const {
getPagesJson
} = require('./uni_modules')
let mainEntry = ''
let nvueMainEntry = ''
......@@ -33,10 +36,6 @@ function getNVueMainEntry () {
return nvueMainEntry
}
function getPagesJson () {
return processPagesJson(getJson('pages.json', true))
}
function parsePagesJson (content, loader) {
return processPagesJson(parseJson(content, true), loader)
}
......
......@@ -3,8 +3,9 @@ const path = require('path')
const {
parseJson
} = require('@dcloudio/uni-cli-shared/lib/json')
const merge = require('@dcloudio/uni-cli-shared/lib/pages-json').default
} = require('./json')
const merge = require('./pages-json').default
function normalizeUniModulesPagesJson (pagesJson, pluginId) {
if (Array.isArray(pagesJson.pages)) {
......@@ -20,28 +21,31 @@ function normalizeUniModulesPagesJson (pagesJson, pluginId) {
return pagesJson
}
module.exports = function parsePages (content) {
const uniModulesDir = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules')
const pluginPagesJsons = []
global.uniModules.forEach(plugin => {
const pagesJsonPath = path.resolve(uniModulesDir, plugin, 'pages.json')
if (fs.existsSync(pagesJsonPath)) {
pluginPagesJsons.push(
normalizeUniModulesPagesJson(parseJson(fs.readFileSync(pagesJsonPath).toString(), true), plugin)
)
}
})
if (pluginPagesJsons.length) {
module.exports = {
getPagesJson (content) {
const uniModulesDir = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules')
const pluginPagesJsons = []
global.uniModules.forEach(plugin => {
const pagesJsonPath = path.resolve(uniModulesDir, plugin, 'pages.json')
if (fs.existsSync(pagesJsonPath)) {
pluginPagesJsons.push(
normalizeUniModulesPagesJson(parseJson(fs.readFileSync(pagesJsonPath).toString(), true), plugin)
)
}
})
content = content || fs.readFileSync(path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'), 'utf8')
const mainPagesJson = parseJson(content, true)
const pagesJson = merge(pluginPagesJsons.concat(mainPagesJson))
if (Array.isArray(mainPagesJson.pages)) { // entry page
const entryPagePath = mainPagesJson.pages[0].path
const index = pagesJson.pages.findIndex(page => page.path === entryPagePath)
const entryPage = pagesJson.pages[index]
pagesJson.pages.splice(index, 1)
pagesJson.pages.unshift(entryPage)
if (pluginPagesJsons.length) {
const pagesJson = merge(pluginPagesJsons.concat(mainPagesJson))
if (Array.isArray(mainPagesJson.pages)) { // entry page
const entryPagePath = mainPagesJson.pages[0].path
const index = pagesJson.pages.findIndex(page => page.path === entryPagePath)
const entryPage = pagesJson.pages[index]
pagesJson.pages.splice(index, 1)
pagesJson.pages.unshift(entryPage)
}
return pagesJson
}
return JSON.stringify(pagesJson)
return mainPagesJson
}
return content
}
......@@ -30,7 +30,7 @@ function checkEmitFile (filePath, jsonObj, changedEmitFiles) {
}
module.exports = function (content, map) {
content = require('./uni_modules.js')(content)
content = JSON.stringify(require('@dcloudio/uni-cli-shared/lib/uni_modules').getPagesJson(content))
if (this.resourceQuery) {
const params = loaderUtils.parseQuery(this.resourceQuery)
if (params) {
......@@ -41,6 +41,13 @@ module.exports = function (content, map) {
}
}
}
// add deps
global.uniModules.forEach(module => {
const uniModulePagesJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', module, 'pages.json')
if (fs.existsSync(uniModulePagesJsonPath)) {
this.addDependency(uniModulePagesJsonPath)
}
})
if (
process.env.UNI_USING_COMPONENTS ||
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册