Skip to content

Commit

Permalink
fix: using babel to extract config object #5
Browse files Browse the repository at this point in the history
  • Loading branch information
elcarim5efil committed Feb 28, 2019
1 parent 978381a commit a71c5da
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 38 deletions.
27 changes: 27 additions & 0 deletions lib/extract-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const babelon = require( 'babelon' )
const generate = require( 'babel-generator' ).default

module.exports = function ( { types: t } ) {
return {
visitor: {
ExportDefaultDeclaration( path ) {
path.node.declaration.properties.forEach( prop => {
if (
t.isObjectProperty( prop ) &&
t.isIdentifier( prop.key, { name: 'config' } )
) {
const code = generate( prop.value ).code

path.hub.file.metadata.config = {
code: code,
node: prop.value,
value: babelon.eval( code )
}
}
} )

path.remove()
},
},
}
}
52 changes: 15 additions & 37 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,32 @@
const fs = require('fs')
const path = require( 'path' )
const json5 = require('json5')

const exp4parse = /\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/|\/\/[^\r\n]*|\s/g
const extractConfigPlugin = require('./extract-config');
const babel = require('babel-core');

function resolve (...args) {
return path.resolve( __dirname, '../', ...args)
const babelOptions = {
plugins: [
extractConfigPlugin
]
}

function walk4Obj (txt, startStr) {
if(typeof txt !== 'string' || typeof startStr !== 'string'){
return console.log('Props error')
}
let index = txt.indexOf(startStr),
resault = {},
begin = index + startStr.length,
end = begin

try{
if(index>0){
for(let _i = begin, _l=txt.length, _a=0;_i<_l;_i++) {
switch(txt.charAt(_i) || '') {
case '{': _a ++
break
case '}': _a --
break
default : break
}
if(_a <= 0) {
end = _i
break
}
}
resault = json5.parse(txt.substring(begin,end+1))
}
function resolve (...args) {
return path.resolve( __dirname, '../', ...args)
}

} catch (e) {
console.log(e)
}
return resault
function extractConfig(txt) {
const { metadata } = babel.transform( txt, babelOptions )
return metadata.config.value
}

function getAppObj(file){
let txt = fs.readFileSync(file,'utf8')
txt = txt.replace(exp4parse,'')
return walk4Obj(txt,'exportdefault')['config'] || {}
return extractConfig(txt)
}

module.exports = {
resolve,
walk4Obj,
getAppObj
getAppObj,
extractConfig
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"homepage": "https://github.com/megalojs/megalo-entry#readme",
"dependencies": {
"json5": "^2.1.0"
"babel-core": "^6.26.3",
"babelon": "^1.0.5"
}
}
80 changes: 80 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

const txt = `import App from './App'
import Vue from 'vue'
import Vuex from 'vuex'
import VHtmlPlugin from '@megalo/vhtml-plugin'
Vue.use(Vuex)
Vue.use(VHtmlPlugin)
const store = require('./store').default
Vue.prototype.$store = store
const app = new Vue( App )
app.$mount()
export default {
config: {
usingComponents: {
'map-route': 'plugin://myPlugin/mapRoute'
},
window: {
backgroundTextStyle: 'light',
navigationBarBackgroundColor: '#fff',
navigationBarTitleText: 'WeChat',
navigationBarTextStyle: 'black'
},
pages: [
'pages/play/index',
// 'pages/index/index',
// 'pages/test/index',
// 'pages/todomvc/index',
// 'pages/v-model/index',
// 'pages/v-html/index',
// 'pages/vuex/index',
// 'pages/native/index',
// 'pages/webview/index',
// 'pages/img/index',
],
// subPackages: [
// {
// root: 'packageA',
// pages: [
// 'pages/a/index',
// 'pages/todomvc/index',
// ]
// }
// ],
// tabBar: {
// list: [
// {
// pagePath: 'pages/index/index',
// text: '首页'
// },
// {
// pagePath: 'pages/todomvc/index',
// text: 'todo'
// }
// ]
// },
_alipay: {
window: {
navigationBarTitleText: 'Alipay'
}
},
_swan: {
window: {
navigationBarTitleText: 'Baidu'
}
},
myPlugin: {
"version": "1.0.0",
"provider": "wxidxxxxxxxxxxxxxxxx"
}
}
}`;

const { extractConfig } = require('./lib/util')

console.log(extractConfig(txt))

0 comments on commit a71c5da

Please sign in to comment.