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

app-settings-loader

v2.0.9

Published

A webpack loader to load and customize application settings

Downloads

12,898

Readme

app-settings-loader

build status Coverage Status

NPM

A simple webpack loader to load and customize application settings based on the provided environment.

Why

We often specify the application settings in a JSON file and use it during development; something like below, for example.

{
    "debug": true,
    "endpoint": "http://localhost:8080/",
    "basePath": "api/"
}

However, for production (or any other environment) we may need different settings/values for a subset of keys. For example,

{
    "debug": false,
    "endpoint": "http://my.awesome.service/",
}

Using this loader, you can bundle environment specific values, given 2 JSON files exist.

How

Install

npm i -D app-settings-loader

Use

Let us assume that we have following directory structure.

project_root
|
+-config
| |
| +-appConfig.json
| +-appConfig.development.json
|
+-main.ts
+-webpack.config.js

Then you use the appConfig.json in main.ts as follows.

// with "resolveJsonModule": true (and "esModuleInterop": true) in your tsconfig
import config from "./config/appConfig.json";

// or
const config = require("./config/appConfig.json");

In your webpack add the loader as follows.

{
    test: /appConfig\.json$/i, // change this as per your file name
    use: [
        { loader: "app-settings-loader", options: { env: production ? 'production' : 'development' } },
    ]
},

With the above config, the loader will merge the customization coming from the appConfig.development.json with the base config in appConfig.json, when production is truthy.

How does it work

The loader has only one option env (environment, default value is "development"). If the base JSON file name is mysettings.json, it looks for a JSON file named mysettings.{env}.json at the same directory path. If there is one, it merges the customization form that file with the base file, and returns the result. In case there is no customization file, it returns the base content. Note that if you want you may have more environment names, as per your need. That is you may set env as Betelgeuse5, and provided you have a JSON for that, it will load that too :)

Note

  • Merging configurations: The merge operation keeps the schema of the base file intact. That is, no unknown properties or property value of different type, from the customization files can be applied on the base file. This makes sense because during development you expect a certain set of keys and values of certain datatype to be present in your config and you program against this known schema. Therefore any new keys present in the customization file cannot and should not play any part in the source code.
  • Valid JSON: The content of the both base and customization file needs to be valid JSON. Otherwise, it will throw an error.
  • Do not store sensitive data: As the config will be part of webpack bundle, you should not store any sensitive data in the customization file. The thumb rule is that any sensitive data that you don't want to keep in your source code, should also not be the part of either base or customization config file.

That's it. If you face any problem, feel free to open an issue.