0.2.1 • Published 7 years ago
json-glob-loader v0.2.1
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.fooThis 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-devThen 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