Code Monkey home page Code Monkey logo

generate-json-file-webpack-plugin's Introduction

generate-json-file-webpack-plugin

Webpack plugin that creates a custom JSON file.

Install

npm install --save-dev generate-json-file-webpack-plugin

Options

Name Type Description
filename {String} Output file name
jsonFile {String} Path to an existing JSON file to extend (optional)
value {Object|Function} Object to add to output file. (You don't actually need to return anything if you are extending an existing file. You can just manipulate the properties.)

Usage

In your webpack.config.js instantiate the plugin.

const GenerateJsonFile = require('generate-json-file-webpack-plugin');

module.exports = {
  // webpack configuration
  // ...
  plugins: [
    new GenerateJsonFile({
      // json configuration
    })
  ]
};

Here is a basic example that creates a mainfest.json file in your output directory:

webpack.config.js

module.exports = {
  output: {
    path: 'dist/'
  },
  // ...
  plugins: [
    new GenerateJsonFile({
      filename: 'manifest.json',
      value: {
        foo: 'bar'
      }
    })
  ]
};

That will generate a file that looks like this:

manifest.json

{
  "foo": "bar"
}

Here's an example of how you can dynamically configure values to be emitted to your JSON file:

webpack.config.js

plugins: [
  new GenerateJsonFile({
    filename: 'manifest.json',
    value: () => {
      if (mode === 'development') {
        return {
          foo: 'bar'
        }
      }

      return {
        foo: 'baz'
      }
    }
  })
]

You can add to existing JSON files by using the jsonFile option and including a path to your source file:

webpack.config.js

plugins: [
  new GenerateJsonFile({
    jsonFile: './src/manifest.json',
    filename: 'manifest.json',
    value: (manifest) => {

      // manipulate existing values
      manifest.array.push('add me!');

      // add new values
      return {
        foo: 'baz'
      }
    }
  })
]

Here is the source file that you are extending:

manifest.json

{
  "array": ["bar"]
}

And the output file:

manifest.json

{
  "array": ["bar", "add me!"],
  "foo": "baz"
}

Todo:

  • add tests

License

MIT © Daniel Hayes

generate-json-file-webpack-plugin's People

Contributors

daniel-hayes avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

generate-json-file-webpack-plugin's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.