From 27ededb422e769d49981d1bbdfe6212143c8b1fb Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 28 Aug 2019 16:57:47 +0800 Subject: [PATCH] feat(cli): support pages.js #450 --- packages/uni-cli-shared/lib/pages.js | 30 +++++++++++++++---- .../lib/plugin/index-new.js | 28 +++++++++++++---- .../webpack-uni-mp-loader/lib/plugin/index.js | 28 +++++++++++++---- .../webpack-uni-pages-loader/lib/index-new.js | 13 +++++++- .../webpack-uni-pages-loader/lib/index.js | 15 ++++++++-- 5 files changed, 93 insertions(+), 21 deletions(-) diff --git a/packages/uni-cli-shared/lib/pages.js b/packages/uni-cli-shared/lib/pages.js index 29d1f284f..9a1dfed8d 100644 --- a/packages/uni-cli-shared/lib/pages.js +++ b/packages/uni-cli-shared/lib/pages.js @@ -35,8 +35,8 @@ function getPagesJson () { return processPagesJson(getJson('pages.json', true)) } -function parsePagesJson (content) { - return processPagesJson(parseJson(content, true)) +function parsePagesJson (content, loader) { + return processPagesJson(parseJson(content, true), loader) } function filterPages (pages = [], root) { @@ -48,7 +48,25 @@ function filterPages (pages = [], root) { } } -function processPagesJson (pagesJson) { +const pagesJsonJsFileName = 'pages.js' + +function processPagesJson (pagesJson, loader = { + addDependency: function () {} +}) { + const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName) + if (fs.existsSync(pagesJsonJsPath)) { + delete require.cache[pagesJsonJsPath] + const pagesJsonJsFn = require(pagesJsonJsPath) + if (typeof pagesJsonJsFn === 'function') { + pagesJson = pagesJsonJsFn(pagesJson, loader) + if (!pagesJson) { + console.error(`${pagesJsonJsFileName} 必须返回一个 json 对象`) + } + } else { + console.error(`${pagesJsonJsFileName} 必须导出 function`) + } + } + let uniNVueEntryPagePath if (pagesJson.pages && pagesJson.pages.length) { // 如果首页是 nvue if (isNVuePage(pagesJson.pages[0])) { @@ -86,7 +104,7 @@ function isNVuePage (page, root = '') { } function isValidPage (page, root = '') { - if (typeof page === 'string') { // 不合法的配置 + if (typeof page === 'string') { // 不合法的配置 console.warn(`${page} 配置错误, 已被忽略, 查看文档: https://uniapp.dcloud.io/collocation/pages?id=pages`) return false } @@ -193,7 +211,6 @@ function parseEntry (pagesJson) { process.UNI_NVUE_ENTRY = {} if (process.env.UNI_USING_NATIVE) { - // TODO 考虑 pages.json.js process.UNI_NVUE_ENTRY['app-config'] = path.resolve(process.env.UNI_INPUT_DIR, 'pages.json') process.UNI_NVUE_ENTRY['app-service'] = path.resolve(process.env.UNI_INPUT_DIR, getMainEntry()) } @@ -231,5 +248,6 @@ module.exports = { parsePages, parseEntry, getPagesJson, - parsePagesJson + parsePagesJson, + pagesJsonJsFileName } diff --git a/packages/webpack-uni-mp-loader/lib/plugin/index-new.js b/packages/webpack-uni-mp-loader/lib/plugin/index-new.js index c862e39b2..fb78e0e05 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/index-new.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/index-new.js @@ -6,6 +6,10 @@ const { normalizePath } = require('@dcloudio/uni-cli-shared') +const { + pagesJsonJsFileName +} = require('@dcloudio/uni-cli-shared/lib/pages') + const generateApp = require('./generate-app') const generateJson = require('./generate-json') const generateComponent = require('./generate-component') @@ -43,7 +47,8 @@ function addSubPackagesRequire (compilation) { name.indexOf(root) === 0 && name !== subPackageVendorPath ) { - const source = `require('${normalizePath(path.relative(path.dirname(name), subPackageVendorPath))}');` + + const source = + `require('${normalizePath(path.relative(path.dirname(name), subPackageVendorPath))}');` + compilation.assets[name].source() compilation.assets[name] = { @@ -82,11 +87,22 @@ class WebpackUniMPPlugin { }) compiler.hooks.invalid.tap('webpack-uni-mp-invalid', (fileName, changeTime) => { - if (fileName && typeof fileName === 'string' && path.basename(fileName) === 'pages.json') { // 重新解析 entry - try { - parseEntry() - } catch (e) { - console.error(e) + if ( + fileName && + typeof fileName === 'string' + ) { // 重新解析 entry + const basename = path.basename(fileName) + const deps = process.UNI_PAGES_DEPS || new Set() + if ( + basename === 'pages.json' || + basename === pagesJsonJsFileName || + deps.has(normalizePath(fileName)) + ) { + try { + parseEntry() + } catch (e) { + console.error(e) + } } } }) diff --git a/packages/webpack-uni-mp-loader/lib/plugin/index.js b/packages/webpack-uni-mp-loader/lib/plugin/index.js index 55d87c83f..1068c881c 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/index.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/index.js @@ -2,9 +2,14 @@ const path = require('path') const { md5, - parseEntry + parseEntry, + normalizePath } = require('@dcloudio/uni-cli-shared') +const { + pagesJsonJsFileName +} = require('@dcloudio/uni-cli-shared/lib/pages') + const { getPages, getSubPages, @@ -93,11 +98,22 @@ class WebpackUniMPPlugin { }) compiler.hooks.invalid.tap('webpack-uni-mp-invalid', (fileName, changeTime) => { - if (fileName && typeof fileName === 'string' && path.basename(fileName) === 'pages.json') { // 重新解析 entry - try { - parseEntry() - } catch (e) { - console.error(e) + if ( + fileName && + typeof fileName === 'string' + ) { // 重新解析 entry + const basename = path.basename(fileName) + const deps = process.UNI_PAGES_DEPS || new Set() + if ( + basename === 'pages.json' || + basename === pagesJsonJsFileName || + deps.has(normalizePath(fileName)) + ) { + try { + parseEntry() + } catch (e) { + console.error(e) + } } } }) diff --git a/packages/webpack-uni-pages-loader/lib/index-new.js b/packages/webpack-uni-pages-loader/lib/index-new.js index 81b4a617b..25d781a7e 100644 --- a/packages/webpack-uni-pages-loader/lib/index-new.js +++ b/packages/webpack-uni-pages-loader/lib/index-new.js @@ -14,6 +14,10 @@ const { updateProjectJson } = require('@dcloudio/uni-cli-shared/lib/cache') +const { + pagesJsonJsFileName +} = require('@dcloudio/uni-cli-shared/lib/pages') + const parseStyle = require('./util').parseStyle // 将开发者手动设置的 usingComponents 调整名称,方便与自动解析到的 usingComponents 做最后合并 @@ -28,12 +32,19 @@ function renameUsingComponents (jsonObj) { module.exports = function (content) { this.cacheable && this.cacheable() + const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName) const manifestJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json') const manifestJson = parseManifestJson(fs.readFileSync(manifestJsonPath, 'utf8')) + this.addDependency(pagesJsonJsPath) this.addDependency(manifestJsonPath) - const pagesJson = parsePagesJson(content) + const pagesJson = parsePagesJson(content, { + addDependency: (file) => { + (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(normalizePath(file)) + this.addDependency(file) + } + }) // TODO 与 usingComponents 放在一块读取设置 if (manifestJson.transformPx === false) { process.UNI_TRANSFORM_PX = false diff --git a/packages/webpack-uni-pages-loader/lib/index.js b/packages/webpack-uni-pages-loader/lib/index.js index 0a8f56b6e..83d27c4c3 100644 --- a/packages/webpack-uni-pages-loader/lib/index.js +++ b/packages/webpack-uni-pages-loader/lib/index.js @@ -13,6 +13,10 @@ const { getPagesJson } = require('@dcloudio/uni-cli-shared/lib/cache') +const { + pagesJsonJsFileName +} = require('@dcloudio/uni-cli-shared/lib/pages') + const parseStyle = require('./util').parseStyle const emitFileCaches = {} @@ -25,7 +29,7 @@ function checkEmitFile (filePath, jsonObj, changedEmitFiles) { } } -module.exports = function (content) { +module.exports = function (content) { if (this.resourceQuery) { const params = loaderUtils.parseQuery(this.resourceQuery) if (params) { @@ -44,11 +48,18 @@ module.exports = function (content) { this.cacheable && this.cacheable() const manifestJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json') + const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName) const manifestJson = parseManifestJson(fs.readFileSync(manifestJsonPath, 'utf8')) this.addDependency(manifestJsonPath) + this.addDependency(pagesJsonJsPath) - const pagesJson = parsePagesJson(content) + const pagesJson = parsePagesJson(content, { + addDependency: (file) => { + (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(normalizePath(file)) + this.addDependency(file) + } + }) if (manifestJson.transformPx === false) { process.UNI_TRANSFORM_PX = false -- GitLab