diff --git a/lib/getSubPackagesRoot.js b/lib/getSubPackagesRoot.js new file mode 100644 index 0000000..18a9dfa --- /dev/null +++ b/lib/getSubPackagesRoot.js @@ -0,0 +1,27 @@ +'use strict' +const { getAppObj } = require('./util') + +// 获取指定目录下符合glob的所有文件 +module.exports = function(file) { + let entries = {}, + mainObj = {}, + subpackages + + try { + mainObj = getAppObj(file) || {} + subpackages = mainObj.subpackages || mainObj.subPackages || [] + subpackages.forEach(sp=>{ + let {root, pages} = sp + if(root && pages.length>0){ + pages.forEach(p=>{ + entries[`${root}/${p}`] = root + }) + } + }) + + } catch (e) { + console.log(e) + } + + return entries +} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 59274a0..af3777f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,7 @@ const pagesEntry = require( './pagesEntry' ) +const getSubPackagesRoot = require( './getSubPackagesRoot' ) + module.exports = { - pagesEntry + pagesEntry, + getSubPackagesRoot } \ No newline at end of file diff --git a/lib/pagesEntry.js b/lib/pagesEntry.js index 3281f3e..fa2a3e2 100644 --- a/lib/pagesEntry.js +++ b/lib/pagesEntry.js @@ -1,22 +1,16 @@ 'use strict' -const fs = require('fs') const path = require( 'path' ) -const { walk4Obj } = require('./util') -const exp4parse = /\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/|\/\/[^\r\n]*|\s/g +const { getAppObj } = require('./util') // 获取指定目录下符合glob的所有文件 -module.exports = function pagesEntry(file) { +module.exports = function(file) { let entries = {}, - txt = '', mainObj = {}, pages, subpackages try { - txt = fs.readFileSync(file,'utf8') - txt = txt.replace(exp4parse,'') - mainObj = walk4Obj(txt,'exportdefault')['config'] || {} - + mainObj = getAppObj(file) || {} pages = mainObj.pages || [] subpackages = mainObj.subpackages || mainObj.subPackages || [] diff --git a/lib/util.js b/lib/util.js index a5faa2f..c4d3956 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,9 @@ +const fs = require('fs') const path = require( 'path' ) const json5 = require('json5') +const exp4parse = /\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/|\/\/[^\r\n]*|\s/g + function resolve (...args) { return path.resolve( __dirname, '../', ...args) } @@ -38,7 +41,14 @@ function walk4Obj (txt, startStr) { return resault } +function getAppObj(file){ + let txt = fs.readFileSync(file,'utf8') + txt = txt.replace(exp4parse,'') + return walk4Obj(txt,'exportdefault')['config'] || {} +} + module.exports = { resolve, - walk4Obj + walk4Obj, + getAppObj } diff --git a/package.json b/package.json index a19e7dd..657bdc3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@megalo/entry", - "version": "0.0.1", + "version": "0.1.0", "description": "", "main": "lib/index.js", "scripts": {