yaml-loader
Advanced tools
Comparing version 0.6.0 to 0.7.0
59
index.js
@@ -1,28 +0,59 @@ | ||
var loaderUtils = require('loader-utils') | ||
var YAML = require('yaml') | ||
const { getOptions } = require('loader-utils') | ||
const { stringify } = require('javascript-stringify') | ||
const YAML = require('yaml') | ||
const makeIdIterator = (prefix = 'v', i = 1) => ({ next: () => prefix + i++ }) | ||
module.exports = function yamlLoader(src) { | ||
const { asStream, namespace, ...options } = Object.assign( | ||
const { asJSON, asStream, namespace, ...options } = Object.assign( | ||
{ prettyErrors: true }, | ||
loaderUtils.getOptions(this) | ||
getOptions(this) | ||
) | ||
// keep track of repeated object references | ||
const refs = new Map() | ||
const idIter = makeIdIterator() | ||
function addRef(ref, count) { | ||
if (ref && typeof ref === 'object' && count > 1) | ||
refs.set(ref, { id: idIter.next(), seen: false }) | ||
} | ||
const stringifyWithRefs = (value) => | ||
stringify(value, (value, space, next) => { | ||
const v = refs.get(value) | ||
if (v) { | ||
if (v.seen) return v.id | ||
v.seen = true | ||
} | ||
return next(value) | ||
}) | ||
const jsOpt = Object.assign({}, options, { onAnchor: addRef }) | ||
if (asJSON) { | ||
jsOpt.json = true | ||
jsOpt.mapAsMap = false | ||
} | ||
let res | ||
if (asStream) { | ||
const stream = YAML.parseAllDocuments(src, options) | ||
const res = [] | ||
res = [] | ||
for (const doc of stream) { | ||
for (const warn of doc.warnings) this.emitWarning(warn) | ||
for (const err of doc.errors) throw err | ||
res.push(doc.toJSON()) | ||
res.push(doc.toJS(jsOpt)) | ||
} | ||
return JSON.stringify(res) | ||
} else { | ||
const doc = YAML.parseDocument(src, options) | ||
for (const warn of doc.warnings) this.emitWarning(warn) | ||
for (const err of doc.errors) throw err | ||
if (namespace) doc.contents = doc.getIn(namespace.split('.')) | ||
res = doc.toJS(jsOpt) | ||
} | ||
let res = YAML.parse(src, options) | ||
if (namespace) { | ||
res = namespace.split('.').reduce(function(acc, name) { | ||
return acc[name] | ||
}, res) | ||
} | ||
return JSON.stringify(res) | ||
if (asJSON) return JSON.stringify(res) | ||
let str = '' | ||
for (const [obj, { id }] of refs.entries()) | ||
str += `var ${id} = ${stringifyWithRefs(obj)};\n` | ||
str += `export default ${stringifyWithRefs(res)};` | ||
return str | ||
} |
{ | ||
"name": "yaml-loader", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"license": "MIT", | ||
@@ -29,20 +29,22 @@ "description": "YAML loader for Webpack", | ||
"scripts": { | ||
"prettier": "prettier --write '**/*.{js,md}'", | ||
"test": "jest" | ||
"prettier": "prettier --ignore-path .gitignore --write .", | ||
"test": "jest --coverage" | ||
}, | ||
"prettier": { | ||
"semi": false, | ||
"singleQuote": true | ||
"singleQuote": true, | ||
"trailingComma": "none" | ||
}, | ||
"dependencies": { | ||
"loader-utils": "^1.4.0", | ||
"yaml": "^1.8.3" | ||
"javascript-stringify": "^2.0.1", | ||
"loader-utils": "^2.0.0", | ||
"yaml": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"jest": "^25.1.0", | ||
"prettier": "^1.19.1" | ||
"jest": "^27.5.1", | ||
"prettier": "^2.1.1" | ||
}, | ||
"engines": { | ||
"node": ">= 6" | ||
"node": ">= 10.14.2" | ||
} | ||
} |
@@ -20,3 +20,2 @@ # yaml-loader for Webpack | ||
test: /\.ya?ml$/, | ||
type: 'json', // Required by Webpack v4 | ||
use: 'yaml-loader' | ||
@@ -47,4 +46,9 @@ } | ||
In addition to all [`yaml` options](https://eemeli.org/yaml/#options), the loader supports the following additional options: | ||
In addition to all [`yaml` options](https://eemeli.org/yaml/#options) used by its parsing methods, | ||
the loader supports the following additional options: | ||
### `asJSON` | ||
If enabled, the loader output is stringified JSON rather than stringified JavaScript. For Webpack v4, you'll need to set the rule to have `type: "json"`. Also useful for chaining with other loaders that expect JSON input. | ||
### `asStream` | ||
@@ -51,0 +55,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6155
53
97
0
3
+ Addedjavascript-stringify@^2.0.1
+ Addedjavascript-stringify@2.1.0(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedloader-utils@2.0.4(transitive)
+ Addedyaml@2.7.0(transitive)
- Removedjson5@1.0.2(transitive)
- Removedloader-utils@1.4.2(transitive)
- Removedminimist@1.2.8(transitive)
- Removedyaml@1.10.2(transitive)
Updatedloader-utils@^2.0.0
Updatedyaml@^2.0.0