json-glob-loader

0.2.1 • Public • Published

npm Build Status

json-glob-loader

A loader for webpack that analyzes any JSON searching for file paths using (glob)[https://www.npmjs.com/package/glob]'s patterns, so they will be replaced inside the JSON.

Assuming this file tree:

└── my-files-dir
    ├── a.foo
    ├── a2.foo
    └── b.foo

This simple a JSON with this content:

{
  "foo":true,
  "files":[
    "*"
  ],
  "some-more": {
    "data": [
      "first",
      "a*",
      "last"
    ]
  }
}

Will be loaded as:

{
  "foo":true,
  "files":[
    "a.foo",
    "a2.foo"
    "b.foo"
  ],
  "some-more": {
    "data": [
      "first",
      "a.foo",
      "a2.foo",
      "last"
    ]
  }
}

Getting Started

To begin, you'll need to install json-glob-loader:

$ npm install json-glob-loader --save-dev

Then add the loader to your webpack config. For example:

import myFile from '../wherever/my/json/is/config.js';
// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /config\.json$/,
        use: [{
          loader: 'json-glob-loader',
          options: {
            baseDir: path.join(__dirname, 'my-files-dir/'),
          }
        }]
      }
    ]
  }
}

config

  • baseDir: baseDir from where glob will be applied (omitted from the resultant JSON declaration).
  • transformStringsToArray: if set to true, will replace strings outside Arrays, transforming this into Arrays (optional, false by default).
  • globOptions: glob options to be applied on the glob method.

KNOWN ISSUES

🙋 If no matches are found using a legit expression, then no replacement will be made; or any string would disappear. ** Make sure there is at least one file on your folders... **

testing

$ npm test

License

MIT

Package Sidebar

Install

npm i json-glob-loader

Weekly Downloads

0

Version

0.2.1

License

MIT

Unpacked Size

11.1 kB

Total Files

12

Last publish

Collaborators

  • jabiinfante