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

json-bundler

v1.0.0

Published

Bundles your JSON & JSON5 files intelligently.

Downloads

50

Readme

json-bundler

Bundles your JSON files intelligently.

npm version dependency status travis ci build status Codecov Known Vulnerabilities license

What it does

When developing web applications, we often use JSON files in order to define content (e.g. for internationalization) or extracting some form of configuration. Especially in the former case, we usually end up with one huge JSON file, probably a couple or hundred or thousand lines long.

Meet the json-bundler, a NodeJS-based command line tool, enabling you to place multiple JSON files in multiple places - and bundle them together intelligently.

For example, the json-bundler allows you to place your i18n files directly next to your component implementations (e.g. next to Angular components), or to reference JSON files published within a npm library.

JSON Bundler Preview

How to install

You can get the json-bundler via npm by either adding it as a new devDependency to your package.json file and running npm install, or running the following command:

npm install json-bundler

Requirements

  • json-bundler requires NodeJS 7.6 (or higher) to be installed
  • If using Travis CI, we recommend setting the environment variable FORCE_COLOR to 1 in order to enable colored logging

How it works

Using the json-bundler is very straightforward. In general:

  • Entry Point There must be exactly one JSON file acting as the singular entry point (e.g. index.json or en.json).
  • References JSON files are referenced (and thus included) using the $ref key, and the path to the JSON file as the value. Reference paths are always relative to the JSON file they're being defined within. Paths starting with ~ are pointing to the project's node_modules folder (for easy npm library access).
  • Bundling When bundling, referenced JSON files get merged in (instead of just placed in), at the exact position in the JSNO files at which they're being referenced. Existing values will not be overwritten by referenced files (the "referencee" has always higher priority).

Also: Both json and json5 files are supported, even in a mixed manner. That means you can do linebreaks, use comments, and much more. See JSON5 for further details.

How to use

It's recommended to use the json-bundler within one of your package.json scripts. For instance:

{
  "scripts": {
    "json:bundle": "json-bundler --entryFile <PATH> --outFile <PATH>"
  }
}

The following parameters are available:

  • --entryFile <PATH> (required) defines the path to the root JSON / JSON5 file
  • --outFile <PATH> (required) defines the path to the output file
  • --minified enables, if used, the minification of the output file (production build, in other words)
  • --watch enables the watch mode - which comes in very handy during development

You can always run json-bundler --help to get a full list of available command line parameters.

Example

The following is a very simple example, demonstrating the basics of json-bundler. For further examples, feel free to take a look at the unit tests of the library.

Source

First file at src/app/en.json (entry file):

{
  "title": "My application title",
  "login": {
    "$ref": "./pages/login.json5"
  },
  "home": {
    "title": "Welcome back, it's Christmas time!",
    "$ref": "./pages/home.json"
  },
  "footer": {
    "$ref": "~my-library/i18n/footer.json"
  }
}

Second file at src/app/pages/login.json5 (referenced file):

// Login Page
{
  "title": "Login", // TODO: Rename to register?
  "form": {
    "user": "Please enter your username.",
    "password": "Please enter your password."
  }
}

Third file at src/app/pages/home.json (referenced file):

{
  "title": "Welcome back!",
  "description": "Lorem ipsum dolor sit amet."
}

Fourth file at node_modules/my-library/i18n/footer.json (referenced file):

{
  "copyright": "My company",
  "legal": "My super-duper important legal information. Plus imprint, of course."
}

Notice that:

  • One JSON file can reference multiple other JSON files
  • The place of the reference within the JSON structure will define where the referenced file gets merged in
  • The paths are relative to the file they are defined within
  • The ~ symbol can be used to access libraries (as a shortcut)
  • The paths include the file ending (either .json or .json5)

Bundling

We use the following command to create the bundle:

json-bundler --entryFile src/app/en.json --outFile dist/en.json

Notice that:

  • We use the entry file from above
  • We only define the required parameters here (entryFile and outFile)

Output

The result is a JSON file at dist/en.json, and it contains the following:

{
  "title": "My application title",
  "login": {
    "title": "Login",
    "form": {
      "user": "Please enter your username.",
      "password": "Please enter your password."
    }
  },
  "home": {
    "title": "Welcome back, it's Christmas time!",
    "description": "Lorem ipsum dolor sit amet."
  },
  "footer": {
    "copyright": "My company",
    "legal": "My super-duper important legal information. Plus imprint, of course."
  }
}

Notice that:

  • Referenced files get merged in at the place they got referenced
  • Files are included, no matter if they are json or json5, no matter if they exist within the project or come from a library
  • The home/title has the value of the src/app/en.json file, and not the value defined in src/app/pages/home.json - the "parent" (aka the referencee) always has higher priority than the referenced file during merge

Creator

Dominique Müller