From 978381a92348dd342d7456343b17e06316335bd5 Mon Sep 17 00:00:00 2001 From: zwwill Date: Wed, 5 Dec 2018 14:32:40 +0800 Subject: [PATCH] fix: entry catalog for .vue --- lib/{ => func}/getSubPackagesRoot.js | 2 +- lib/func/pagesEntry.js | 49 ++++++++++++++++++++++++++++ lib/index.js | 4 +-- lib/pagesEntry.js | 34 ------------------- package.json | 2 +- 5 files changed, 53 insertions(+), 38 deletions(-) rename lib/{ => func}/getSubPackagesRoot.js (93%) create mode 100644 lib/func/pagesEntry.js delete mode 100644 lib/pagesEntry.js diff --git a/lib/getSubPackagesRoot.js b/lib/func/getSubPackagesRoot.js similarity index 93% rename from lib/getSubPackagesRoot.js rename to lib/func/getSubPackagesRoot.js index 18a9dfa..9de5990 100644 --- a/lib/getSubPackagesRoot.js +++ b/lib/func/getSubPackagesRoot.js @@ -1,5 +1,5 @@ 'use strict' -const { getAppObj } = require('./util') +const { getAppObj } = require('../util') // 获取指定目录下符合glob的所有文件 module.exports = function(file) { diff --git a/lib/func/pagesEntry.js b/lib/func/pagesEntry.js new file mode 100644 index 0000000..76849a7 --- /dev/null +++ b/lib/func/pagesEntry.js @@ -0,0 +1,49 @@ +'use strict' +const path = require( 'path' ) +const { getAppObj } = require('../util') +const fs = require('fs') + +const matchPath = function (p) { + const files = [path.resolve(`src/${p}.js`),path.resolve(`src/${p}.vue`)] + return fs.existsSync(files[0]) && files[0] || fs.existsSync(files[1]) && files[1] +} + +// 获取指定目录下符合glob的所有文件 +module.exports = function(file, whileList = []) { + let entries = {}, + mainObj = {}, + pages, + subpackages + + try { + mainObj = getAppObj(file) || {} + pages = mainObj.pages || [] + subpackages = mainObj.subpackages || mainObj.subPackages || [] + + pages.forEach(p=>{ + // const _p = p.replace(/^pages(\/[^\/]*)(\/[^\/]*)?/,($0,$1,$2)=>{return `pages${$1}${$2||$1}`}) + matchPath(p) && (entries[p] = matchPath(p)) + }) + subpackages.forEach(sp=>{ + let {root, pages} = sp + if(root && pages.length>0){ + pages.forEach(p=>{ + // const _p = p.replace(/^pages(\/[^\/]*)(\/[^\/]*)?/,($0,$1,$2)=>{return `pages${$1}${$2||$1}`}) + matchPath(`${root}/${p}`) && (entries[`${root}/${p}`] = matchPath(`${root}/${p}`)) + }) + } + }) + + // 白名单筛选 + if(whileList.length > 0){ + for(let p in entries){ + whileList.indexOf(p) === -1 && (delete entries[p]) + } + } + + } catch (e) { + console.log(e) + } + + return entries +} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index af3777f..bdb0aa7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,5 @@ -const pagesEntry = require( './pagesEntry' ) -const getSubPackagesRoot = require( './getSubPackagesRoot' ) +const pagesEntry = require( './func/pagesEntry' ) +const getSubPackagesRoot = require( './func/getSubPackagesRoot' ) module.exports = { pagesEntry, diff --git a/lib/pagesEntry.js b/lib/pagesEntry.js deleted file mode 100644 index fa2a3e2..0000000 --- a/lib/pagesEntry.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict' -const path = require( 'path' ) -const { getAppObj } = require('./util') - -// 获取指定目录下符合glob的所有文件 -module.exports = function(file) { - let entries = {}, - mainObj = {}, - pages, - subpackages - - try { - mainObj = getAppObj(file) || {} - pages = mainObj.pages || [] - subpackages = mainObj.subpackages || mainObj.subPackages || [] - - pages.forEach(p=>{ - entries[p] = path.resolve(`src/${p}.js`) - }) - subpackages.forEach(sp=>{ - let {root, pages} = sp - if(root && pages.length>0){ - pages.forEach(p=>{ - entries[`${root}/${p}`] = path.resolve(`src/${root}/${p}.js`) - }) - } - }) - - } catch (e) { - console.log(e) - } - - return entries -} \ No newline at end of file diff --git a/package.json b/package.json index 657bdc3..6ec5c22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@megalo/entry", - "version": "0.1.0", + "version": "0.1.1", "description": "", "main": "lib/index.js", "scripts": {