├── .prettierignore ├── example ├── index.js ├── webpack.config.js └── __tests__ │ └── example-test.js ├── .prettierrc ├── .gitignore ├── renovate.json ├── .github └── workflows │ └── test.yml ├── LICENSE ├── index.js ├── package.json └── README.md /.prettierignore: -------------------------------------------------------------------------------- 1 | /example/build/ 2 | -------------------------------------------------------------------------------- /example/index.js: -------------------------------------------------------------------------------- 1 | console.log('Hello'); 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /example/build/ 2 | *.log 3 | node_modules 4 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["config:js-lib", ":automergeMinor", ":automergePatch"] 3 | } 4 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push] 4 | 5 | jobs: 6 | test: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | matrix: 11 | node-version: [10.x, 12.x] 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Use Node.js ${{ matrix.node-version }} 16 | uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /example/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const GenerateJsonPlugin = require('..'); 3 | 4 | module.exports = { 5 | entry: path.resolve(__dirname, 'index.js'), 6 | output: { 7 | filename: 'bundle.js', 8 | path: path.resolve(__dirname, 'build'), 9 | pathinfo: true, 10 | }, 11 | plugins: [ 12 | new GenerateJsonPlugin( 13 | 'my-file.json', 14 | { foo: 'bar', one: 'two' }, 15 | (key, value) => { 16 | if (value === 'bar') { 17 | return 'baz'; 18 | } 19 | return value; 20 | }, 21 | 2 22 | ), 23 | ], 24 | }; 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Spencer Elliott 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any 4 | purpose with or without fee is hereby granted, provided that the above 5 | copyright notice and this permission notice appear in all copies. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { Compilation } = require('webpack'); 2 | 3 | function GenerateJsonPlugin(filename, value, replacer, space) { 4 | Object.assign(this, { filename, value, replacer, space }); 5 | 6 | this.plugin = { name: 'GenerateJsonPlugin' }; 7 | } 8 | 9 | GenerateJsonPlugin.prototype.apply = function apply(compiler) { 10 | compiler.hooks.compilation.tap(this.plugin, (compilation) => { 11 | const json = JSON.stringify(this.value, this.replacer, this.space); 12 | 13 | compilation.hooks.processAssets.tap( 14 | { 15 | name: this.plugin.name, 16 | stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, 17 | }, 18 | (assets) => { 19 | assets[this.filename] = { 20 | source: () => json, 21 | size: () => json.length, 22 | }; 23 | } 24 | ); 25 | }); 26 | }; 27 | 28 | module.exports = GenerateJsonPlugin; 29 | -------------------------------------------------------------------------------- /example/__tests__/example-test.js: -------------------------------------------------------------------------------- 1 | const del = require('del'); 2 | const fs = require('fs'); 3 | const path = require('path'); 4 | const webpack = require('webpack'); 5 | const config = require('../webpack.config'); 6 | 7 | describe('example', () => { 8 | it('creates a file my-file.json', async () => { 9 | await del(path.join(__dirname, '..', 'build')); 10 | await new Promise((resolve, reject) => { 11 | webpack(config, (error, stats) => { 12 | if (error) { 13 | reject(error); 14 | } 15 | fs.readFile( 16 | path.resolve(stats.compilation.compiler.outputPath, 'my-file.json'), 17 | 'utf8', 18 | (err, data) => { 19 | if (err) { 20 | reject(err); 21 | } 22 | expect(data).toBe( 23 | '{\n' + ' "foo": "baz",\n' + ' "one": "two"\n' + '}' 24 | ); 25 | resolve(); 26 | } 27 | ); 28 | }); 29 | }); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "generate-json-webpack-plugin", 3 | "version": "2.0.0", 4 | "description": "Webpack plugin to generate a custom JSON asset", 5 | "main": "index.js", 6 | "scripts": { 7 | "jest": "jest", 8 | "format:check": "prettier --check .", 9 | "format:write": "prettier --write .", 10 | "test": "npm run format:check && npm run jest" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/elliottsj/generate-json-webpack-plugin.git" 15 | }, 16 | "keywords": [ 17 | "webpack" 18 | ], 19 | "author": "Spencer Elliott ", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/elliottsj/generate-json-webpack-plugin/issues" 23 | }, 24 | "homepage": "https://github.com/elliottsj/generate-json-webpack-plugin#readme", 25 | "devDependencies": { 26 | "del": "6.1.1", 27 | "jest": "26.6.3", 28 | "prettier": "2.8.8", 29 | "webpack": "5.99.7" 30 | }, 31 | "jest": { 32 | "testEnvironment": "node", 33 | "roots": [ 34 | "/example" 35 | ] 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # generate-json-webpack-plugin 2 | 3 | [![npm version](https://img.shields.io/npm/v/generate-json-webpack-plugin.svg)](https://www.npmjs.com/package/generate-json-webpack-plugin) 4 | 5 | webpack plugin to generate a custom JSON asset 6 | 7 | ### Installation 8 | 9 | ```shell 10 | npm install generate-json-webpack-plugin 11 | ``` 12 | 13 | ### Usage 14 | 15 | ```js 16 | // webpack.config.js 17 | const GenerateJsonPlugin = require('generate-json-webpack-plugin'); 18 | 19 | module.exports = { 20 | // ... 21 | plugins: [ 22 | // ... 23 | new GenerateJsonPlugin('my-file.json', { 24 | foo: 'bar', 25 | }), 26 | ], 27 | // ... 28 | }; 29 | ``` 30 | 31 | This will create a file `my-file.json` in webpack's output directory, with contents: 32 | 33 | ```json 34 | { "foo": "bar" } 35 | ``` 36 | 37 | You may optionally pass 'replacer' and 'space' arguments as the 3rd and 4th arguments, which will be passed to [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) to customize the JSON output: 38 | 39 | ```js 40 | new GenerateJsonPlugin( 41 | 'my-file.json', 42 | { foo: 'bar', one: 'two' }, 43 | (key, value) => { 44 | if (value === 'bar') { 45 | return 'baz'; 46 | } 47 | return value; 48 | }, 49 | 2 50 | ); 51 | ``` 52 | 53 | `my-file.json` will contain: 54 | 55 | ```json 56 | { 57 | "foo": "baz", 58 | "one": "two" 59 | } 60 | ``` 61 | --------------------------------------------------------------------------------