-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from kaola-fed/issue47
feat: support lang(json/yaml) for <config></config> block, closes #47
- Loading branch information
Showing
4 changed files
with
97 additions
and
23 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
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,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 ) | ||
} | ||
} |
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