diff --git a/examples/vue-mp/src/pages/counter/index.vue b/examples/vue-mp/src/pages/counter/index.vue index 6dcc5e7..f025323 100644 --- a/examples/vue-mp/src/pages/counter/index.vue +++ b/examples/vue-mp/src/pages/counter/index.vue @@ -74,8 +74,21 @@ export default { } - - { - a: 1, - } + +pages: + - pages/index +subpackages: + - + root: packageA + pages: + - pages/test + - + root: pages/packageB + pages: + - pages/test +window: + backgroundTextStyle: light + navigationBarBackgroundColor: '#fff' + navigationBarTitleText: demo + navigationBarTextStyle: black diff --git a/packages/target/lib/frameworks/vue/loader/config.js b/packages/target/lib/frameworks/vue/loader/config.js index 9fd2611..f1fb894 100644 --- a/packages/target/lib/frameworks/vue/loader/config.js +++ b/packages/target/lib/frameworks/vue/loader/config.js @@ -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 block in ${ relativePath }, ${ source.trim() } + +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 ) + } } diff --git a/packages/target/package.json b/packages/target/package.json index 80924ec..486e057 100644 --- a/packages/target/package.json +++ b/packages/target/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index a99227d..724d03d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: