Skip to content

Commit

Permalink
feat: new func getSubPackagesRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
zwwill committed Nov 27, 2018
1 parent d695988 commit 8ccfb58
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
27 changes: 27 additions & 0 deletions lib/getSubPackagesRoot.js
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const pagesEntry = require( './pagesEntry' )
const getSubPackagesRoot = require( './getSubPackagesRoot' )

module.exports = {
pagesEntry
pagesEntry,
getSubPackagesRoot
}
12 changes: 3 additions & 9 deletions lib/pagesEntry.js
Original file line number Diff line number Diff line change
@@ -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 || []

Expand Down
12 changes: 11 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@megalo/entry",
"version": "0.0.1",
"version": "0.1.0",
"description": "",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit 8ccfb58

Please sign in to comment.