├── .gitignore ├── webpack ├── webpack.common.js ├── webpack.dev.js └── webpack.prod.js ├── github.sh ├── examples ├── static │ └── logo.png ├── main.js ├── pages │ ├── page1 │ │ └── page1.vue │ ├── page2 │ │ └── page2.vue │ └── index │ │ └── index.vue ├── pagesB │ └── detail │ │ └── detail.vue ├── pagesA │ └── list │ │ └── list.vue ├── vue.config.js ├── App.vue ├── pages.json ├── uni.scss ├── common │ └── uni-read-pages-bundle.js └── manifest.json ├── tea.yaml ├── npm-package ├── package.json ├── index.js └── README.md ├── LICENSE ├── package.json ├── dist └── uni-read-pages@1.0.5.js ├── src └── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /examples/node_modules 2 | /examples/unpackage 3 | /node_modules 4 | -------------------------------------------------------------------------------- /webpack/webpack.common.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = { 3 | entry: './src/index.js', 4 | }; -------------------------------------------------------------------------------- /github.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cp -avx ./README.md ./npm-package 4 | cp -avx ./src/* ./npm-package 5 | 6 | -------------------------------------------------------------------------------- /examples/static/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilurianYang/uni-read-pages/HEAD/examples/static/logo.png -------------------------------------------------------------------------------- /tea.yaml: -------------------------------------------------------------------------------- 1 | # https://tea.xyz/what-is-this-file 2 | --- 3 | version: 1.0.0 4 | codeOwners: 5 | - '0x6D4379082b9CEa0D5E074c0a27EBBcE41EEe47DE' 6 | quorum: 1 7 | -------------------------------------------------------------------------------- /examples/main.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import App from './App' 3 | 4 | Vue.config.productionTip = false 5 | 6 | App.mpType = 'app' 7 | 8 | const app = new Vue({ 9 | ...App 10 | }) 11 | app.$mount() 12 | -------------------------------------------------------------------------------- /examples/pages/page1/page1.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 19 | 20 | 23 | -------------------------------------------------------------------------------- /examples/pages/page2/page2.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 19 | 20 | 23 | -------------------------------------------------------------------------------- /examples/pagesB/detail/detail.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 19 | 20 | 23 | -------------------------------------------------------------------------------- /examples/pagesA/list/list.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 19 | 20 | 23 | -------------------------------------------------------------------------------- /examples/vue.config.js: -------------------------------------------------------------------------------- 1 | const TransformPages = require('./common/uni-read-pages-bundle') 2 | const tfPages = new TransformPages() 3 | module.exports = { 4 | configureWebpack: { 5 | plugins: [ 6 | new tfPages.webpack.DefinePlugin({ 7 | ROUTES: JSON.stringify(tfPages.routes) 8 | }) 9 | ] 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /examples/App.vue: -------------------------------------------------------------------------------- 1 | 14 | 15 | 18 | -------------------------------------------------------------------------------- /webpack/webpack.dev.js: -------------------------------------------------------------------------------- 1 | const path = require("path"); 2 | const merge = require("webpack-merge"); 3 | const common = require("./webpack.common.js"); 4 | const CopyPlugin = require('copy-webpack-plugin'); 5 | 6 | 7 | module.exports = merge(common, { 8 | mode: 'development', 9 | output: { 10 | filename: `uni-read-pages-bundle.js`, 11 | path: path.resolve(__dirname, '../', 'examples/common'), 12 | }, 13 | devServer: { 14 | contentBase: "./examples/common" 15 | }, 16 | plugins: [ 17 | new CopyPlugin([{ 18 | context: './src/', 19 | force: true, 20 | from: './index.js', 21 | to: `uni-read-pages-bundle.js`, 22 | globOptions: { 23 | copyUnmodified: true 24 | } 25 | }]), 26 | ] 27 | }); -------------------------------------------------------------------------------- /webpack/webpack.prod.js: -------------------------------------------------------------------------------- 1 | const path = require("path"); 2 | const merge = require("webpack-merge"); 3 | const common = require("./webpack.common.js"); 4 | const rimraf = require("rimraf"); 5 | const CopyPlugin = require('copy-webpack-plugin'); 6 | 7 | const { 8 | version 9 | } = require('../npm-package/package.json'); 10 | 11 | rimraf("dist", () => {}); 12 | 13 | module.exports = merge(common, { 14 | mode: "production", 15 | output: { 16 | filename: `uni-read-pages@${version}.js`, 17 | path: path.resolve(__dirname, '../', 'dist'), 18 | }, 19 | plugins: [ 20 | new CopyPlugin([{ 21 | context: './src/', 22 | from: './index.js', 23 | force:true, 24 | to: `uni-read-pages@${version}.js`, 25 | }, { 26 | context: './src/', 27 | from: './index.js', 28 | force:true, 29 | to: `../npm-package/`, 30 | }]), 31 | ] 32 | }); -------------------------------------------------------------------------------- /npm-package/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "uni-read-pages", 3 | "version": "1.0.5", 4 | "description": "read `pages.json` file to generate the routes table", 5 | "main": "index.js", 6 | "directories": { 7 | "example": "examples" 8 | }, 9 | "scripts": { 10 | "postinstall": "node -e \"console.log('\\x1b[91m','\\n\\n uni-simple-router 垫脚片,欢迎下载!\\n \\n 开源不易,需要鼓励。去给 uni-read-pages 项目 点个 star 吧 \\n\\n')\"", 11 | "dev": "webpack --watch --progress --config webpack/webpack.dev.js", 12 | "build": "webpack --progress --config webpack/webpack.prod.js" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/SilurianYang/uni-read-pages.git" 17 | }, 18 | "keywords": [], 19 | "author": "", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/SilurianYang/uni-read-pages/issues" 23 | }, 24 | "homepage": "https://github.com/SilurianYang/uni-read-pages#readme" 25 | } 26 | -------------------------------------------------------------------------------- /examples/pages.json: -------------------------------------------------------------------------------- 1 | { 2 | "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages 3 | { 4 | "path": "pages/index/index", 5 | "name": "index", 6 | "style": { 7 | "navigationBarTitleText": "uni-app" 8 | } 9 | }, { 10 | "path": "pages/page1/page1", 11 | "name": "page1", 12 | "style": {} 13 | } 14 | // #ifdef MP-WEIXIN 15 | , { 16 | "path": "pages/page2/page2", 17 | "name": "page2", 18 | "style": {} 19 | } 20 | // #endif 21 | ], 22 | "subPackages": [{ 23 | "root": "pagesA", 24 | "pages": [{ 25 | "path": "list/list", 26 | "style": {} 27 | }] 28 | }, { 29 | "root": "pagesB", 30 | "pages": [{ 31 | "path": "detail/detail", 32 | "style": {} 33 | }] 34 | }], 35 | "globalStyle": { 36 | "navigationBarTextStyle": "black", 37 | "navigationBarTitleText": "uni-app", 38 | "navigationBarBackgroundColor": "#F8F8F8", 39 | "backgroundColor": "#F8F8F8" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /examples/pages/index/index.vue: -------------------------------------------------------------------------------- 1 | 9 | 10 | 25 | 26 | 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 hhyang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "uni-read-pages", 3 | "version": "1.0.0", 4 | "description": "read `pages.json` file to generate the routes table", 5 | "main": "index.js", 6 | "directories": { 7 | "example": "examples" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1", 11 | "dev": "webpack --watch --progress --config webpack/webpack.dev.js", 12 | "build": "webpack --progress --config webpack/webpack.prod.js" 13 | }, 14 | "dependencies": { 15 | "@babel/preset-env": "^7.7.6", 16 | "cross-env": "^6.0.3", 17 | "webpack": "^4.41.3", 18 | "webpack-cli": "^3.3.10" 19 | }, 20 | "devDependencies": { 21 | "@babel/core": "^7.7.5", 22 | "babel-loader": "^8.0.6", 23 | "copy-webpack-plugin": "^5.1.1", 24 | "rimraf": "^3.0.0", 25 | "webpack-dev-server": "^3.9.0", 26 | "webpack-merge": "^4.2.2" 27 | }, 28 | "repository": { 29 | "type": "git", 30 | "url": "git+https://github.com/SilurianYang/uni-read-pages.git" 31 | }, 32 | "keywords": [], 33 | "author": "", 34 | "license": "ISC", 35 | "bugs": { 36 | "url": "https://github.com/SilurianYang/uni-read-pages/issues" 37 | }, 38 | "homepage": "https://github.com/SilurianYang/uni-read-pages#readme" 39 | } 40 | -------------------------------------------------------------------------------- /examples/uni.scss: -------------------------------------------------------------------------------- 1 | /** 2 | * 这里是uni-app内置的常用样式变量 3 | * 4 | * uni-app 官方扩展插件及插件市场(https://ext.dcloud.net.cn)上很多三方插件均使用了这些样式变量 5 | * 如果你是插件开发者,建议你使用scss预处理,并在插件代码中直接使用这些变量(无需 import 这个文件),方便用户通过搭积木的方式开发整体风格一致的App 6 | * 7 | */ 8 | 9 | /** 10 | * 如果你是App开发者(插件使用者),你可以通过修改这些变量来定制自己的插件主题,实现自定义主题功能 11 | * 12 | * 如果你的项目同样使用了scss预处理,你也可以直接在你的 scss 代码中使用如下变量,同时无需 import 这个文件 13 | */ 14 | 15 | /* 颜色变量 */ 16 | 17 | /* 行为相关颜色 */ 18 | $uni-color-primary: #007aff; 19 | $uni-color-success: #4cd964; 20 | $uni-color-warning: #f0ad4e; 21 | $uni-color-error: #dd524d; 22 | 23 | /* 文字基本颜色 */ 24 | $uni-text-color:#333;//基本色 25 | $uni-text-color-inverse:#fff;//反色 26 | $uni-text-color-grey:#999;//辅助灰色,如加载更多的提示信息 27 | $uni-text-color-placeholder: #808080; 28 | $uni-text-color-disable:#c0c0c0; 29 | 30 | /* 背景颜色 */ 31 | $uni-bg-color:#ffffff; 32 | $uni-bg-color-grey:#f8f8f8; 33 | $uni-bg-color-hover:#f1f1f1;//点击状态颜色 34 | $uni-bg-color-mask:rgba(0, 0, 0, 0.4);//遮罩颜色 35 | 36 | /* 边框颜色 */ 37 | $uni-border-color:#c8c7cc; 38 | 39 | /* 尺寸变量 */ 40 | 41 | /* 文字尺寸 */ 42 | $uni-font-size-sm:24upx; 43 | $uni-font-size-base:28upx; 44 | $uni-font-size-lg:32upx; 45 | 46 | /* 图片尺寸 */ 47 | $uni-img-size-sm:40upx; 48 | $uni-img-size-base:52upx; 49 | $uni-img-size-lg:80upx; 50 | 51 | /* Border Radius */ 52 | $uni-border-radius-sm: 4upx; 53 | $uni-border-radius-base: 6upx; 54 | $uni-border-radius-lg: 12upx; 55 | $uni-border-radius-circle: 50%; 56 | 57 | /* 水平间距 */ 58 | $uni-spacing-row-sm: 10px; 59 | $uni-spacing-row-base: 20upx; 60 | $uni-spacing-row-lg: 30upx; 61 | 62 | /* 垂直间距 */ 63 | $uni-spacing-col-sm: 8upx; 64 | $uni-spacing-col-base: 16upx; 65 | $uni-spacing-col-lg: 24upx; 66 | 67 | /* 透明度 */ 68 | $uni-opacity-disabled: 0.3; // 组件禁用态的透明度 69 | 70 | /* 文章场景相关 */ 71 | $uni-color-title: #2C405A; // 文章标题颜色 72 | $uni-font-size-title:40upx; 73 | $uni-color-subtitle: #555555; // 二级标题颜色 74 | $uni-font-size-subtitle:36upx; 75 | $uni-color-paragraph: #3F536E; // 文章段落颜色 76 | $uni-font-size-paragraph:30upx; -------------------------------------------------------------------------------- /npm-package/index.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const CONFIG = { 3 | includes: ['path', 'aliasPath', 'name'] 4 | } 5 | const rootPath = path.resolve(process.cwd(), 'node_modules'); 6 | 7 | /** 解析绝对路径 8 | * @param {Object} dir 9 | */ 10 | function resolvePath(dir) { 11 | return path.resolve(rootPath, dir); 12 | } 13 | 14 | class TransformPages { 15 | constructor(config) { 16 | config = { 17 | ...CONFIG, 18 | ...config 19 | }; 20 | this.CONFIG = config; 21 | this.webpack = require(resolvePath('webpack')); 22 | this.uniPagesJSON = require(resolvePath('@dcloudio/uni-cli-shared/lib/pages.js')); 23 | this.routes = this.getPagesRoutes().concat(this.getNotMpRoutes()); 24 | } 25 | /** 26 | * 获取所有pages.json下的内容 返回json 27 | */ 28 | get pagesJson() { 29 | return this.uniPagesJSON.getPagesJson(); 30 | } 31 | /** 32 | * 通过读取pages.json文件 生成直接可用的routes 33 | */ 34 | getPagesRoutes(pages = this.pagesJson.pages, rootPath = null) { 35 | const routes = []; 36 | for (let i = 0; i < pages.length; i++) { 37 | const item = pages[i]; 38 | const route = {}; 39 | for (let j = 0; j < this.CONFIG.includes.length; j++) { 40 | const key = this.CONFIG.includes[j]; 41 | let value = item[key]; 42 | if (key === 'path') { 43 | value = rootPath ? `/${rootPath}/${value}` : `/${value}` 44 | } 45 | if (key === 'aliasPath' && i == 0 && rootPath == null) { 46 | route[key] = route[key] || '/' 47 | } else if (value !== undefined) { 48 | route[key] = value; 49 | } 50 | } 51 | routes.push(route); 52 | } 53 | return routes; 54 | } 55 | /** 56 | * 解析小程序分包路径 57 | */ 58 | getNotMpRoutes() { 59 | const { 60 | subPackages 61 | } = this.pagesJson; 62 | let routes = []; 63 | if (subPackages == null || subPackages.length == 0) { 64 | return []; 65 | } 66 | for (let i = 0; i < subPackages.length; i++) { 67 | const subPages = subPackages[i].pages; 68 | const root = subPackages[i].root; 69 | const subRoutes = this.getPagesRoutes(subPages, root); 70 | routes = routes.concat(subRoutes) 71 | } 72 | return routes 73 | } 74 | /** 75 | * 单条page对象解析 76 | * @param {Object} pageCallback 77 | * @param {Object} subPageCallback 78 | */ 79 | parsePages(pageCallback, subPageCallback) { 80 | this.uniPagesJSON.parsePages(this.pagesJson, pageCallback, subPageCallback) 81 | } 82 | } 83 | module.exports = TransformPages -------------------------------------------------------------------------------- /dist/uni-read-pages@1.0.5.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const CONFIG = { 3 | includes: ['path', 'aliasPath', 'name'] 4 | } 5 | const rootPath = path.resolve(process.cwd(), 'node_modules'); 6 | 7 | /** 解析绝对路径 8 | * @param {Object} dir 9 | */ 10 | function resolvePath(dir) { 11 | return path.resolve(rootPath, dir); 12 | } 13 | 14 | class TransformPages { 15 | constructor(config) { 16 | config = { 17 | ...CONFIG, 18 | ...config 19 | }; 20 | this.CONFIG = config; 21 | this.webpack = require(resolvePath('webpack')); 22 | this.uniPagesJSON = require(resolvePath('@dcloudio/uni-cli-shared/lib/pages.js')); 23 | this.routes = this.getPagesRoutes().concat(this.getNotMpRoutes()); 24 | } 25 | /** 26 | * 获取所有pages.json下的内容 返回json 27 | */ 28 | get pagesJson() { 29 | return this.uniPagesJSON.getPagesJson(); 30 | } 31 | /** 32 | * 通过读取pages.json文件 生成直接可用的routes 33 | */ 34 | getPagesRoutes(pages = this.pagesJson.pages, rootPath = null) { 35 | const routes = []; 36 | for (let i = 0; i < pages.length; i++) { 37 | const item = pages[i]; 38 | const route = {}; 39 | for (let j = 0; j < this.CONFIG.includes.length; j++) { 40 | const key = this.CONFIG.includes[j]; 41 | let value = item[key]; 42 | if (key === 'path') { 43 | value = rootPath ? `/${rootPath}/${value}` : `/${value}` 44 | } 45 | if (key === 'aliasPath' && i == 0 && rootPath == null) { 46 | route[key] = route[key] || '/' 47 | } else if (value !== undefined) { 48 | route[key] = value; 49 | } 50 | } 51 | routes.push(route); 52 | } 53 | return routes; 54 | } 55 | /** 56 | * 解析小程序分包路径 57 | */ 58 | getNotMpRoutes() { 59 | const { 60 | subPackages 61 | } = this.pagesJson; 62 | let routes = []; 63 | if (subPackages == null || subPackages.length == 0) { 64 | return []; 65 | } 66 | for (let i = 0; i < subPackages.length; i++) { 67 | const subPages = subPackages[i].pages; 68 | const root = subPackages[i].root; 69 | const subRoutes = this.getPagesRoutes(subPages, root); 70 | routes = routes.concat(subRoutes) 71 | } 72 | return routes 73 | } 74 | /** 75 | * 单条page对象解析 76 | * @param {Object} pageCallback 77 | * @param {Object} subPageCallback 78 | */ 79 | parsePages(pageCallback, subPageCallback) { 80 | this.uniPagesJSON.parsePages(this.pagesJson, pageCallback, subPageCallback) 81 | } 82 | } 83 | module.exports = TransformPages -------------------------------------------------------------------------------- /examples/common/uni-read-pages-bundle.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const CONFIG = { 3 | includes: ['path', 'aliasPath', 'name'] 4 | } 5 | const rootPath = path.resolve(process.cwd(), 'node_modules'); 6 | 7 | /** 解析绝对路径 8 | * @param {Object} dir 9 | */ 10 | function resolvePath(dir) { 11 | return path.resolve(rootPath, dir); 12 | } 13 | 14 | class TransformPages { 15 | constructor(config) { 16 | config = { 17 | ...CONFIG, 18 | ...config 19 | }; 20 | this.CONFIG = config; 21 | this.webpack = require(resolvePath('webpack')); 22 | this.uniPagesJSON = require(resolvePath('@dcloudio/uni-cli-shared/lib/pages.js')); 23 | this.routes = this.getPagesRoutes().concat(this.getNotMpRoutes()); 24 | } 25 | /** 26 | * 获取所有pages.json下的内容 返回json 27 | */ 28 | get pagesJson() { 29 | return this.uniPagesJSON.getPagesJson(); 30 | } 31 | /** 32 | * 通过读取pages.json文件 生成直接可用的routes 33 | */ 34 | getPagesRoutes(pages = this.pagesJson.pages, rootPath = null) { 35 | const routes = []; 36 | for (let i = 0; i < pages.length; i++) { 37 | const item = pages[i]; 38 | const route = {}; 39 | for (let j = 0; j < this.CONFIG.includes.length; j++) { 40 | const key = this.CONFIG.includes[j]; 41 | let value = item[key]; 42 | if (key === 'path') { 43 | value = rootPath ? `/${rootPath}/${value}` : `/${value}` 44 | } 45 | if (key === 'aliasPath' && i == 0 && rootPath == null) { 46 | route[key] = route[key] || '/' 47 | } else if (value !== undefined) { 48 | route[key] = value; 49 | } 50 | } 51 | routes.push(route); 52 | } 53 | return routes; 54 | } 55 | /** 56 | * 解析小程序分包路径 57 | */ 58 | getNotMpRoutes() { 59 | const { 60 | subPackages 61 | } = this.pagesJson; 62 | let routes = []; 63 | if (subPackages == null || subPackages.length == 0) { 64 | return []; 65 | } 66 | for (let i = 0; i < subPackages.length; i++) { 67 | const subPages = subPackages[i].pages; 68 | const root = subPackages[i].root; 69 | const subRoutes = this.getPagesRoutes(subPages, root); 70 | routes = routes.concat(subRoutes) 71 | } 72 | return routes 73 | } 74 | /** 75 | * 单条page对象解析 76 | * @param {Object} pageCallback 77 | * @param {Object} subPageCallback 78 | */ 79 | parsePages(pageCallback, subPageCallback) { 80 | this.uniPagesJSON.parsePages(this.pagesJson, pageCallback, subPageCallback) 81 | } 82 | } 83 | module.exports = TransformPages -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const CONFIG = { 3 | includes: ['path', 'aliasPath', 'name'] 4 | } 5 | const rootPath = path.resolve(process.cwd(), 'node_modules'); 6 | 7 | /** 解析绝对路径 8 | * @param {Object} dir 9 | */ 10 | function resolvePath(dir) { 11 | return path.resolve(rootPath, dir); 12 | } 13 | 14 | class TransformPages { 15 | constructor(config) { 16 | config = { 17 | ...CONFIG, 18 | ...config 19 | }; 20 | this.CONFIG = config; 21 | this.webpack = require(resolvePath('webpack')); 22 | this.uniPagesJSON = require(resolvePath('@dcloudio/uni-cli-shared/lib/pages.js')); 23 | this.routes = this.getPagesRoutes().concat(this.getNotMpRoutes()); 24 | } 25 | /** 26 | * 获取所有pages.json下的内容 返回json 27 | */ 28 | get pagesJson() { 29 | return this.uniPagesJSON.getPagesJson(); 30 | } 31 | /** 32 | * 通过读取pages.json文件 生成直接可用的routes 33 | */ 34 | getPagesRoutes(pages = this.pagesJson.pages, rootPath = null) { 35 | const routes = []; 36 | for (let i = 0; i < pages.length; i++) { 37 | const item = pages[i]; 38 | const route = {}; 39 | for (let j = 0; j < this.CONFIG.includes.length; j++) { 40 | const key = this.CONFIG.includes[j]; 41 | let value = item[key]; 42 | if (key === 'path') { 43 | value = rootPath ? `/${rootPath}/${value}` : `/${value}` 44 | } 45 | if (key === 'aliasPath' && i == 0 && rootPath == null) { 46 | route[key] = route[key] || '/' 47 | } else if (value !== undefined) { 48 | route[key] = value; 49 | } 50 | } 51 | routes.push(route); 52 | } 53 | return routes; 54 | } 55 | /** 56 | * 解析小程序分包路径 57 | */ 58 | getNotMpRoutes() { 59 | const { 60 | subPackages 61 | } = this.pagesJson; 62 | let routes = []; 63 | if (subPackages == null || subPackages.length == 0) { 64 | return []; 65 | } 66 | for (let i = 0; i < subPackages.length; i++) { 67 | const subPages = subPackages[i].pages; 68 | const root = subPackages[i].root; 69 | const subRoutes = this.getPagesRoutes(subPages, root); 70 | routes = routes.concat(subRoutes) 71 | } 72 | return routes 73 | } 74 | /** 75 | * 单条page对象解析 76 | * @param {Object} pageCallback 77 | * @param {Object} subPageCallback 78 | */ 79 | parsePages(pageCallback, subPageCallback) { 80 | this.uniPagesJSON.parsePages(this.pagesJson, pageCallback, subPageCallback) 81 | } 82 | } 83 | module.exports = TransformPages -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # uni-read-pages 2 | 3 | ![coverage](https://img.shields.io/badge/coverage%20-98%25-green) ![npm](https://img.shields.io/badge/npm%20-v2.6.11-blue) ![license](https://img.shields.io/badge/license-MIT-red) ![size](https://img.shields.io/badge/size-1.48%20kb-yellowgreen) 4 | 5 | 通过 [vue.config.js](https://cli.vuejs.org/zh/config/) 配合此库,可以随心所欲的读取 `pages.json` 下的所有配置 6 | 7 | ## 安装 8 | 9 | 您可以使用 `Yarn` 或 `npm` 安装该软件包(选择一个): 10 | 11 | ##### Yarn 12 | 13 | ```sh 14 | yarn add uni-read-pages 15 | ``` 16 | ##### npm 17 | 18 | ```sh 19 | npm install uni-read-pages 20 | ``` 21 | ## 开始 22 | 配置 `vue.config.js` 通过 `webpack` 注入全局变量 [查看文档](https://www.webpackjs.com/plugins/define-plugin/) 23 | 24 | #### 配置 `vue.config.js` 25 | ```js 26 | //vue.config.js 27 | const TransformPages = require('uni-read-pages') 28 | const tfPages = new TransformPages() 29 | module.exports = { 30 | configureWebpack: { 31 | plugins: [ 32 | new tfPages.webpack.DefinePlugin({ 33 | ROUTES: JSON.stringify(tfPages.routes) 34 | }) 35 | ] 36 | } 37 | } 38 | ``` 39 | 借助`webpack.DefinePlugin` 轻松注入全局变量。`ROUTES` 及可全局使用 40 | 41 | #### 使用 42 | ```js 43 | // xxx.vue 44 | 56 | ``` 57 | ## API 58 | #### options 59 | ```js 60 | //默认值 61 | const CONFIG={ 62 | includes:['path','aliasPath','name'] //需要获取包涵的字段 63 | } 64 | ``` 65 | 66 | #### Instance method 67 | 68 | * **getPagesRoutes** 69 | * 通过读取 `pages.json` 文件 生成直接可用的routes 70 | 71 | * **parsePages(pageCallback, subPageCallback)** 72 | * 单条page对象解析 73 | 74 | * **resolvePath(dir)** 75 | * 解析绝对路径 76 | 77 | #### Instance attr 78 | 79 | * **CONFIG** 80 | * 当前配置项 81 | 82 | * **webpack** 83 | * 当前工程下需要用到 `webpack` 84 | 85 | * **uniPagesJSON** 86 | * 当前 `uni-app` 内置对象,可以通过此属性调用一些内置方法 87 | 88 | * **routes** 89 | * 通过 **includes** 解析后得到的路由表 **可直接使用** 90 | 91 | #### getter 92 | 93 | * **pagesJson** 94 | * 获取所有 `pages.json` 下的内容 返回 `json` 95 | 96 | 97 | #### uniPagesJSON method 98 | 99 | * getMainEntry() 100 | * getNVueMainEntry() 101 | * parsePages (pagesJson, pageCallback, subPageCallback) 102 | * parseEntry (pagesJson) 103 | * getPagesJson() 104 | * parsePagesJson (content, loader) 105 | 106 | #### uniPagesJSON attr 107 | * pagesJsonJsFileName //默认值 pages.js -------------------------------------------------------------------------------- /npm-package/README.md: -------------------------------------------------------------------------------- 1 | # uni-read-pages 2 | 3 | ![coverage](https://img.shields.io/badge/coverage%20-98%25-green) ![npm](https://img.shields.io/badge/npm%20-v2.6.11-blue) ![license](https://img.shields.io/badge/license-MIT-red) ![size](https://img.shields.io/badge/size-1.48%20kb-yellowgreen) 4 | 5 | 通过 [vue.config.js](https://cli.vuejs.org/zh/config/) 配合此库,可以随心所欲的读取 `pages.json` 下的所有配置 6 | 7 | ## 安装 8 | 9 | 您可以使用 `Yarn` 或 `npm` 安装该软件包(选择一个): 10 | 11 | ##### Yarn 12 | 13 | ```sh 14 | yarn add uni-read-pages 15 | ``` 16 | ##### npm 17 | 18 | ```sh 19 | npm install uni-read-pages 20 | ``` 21 | ## 开始 22 | 配置 `vue.config.js` 通过 `webpack` 注入全局变量 [查看文档](https://www.webpackjs.com/plugins/define-plugin/) 23 | 24 | #### 配置 `vue.config.js` 25 | ```js 26 | //vue.config.js 27 | const TransformPages = require('uni-read-pages') 28 | const tfPages = new TransformPages() 29 | module.exports = { 30 | configureWebpack: { 31 | plugins: [ 32 | new tfPages.webpack.DefinePlugin({ 33 | ROUTES: JSON.stringify(tfPages.routes) 34 | }) 35 | ] 36 | } 37 | } 38 | ``` 39 | 借助`webpack.DefinePlugin` 轻松注入全局变量。`ROUTES` 及可全局使用 40 | 41 | #### 使用 42 | ```js 43 | // xxx.vue 44 | 56 | ``` 57 | ## API 58 | #### options 59 | ```js 60 | //默认值 61 | const CONFIG={ 62 | cli:false, //当前是否为脚手架初始化的项目 63 | includes:['path','aliasPath','name'] //需要获取包涵的字段 64 | } 65 | ``` 66 | 67 | #### Instance method 68 | 69 | * **getPagesRoutes** 70 | * 通过读取 `pages.json` 文件 生成直接可用的routes 71 | 72 | * **parsePages(pageCallback, subPageCallback)** 73 | * 单条page对象解析 74 | 75 | * **resolvePath(dir)** 76 | * 解析绝对路径 77 | 78 | #### Instance attr 79 | 80 | * **CONFIG** 81 | * 当前配置项 82 | 83 | * **webpack** 84 | * 当前工程下需要用到 `webpack` 85 | 86 | * **uniPagesJSON** 87 | * 当前 `uni-app` 内置对象,可以通过此属性调用一些内置方法 88 | 89 | * **routes** 90 | * 通过 **includes** 解析后得到的路由表 **可直接使用** 91 | 92 | #### getter 93 | 94 | * **pagesJson** 95 | * 获取所有 `pages.json` 下的内容 返回 `json` 96 | 97 | 98 | #### uniPagesJSON method 99 | 100 | * getMainEntry() 101 | * getNVueMainEntry() 102 | * parsePages (pagesJson, pageCallback, subPageCallback) 103 | * parseEntry (pagesJson) 104 | * getPagesJson() 105 | * parsePagesJson (content, loader) 106 | 107 | #### uniPagesJSON attr 108 | * pagesJsonJsFileName //默认值 pages.js -------------------------------------------------------------------------------- /examples/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "xxxxxx", 3 | "appid" : "__UNI__7CBF75C", 4 | "description": "", 5 | "versionName": "1.0.0", 6 | "versionCode": "100", 7 | "transformPx": false, 8 | /* 5+App特有相关 */ 9 | "app-plus": { 10 | "usingComponents": true, 11 | "nvueCompiler": "uni-app", 12 | "splashscreen": { 13 | "alwaysShowBeforeRender": true, 14 | "waiting": true, 15 | "autoclose": true, 16 | "delay": 0 17 | }, 18 | /* 模块配置 */ 19 | "modules": { 20 | 21 | }, 22 | /* 应用发布信息 */ 23 | "distribute": { 24 | /* android打包配置 */ 25 | "android": { 26 | "permissions": ["", 27 | "", 28 | "", 29 | "", 30 | "", 31 | "", 32 | "", 33 | "", 34 | "", 35 | "", 36 | "", 37 | "", 38 | "", 39 | "", 40 | "", 41 | "", 42 | "", 43 | "", 44 | "", 45 | "", 46 | "", 47 | "" 48 | ] 49 | }, 50 | /* ios打包配置 */ 51 | "ios": { 52 | 53 | }, 54 | /* SDK配置 */ 55 | "sdkConfigs": { 56 | 57 | } 58 | } 59 | }, 60 | /* 快应用特有相关 */ 61 | "quickapp": { 62 | 63 | }, 64 | /* 小程序特有相关 */ 65 | "mp-weixin": { 66 | "appid": "", 67 | "setting": { 68 | "urlCheck": false 69 | }, 70 | "usingComponents": true 71 | }, 72 | "mp-alipay" : { 73 | "usingComponents" : true 74 | }, 75 | "mp-baidu" : { 76 | "usingComponents" : true 77 | }, 78 | "mp-toutiao" : { 79 | "usingComponents" : true 80 | } 81 | } 82 | --------------------------------------------------------------------------------