关于uniapp:uniapp-实现pagesjson的模块加载

51次阅读

共计 1395 个字符,预计需要花费 4 分钟才能阅读完成。

对于一个日渐宏大的下 uni-app 我的项目,pages.json 文件会变得有大有难以保护,所有就思考怎么样将 pages.json 文件才分易保护的小文件。
起初在浏览他的源码时发现了这个

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}  ${uniI18n.__('cliShared.requireReturnJsonObject')}`)
      }
    } else {console.error(`${pagesJsonJsFileName} ${uniI18n.__('cliShared.requireExportFunction')}`)
    }
  }
  // 将 subpackages 转换成 subPackages
  if (pagesJson.subpackages && !pagesJson.subPackages) {
    pagesJson.subPackages = pagesJson.subpackages
    delete pagesJson.subpackages
  }

  let uniNVueEntryPagePath
  if (pagesJson.pages && pagesJson.pages.length) { // 如果首页是 nvue
    if (isNVuePage(pagesJson.pages[0])) {uniNVueEntryPagePath = pagesJson.pages[0].path
    }
  }
  // pages
  filterPages(pagesJson.pages)
  // subPackages
  if (Array.isArray(pagesJson.subPackages) && pagesJson.subPackages.length) {
    pagesJson.subPackages.forEach(subPackage => {filterPages(subPackage.pages, subPackage.root)
    })
  }

  if (uniNVuePages.length) { // 间接挂在 pagesJson 上
    pagesJson.nvue = {pages: uniNVuePages.reverse()
    }
    if (uniNVueEntryPagePath) {pagesJson.nvue.entryPagePath = uniNVueEntryPagePath}
  }
  return pagesJson
}

于是就有上面这个工具
仓库地址
uni-module-pages

正文完
 0