Multiple document capable webpack loader, ES6 syntax, js-yaml
/* | |
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