yaml-loader
Advanced tools
Comparing version 0.5.0 to 0.6.0
35
index.js
@@ -1,13 +0,28 @@ | ||
var yaml = require('js-yaml'); | ||
var loaderUtils = require('loader-utils') | ||
var YAML = require('yaml') | ||
module.exports = function (source) { | ||
this.cacheable && this.cacheable(); | ||
try { | ||
var res = yaml.safeLoad(source); | ||
return JSON.stringify(res, undefined, '\t'); | ||
module.exports = function yamlLoader(src) { | ||
const { asStream, namespace, ...options } = Object.assign( | ||
{ prettyErrors: true }, | ||
loaderUtils.getOptions(this) | ||
) | ||
if (asStream) { | ||
const stream = YAML.parseAllDocuments(src, options) | ||
const 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()) | ||
} | ||
return JSON.stringify(res) | ||
} | ||
catch (err) { | ||
this.emitError(err); | ||
return null; | ||
let res = YAML.parse(src, options) | ||
if (namespace) { | ||
res = namespace.split('.').reduce(function(acc, name) { | ||
return acc[name] | ||
}, res) | ||
} | ||
}; | ||
return JSON.stringify(res) | ||
} |
{ | ||
"name": "yaml-loader", | ||
"version": "0.5.0", | ||
"description": "YAML loader for webpack (converts YAML to JSON)", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/okonet/yaml-loader.git" | ||
}, | ||
"version": "0.6.0", | ||
"license": "MIT", | ||
"description": "YAML loader for Webpack", | ||
"keywords": [ | ||
@@ -16,11 +12,37 @@ "yaml", | ||
], | ||
"author": "Andrey Okonetchnikov <andrey@okonet.ru>", | ||
"license": "MIT", | ||
"contributors": [ | ||
"Andrey Okonetchnikov <andrey@okonet.ru>", | ||
"Eemeli Aro <eemeli@gmail.com>" | ||
], | ||
"homepage": "https://github.com/eemeli/yaml-loader", | ||
"bugs": { | ||
"url": "https://github.com/okonet/yaml-loader/issues" | ||
"url": "https://github.com/eemeli/yaml-loader/issues" | ||
}, | ||
"homepage": "https://github.com/okonet/yaml-loader", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/eemeli/yaml-loader.git" | ||
}, | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"prettier": "prettier --write '**/*.{js,md}'", | ||
"test": "jest" | ||
}, | ||
"prettier": { | ||
"semi": false, | ||
"singleQuote": true | ||
}, | ||
"dependencies": { | ||
"js-yaml": "^3.5.2" | ||
"loader-utils": "^1.4.0", | ||
"yaml": "^1.8.3" | ||
}, | ||
"devDependencies": { | ||
"jest": "^25.1.0", | ||
"prettier": "^1.19.1" | ||
}, | ||
"engines": { | ||
"node": ">= 6" | ||
} | ||
} |
@@ -1,48 +0,92 @@ | ||
# yaml-loader for webpack | ||
# yaml-loader for Webpack | ||
YAML loader for [webpack](http://webpack.github.io/). Converts YAML to a valid JSON. If you want a JS Object, chain it with [json-loader](https://github.com/webpack/json-loader). | ||
YAML loader for [Webpack](https://webpack.js.org/). Allows importing YAML files as JS objects. Uses [`yaml`](https://www.npmjs.com/package/yaml) internally. | ||
## Installation | ||
`npm install yaml-loader` | ||
```sh | ||
npm install --save-dev yaml-loader | ||
``` | ||
## Usage | ||
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html) | ||
```js | ||
// webpack.config.js | ||
module.exports = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.ya?ml$/, | ||
type: 'json', // Required by Webpack v4 | ||
use: 'yaml-loader' | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
Simplest case would be: | ||
```yaml | ||
# file.yaml | ||
--- | ||
config: | ||
js: | ||
key: test | ||
hello: world | ||
``` | ||
``` javascript | ||
var json = require("json-loader!yaml-loader!./file.yml"); | ||
// => returns file.yml as javascript object | ||
```js | ||
// application.js | ||
import file from './file.yaml' | ||
file.hello === 'world' | ||
``` | ||
This loader is also useful for getting a valid JSON from YML. For example: | ||
## Options | ||
In addition to all [`yaml` options](https://eemeli.org/yaml/#options), the loader supports the following additional options: | ||
### `asStream` | ||
If enabled, parses the source file as a stream of YAML documents. With this, the output will always be an array, with entries for each document. If set, `namespace` is ignored. | ||
To use this option for only some YAML files, it's probably easiest to use a query parameter and match that using [Rule.resourceQuery](https://webpack.js.org/configuration/module/#ruleresourcequery): | ||
```js | ||
// webpack.config.js | ||
module: { | ||
loaders: [ | ||
{ | ||
test: /\.yaml$/, | ||
include: path.resolve('data'), | ||
loader: 'yaml', | ||
}, | ||
], | ||
module.exports = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.ya?ml$/, | ||
type: 'json', // Required by Webpack v4 | ||
oneOf: [ | ||
{ | ||
resourceQuery: /stream/, | ||
options: { asStream: true }, | ||
use: 'yaml-loader' | ||
}, | ||
{ use: 'yaml-loader' } | ||
] | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
and then | ||
Then, importing `./foo.yaml` will expect it to contain only one document, but `./bar.yaml?stream` may contain multiple documents. | ||
### `namespace` | ||
Allows for exposing a sub-tree of the source document: | ||
```js | ||
// application.js | ||
const actualFilename = require(`file?name=[name].json!./../data/${file}.yaml`); | ||
window.fetch(actualFilename).then(res => { | ||
// ... | ||
}); | ||
import jsCfg from './file.yaml?namespace=config.js' | ||
jsCfg.key === 'test' | ||
``` | ||
The `namespace` should be a series of keys, dot separated. Note that any `options` object in your `webpack.config.js` rule will be superseded by a `?query`. | ||
## License | ||
MIT (http://www.opensource.org/licenses/mit-license.php) | ||
[MIT](http://www.opensource.org/licenses/mit-license.php) |
Sorry, the diff of this file is not supported yet
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
4823
25
93
2
2
4
1
+ Addedloader-utils@^1.4.0
+ Addedyaml@^1.8.3
+ Addedbig.js@5.2.2(transitive)
+ Addedemojis-list@3.0.0(transitive)
+ Addedjson5@1.0.2(transitive)
+ Addedloader-utils@1.4.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedyaml@1.10.2(transitive)
- Removedjs-yaml@^3.5.2
- Removedargparse@1.0.10(transitive)
- Removedesprima@4.0.1(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedsprintf-js@1.0.3(transitive)