-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: using babel to extract config object #5
- Loading branch information
1 parent
978381a
commit a71c5da
Showing
4 changed files
with
124 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |