小程序分包
This commit is contained in:
		| @@ -5,8 +5,9 @@ | ||||
|   "author": "Kubbo", | ||||
|   "scripts": { | ||||
|     "dev:mp-weixin": "node bin/serve.js&&cross-env NODE_ENV=development VUE_APP_CW_MODE=dev UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch --minimize", | ||||
|     "lib": "npm unpublish --force&&npm publish&&npm unpublish --workspaces --force&&npm publish --workspaces", | ||||
|     "pages": "node bin/serve.js" | ||||
|     "lib": "npm unpublish --force&&npm publish", | ||||
|     "pages": "node bin/serve.js", | ||||
|     "lib:all": "node project/build.js&&npm unpublish --workspaces --force&&npm publish --workspaces" | ||||
|   }, | ||||
|   "files": [ | ||||
|     "src/mods", | ||||
| @@ -15,7 +16,7 @@ | ||||
|   ], | ||||
|   "workspaces": [ | ||||
|     "src/components", | ||||
|     "src/project/*" | ||||
|     "project/*" | ||||
|   ], | ||||
|   "dependencies": { | ||||
|     "@dcloudio/uni-app-plus": "^2.0.1-33320211224001", | ||||
|   | ||||
							
								
								
									
										124
									
								
								project/build.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										124
									
								
								project/build.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,124 @@ | ||||
| const fsExtra = require('fs-extra') | ||||
| const path = require('path') | ||||
| const chalk = require('chalk') | ||||
| const fs = require('fs') | ||||
| const {exec} = require("child_process"); | ||||
| /** | ||||
|  * 将函数封装成promise | ||||
|  */ | ||||
| const promisify = fn => { | ||||
|   return function () { | ||||
|     let args = arguments; | ||||
|     return new Promise(function (resolve, reject) { | ||||
|       [].push.call(args, function (err, result) { | ||||
|         if (err) { | ||||
|           console.log(err) | ||||
|           reject(err); | ||||
|         } else { | ||||
|           resolve(result); | ||||
|         } | ||||
|       }); | ||||
|       fn.apply(null, args); | ||||
|     }); | ||||
|   } | ||||
| } | ||||
|  | ||||
| const readdir = promisify(fs.readdir) | ||||
| const stat = promisify(fs.stat) | ||||
|  | ||||
| /** | ||||
|  * 封装打印工具 | ||||
|  */ | ||||
| const {log} = console | ||||
| const chalkTag = { | ||||
|   info: msg => log([chalk.bgBlue.black(' INFO '), msg].join(' ')), | ||||
|   done: msg => log([chalk.bgGreen.black(' DONE '), msg].join(' ')), | ||||
|   warn: msg => log([chalk.bgYellow.black(' WARN '), msg].join(' ')), | ||||
|   error: msg => log([chalk.bgRed.black(' ERROR '), msg].join(' ')), | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * 遍历应用的方法 | ||||
|  */ | ||||
| const findApp = (dir, cb) => { | ||||
|   fsExtra.ensureDirSync(dir) | ||||
|   return readdir(dir).then(apps => { | ||||
|     return Promise.all(apps.map(e => { | ||||
|       let cPath = path.join(dir, e) | ||||
|       return stat(cPath).then(state => { | ||||
|         if (state.isDirectory()) { | ||||
|           return findApp(cPath, cb) | ||||
|         } else if (state.isFile()) { | ||||
|           cb && cb(dir) | ||||
|         } | ||||
|       }) | ||||
|     }) || []) | ||||
|   }) | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * 迁移apps文件 | ||||
|  */ | ||||
| const copyFiles = (dir, source = 'src/mods') => { | ||||
|   chalkTag.info(`开始扫描${source}...`) | ||||
|   return new Promise(resolve => { | ||||
|     fsExtra.emptyDir(dir, err => { | ||||
|       if (!err) { | ||||
|         fsExtra.copy(source, dir).then(() => { | ||||
|           chalkTag.done(source + ' 扫描完毕') | ||||
|           resolve() | ||||
|         }) | ||||
|       } | ||||
|     }) | ||||
|   }) | ||||
| } | ||||
| /** | ||||
|  * 初始化打包配置文件 | ||||
|  */ | ||||
| const init = () => { | ||||
|   chalkTag.info('开始运行项目打包工具...') | ||||
|   return new Promise(resolve => fs.readdir('./project', (err, files) => { | ||||
|     resolve(files.filter(e => e.indexOf('.') < 0)) | ||||
|   })) | ||||
| } | ||||
| /** | ||||
|  * 拷贝对应文件 | ||||
|  */ | ||||
| const generateMain = project => { | ||||
|   const getApps = new Promise(resolve => { | ||||
|     const appLib = path.join(__dirname, project.toString(), 'apps.import.json') | ||||
|     fsExtra.readJson(appLib, (err, data) => { | ||||
|       chalkTag.info(project + '加载业务应用配置...') | ||||
|       let apps = [] | ||||
|       if (data) { | ||||
|         fsExtra.emptyDirSync(`project/${project}/apps`) | ||||
|         findApp('src/mods', file => { | ||||
|           apps.push(file) | ||||
|         }).then(() => { | ||||
|           Promise.all(Object.keys(data).map(e => { | ||||
|             let app = [...new Set(apps)].find(s => s.indexOf(e) > -1) | ||||
|             if (app) { | ||||
|               return copyFiles(`project/${project}/apps/${e}`, app) | ||||
|             } | ||||
|           })).then(() => { | ||||
|             chalkTag.done(project + '业务应用加载完成') | ||||
|             resolve() | ||||
|           }) | ||||
|         }) | ||||
|       } else { | ||||
|         chalkTag.done(project + '业务应用无打包') | ||||
|         resolve() | ||||
|       } | ||||
|     }) | ||||
|   }) | ||||
|   return Promise.all([getApps]) | ||||
| } | ||||
| const start = () => { | ||||
|   //询问打包哪个项目 | ||||
|   init().then(choices => { | ||||
|     return Promise.all(choices.map(prj => generateMain(prj))).then(() => { | ||||
|       chalkTag.info('开始发布...') | ||||
|     }) | ||||
|   }) | ||||
| } | ||||
| start(); | ||||
							
								
								
									
										24
									
								
								project/shandong10086/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								project/shandong10086/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| .DS_Store | ||||
| node_modules/ | ||||
| unpackage/ | ||||
| dist/ | ||||
|  | ||||
| # local env files | ||||
| .env.local | ||||
| .env.*.local | ||||
|  | ||||
| # Log files | ||||
| npm-debug.log* | ||||
| yarn-debug.log* | ||||
| yarn-error.log* | ||||
|  | ||||
| # Editor directories and files | ||||
| .project | ||||
| .idea | ||||
| .vscode | ||||
| *.suo | ||||
| *.ntvs* | ||||
| *.njsproj | ||||
| *.sln | ||||
| *.sw* | ||||
| apps/ | ||||
							
								
								
									
										15
									
								
								project/shandong10086/apps.import.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								project/shandong10086/apps.import.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| { | ||||
|   "AppAddressBook": "便民通讯录", | ||||
|   "AppContent": "内容发布", | ||||
|   "AppPhotoReport": "随手拍", | ||||
|   "AppServiceOnlineNew": "网上办事", | ||||
|   "AppReturnHomeRegister": "返乡登记", | ||||
|   "AppHealthReport": "健康上报", | ||||
|   "AppProgressNew": "办事指南", | ||||
|   "AppIntegralApply": "积分申请", | ||||
|   "AppCreditPoints": "信用积分", | ||||
|   "AppSupermarket": "积分超市", | ||||
|   "AppVillageInfo": "一村一群", | ||||
|   "AppVillageActivity": "居民活动", | ||||
|   "AppVideoSurve": "视频监控" | ||||
| } | ||||
							
								
								
									
										9
									
								
								project/shandong10086/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								project/shandong10086/package.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| { | ||||
|   "name": "@dvcp-wechat-apps/shandong10086", | ||||
|   "version": "1.0.0", | ||||
|   "files": [ | ||||
|     "apps" | ||||
|   ], | ||||
|   "dependencies": { | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user