Skip to content

Instantly share code, notes, and snippets.

@tcrowe
Created July 16, 2017 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcrowe/87472c4d750de4b9334f276bd43a6bb8 to your computer and use it in GitHub Desktop.
Save tcrowe/87472c4d750de4b9334f276bd43a6bb8 to your computer and use it in GitHub Desktop.
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