npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-to-json-webpack-plugin

v1.0.1

Published

`js-to-json-webpack-plugin` is a Webpack plugin that converts **JavaScript** files into **JSON** format.

Downloads

144

Readme

JS-to-JSON-Webpack-plugin

js-to-json-webpack-plugin is a Webpack plugin that converts JavaScript files into JSON format.

Table of Contents

Installation

To install the plugin, run the following command:

npm install js-to-json-webpack-plugin --save-dev

or

yarn add js-to-json-webpack-plugin --dev

Usage

To use the js-to-json-webpack-plugin, add it to your Webpack configuration file (webpack.config.js):

const JSToJSONWebpackPlugin = require('js-to-json-webpack-plugin');

module.exports = {
    // ... your existing configuration
    plugins: [
        new JSToJSONWebpackPlugin({
            // Plugin options
        }),
    ],
};

Options

The plugin options require only input and output-fields. The configuration-field contains sensible defaults. The configuration-field is passed as a parameter to the actual JavaScript-file you wished to convert to JSON.

The plugin accepts the following options: | Option | Type | Default | Description | Optional | | - | - | - | - | - | | input | string | null | The input JavaScript file to convert. | false | | output | string | null | The output JSON file name. |false | | configuration | object | { replacer: null, space: 2, append: {} } | A custom object passed over to the JavaScript file. May contain anything. | true |

configuration default options: | Option | Type | Default | Description | Optional | | - | - | - | - | - | | replacer | function | null | JSON.stringify replacer parameter | true | | space | number | 2 | JSON.stringify space parameter | true | | append | object | {} | Will be appended to the JSON | true |

Example

Here is an example configuration with options:

A configuration JavaScript file that reads the root level package.json and some of it's fields to the Object to be turned into a JSON-file.

// manifest.config.js
const fs = require("fs");

module.exports = (configuration) => {
  // If my custom value was set, return this instead:
  if (configuration.whatYearIsIt && configuration.whatYearIsIt === 1996) {
    return { epiphany: "The dad and the poacher are the same person!" };
  }

  const manifest = {
    manifest_version: 3,
    background: {
      service_worker: "background.js",
      type: "module",
    },
    icons: {
      16: "./icon_16.png",
      48: "./icon_48.png",
      128: "./icon_128.png",
    },
  };

  const packageJSON = JSON.parse(fs.readFileSync("./package.json"));

  return Object.assign({ ...manifest }, configuration.append, {
    name: packageJSON.name,
    description: packageJSON.description,
    version: packageJSON.version,
    author: packageJSON.author,
    homepage_url: packageJSON.homepage,
  });
};

Add JSToJSONWebpackPlugin to your webpack.config.js-file or equivalent:

// webpack.config.js
const JSToJSONWebpackPlugin = require("js-to-json-webpack-plugin");

module.exports = {
  // ... your existing configuration
  plugins: [
    new JSToJSONWebpackPlugin({
      input: "./manifest.config.js",
      output: "manifest.json",
      configuration: {
        replacer: null,
        space: 2,
        // ... and your custom configuration like:
        append: { myCustomField: "something I wanted to add" },
        whatYearIsIt: 2024,
      },
    }),
  ],
};

Ouput to file manifest.json:

{
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js",
    "type": "module"
  },
  "icons": {
    "16": "./icon_16.png",
    "48": "./icon_48.png",
    "128": "./icon_128.png"
  },
  "name": "package-json-name",
  "description": "package.json description",
  "version": "x.x.x",
  "author": "package-json-author",
  "myCustomField": "something I wanted to add"
}

License

This project is licensed under the MIT License - see the LICENSE file for details.