From a71c5da61d32e59b61d0338599fab445b72be968 Mon Sep 17 00:00:00 2001 From: elcarim5efil Date: Thu, 28 Feb 2019 19:48:19 +0800 Subject: [PATCH] fix: using babel to extract config object #5 --- lib/extract-config.js | 27 +++++++++++++++ lib/util.js | 52 ++++++++-------------------- package.json | 3 +- test/index.js | 80 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 38 deletions(-) create mode 100644 lib/extract-config.js create mode 100644 test/index.js diff --git a/lib/extract-config.js b/lib/extract-config.js new file mode 100644 index 0000000..2ede860 --- /dev/null +++ b/lib/extract-config.js @@ -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() + }, + }, + } +} diff --git a/lib/util.js b/lib/util.js index c4d3956..0c3262f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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 } diff --git a/package.json b/package.json index 6ec5c22..272c348 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..5e74081 --- /dev/null +++ b/test/index.js @@ -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)) \ No newline at end of file