yaml-loader
YAML loader for Webpack
Last updated 4 months ago by eemeli .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install yaml-loader 
SYNC missed versions from official npm registry.

yaml-loader for Webpack

YAML loader for Webpack. Allows importing YAML files as JS objects. Uses yaml internally.

Installation

npm install --save-dev yaml-loader

Usage

// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.ya?ml$/,
        type: 'json', // Required by Webpack v4
        use: 'yaml-loader'
      }
    ]
  }
}
# file.yaml
---
config:
  js:
    key: test
hello: world
// application.js
import file from './file.yaml'

file.hello === 'world'

Options

In addition to all 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:

// webpack.config.js
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' }
        ]
      }
    ]
  }
}

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:

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

Current Tags

  • 0.6.0                                ...           latest (4 months ago)

6 Versions

  • 0.6.0                                ...           4 months ago
  • 0.5.0                                ...           3 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
  • 0.1.0                                ...           6 years ago
Maintainers (2)
Downloads
Today 176
This Week 2,346
This Month 3,823
Last Day 414
Last Week 2,324
Last Month 10,165
Dependencies (2)
Dev Dependencies (2)
Dependents (181)

Copyright 2014 - 2017 © taobao.org |