Created
July 16, 2017 09:27
-
-
Save tcrowe/87472c4d750de4b9334f276bd43a6bb8 to your computer and use it in GitHub Desktop.
Multiple document capable webpack loader, ES6 syntax, js-yaml
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
/* | |
yaml loader | |
+ safeLoad for one doc | |
+ safeLoadAll for multiple docs | |
questions: | |
+ does it need yaml parser `onWarning: this.emitWarning`? | |
+ does it need error handling e.g. `try-catch`? | |
*/ | |
import yaml from 'js-yaml' | |
// look for lines starting with new doc | |
let openingDocPattern = /^---/g | |
function yamlLoader(source) { | |
let op | |
// assume single doc | |
let loadFn = yaml.safeLoad | |
this.cacheable() | |
// is this a multi-doc yaml? | |
if (openingDocPattern.test(source)) { | |
// yes | |
loadFn = yaml.safeLoadAll | |
} | |
// parse | |
op = loadFn(source.toString(), { | |
filename: this.resourcePath, | |
json: true | |
}) | |
// turn into js module text | |
op = JSON.stringify(op) | |
op = 'module.exports = ' + op | |
return op | |
} | |
export default yamlLoader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment