Skip to content

Commit

Permalink
Merge pull request #53 from kaola-fed/issue47
Browse files Browse the repository at this point in the history
feat: support lang(json/yaml) for <config></config> block, closes #47
  • Loading branch information
fengzilong authored Dec 25, 2018
2 parents 680ee5d + 784d301 commit 6f92998
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 23 deletions.
21 changes: 17 additions & 4 deletions examples/vue-mp/src/pages/counter/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,21 @@ export default {
}
</style>

<config>
{
a: 1,
}
<config lang="yml">
pages:
- pages/index
subpackages:
-
root: packageA
pages:
- pages/test
-
root: pages/packageB
pages:
- pages/test
window:
backgroundTextStyle: light
navigationBarBackgroundColor: '#fff'
navigationBarTitleText: demo
navigationBarTextStyle: black
</config>
96 changes: 78 additions & 18 deletions packages/target/lib/frameworks/vue/loader/config.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,100 @@
const path = require( 'path' )
const qs = require( 'qs' )
const loaderUtils = require( 'loader-utils' )
const JSON5 = require( 'json5' )
const yaml = require( 'js-yaml' )
const toString = Object.prototype.toString

module.exports = function ( source ) {
const loaderContext = this

const entryHelper = loaderContext.megaloEntryHelper
const resourcePath = loaderContext.resourcePath
const query = qs.parse( loaderContext.resourceQuery.slice( 1 ) ) || {}
const lang = query.lang || 'json'

if ( entryHelper.isEntry( loaderContext.resourcePath ) ) {
const entryKey = entryHelper.getEntryKey( loaderContext.resourcePath )

let config
try {
config = JSON5.parse( source )
if ( toString.call( config ) !== '[object Object]' ) {
config = {}
parseConfig( {
source,
lang,
filepath: resourcePath,
}, function ( e, config ) { // sync callback
if ( e ) {
loaderContext.emitError( getParseError( e, source, resourcePath ) )
return
}
} catch ( e ) {
config = {}

const relativePath = path.relative( process.cwd(), loaderContext.resourcePath )
const reason = `
const entryKey = entryHelper.getEntryKey( loaderContext.resourcePath )

loaderContext.megaloCacheToPages( {
file: entryKey,
config: config,
} )
} )
}

return ''
}

function getParseError( e, source, resourcePath ) {
const relativePath = path.relative( process.cwd(), resourcePath )
const reason = `
[@MEGALO/TARGET] Failed to parse <config> block in ${ relativePath },
<config>
${ source.trim() }
</config>
Details: ${ e.message }
`
loaderContext.emitError( new Error( reason ) )
}

loaderContext.megaloCacheToPages( {
file: entryKey,
config: config,
return new Error( reason )
}

const handlers = {
json( { source, filepath } = {} ) {
return JSON5.parse( source )
},

yaml( { source, filepath } = {} ) {
return yaml.safeLoad( source, {
filename: filepath,
json: true,
} )
},
}

function parseConfig( { source, lang, filepath } = {}, callback ) {
const normalizeMap = {
json: 'json',
json5: 'json',
yaml: 'yaml',
yml: 'yaml',
}

return ''
const normalizedLang = normalizeMap[ lang ]

const handler = handlers[ normalizedLang ]

if ( !handler ) {
return callback( new Error(
'Invalid lang for config block: "' + lang + '", ' +
'consider using "json" or "yaml"'
) )
}

let config
try {
config = handler( {
source,
filepath,
} )

if ( toString.call( config ) !== '[object Object]' ) {
config = {}
}

callback( null, config )
} catch ( e ) {
callback( e )
}
}
1 change: 1 addition & 0 deletions packages/target/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"babelon": "^1.0.5",
"copy-webpack-plugin": "^4.5.2",
"hash-sum": "^1.0.2",
"js-yaml": "^3.12.0",
"json5": "^2.1.0",
"loader-utils": "^1.1.0",
"lodash.merge": "^4.6.1",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5621,7 +5621,7 @@ js-tokens@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"

js-yaml@^3.10.0, js-yaml@^3.5.1, js-yaml@^3.9.0, js-yaml@^3.9.1:
js-yaml@^3.10.0, js-yaml@^3.12.0, js-yaml@^3.5.1, js-yaml@^3.9.0, js-yaml@^3.9.1:
version "3.12.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.12.0.tgz#eaed656ec8344f10f527c6bfa1b6e2244de167d1"
dependencies:
Expand Down

0 comments on commit 6f92998

Please sign in to comment.