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-env-loader

v1.0.1

Published

A webpack loader to handle specific code base on different environment

Downloads

5

Readme

app-env-loader

A webpack loader to handle specific code base on a different environment. It is likely that our code will run on a different environment that needs to be handled specifically. Let's say making API request, it is gonna to request different API server when running code on dev, testing, live environment. this app-env-loader is designed for this scenario.

Install

npm install app-env-loader --save-dev

Usage

webpack.config.js

    ...
      {
        test: /\.(js|mjs|jsx|ts|tsx)$/,
        include: paths.appSrc,
        use: [
          {
            loader: require.resolve('babel-loader'),
            options: {
              ...
            },
          },
          {
            loader: 'app-env-loader' // <-- add loader here 
          }
        ]
      }
    ...

your environment specific files gonna look like down below, base on dev, testing and live environment

// ./config/config.env.js
export default {
  apiHost: 'dev.api.com',
}
// ./config/config.env.testing.js
export default {
  apiHost: 'testing.api.com',
}
// ./config/config.env.live.js
export default {
  apiHost: 'live.api.com',
}

the '.env.' is required in the file name to tell the loader that you need to handle the file specifically, and an environment specific name is followed right after to tell the loader to handle it on the specific envirnoment. you can import your config file:

// index.js
import { apiHost } from './config/config.env.js'

you can config the 'package.json' file:

  ...
  "scripts": {
    "start": "node scripts/start.js", // <-- this will load the default env file
    "dev": "node scripts/start.js APP_ENV=dev", // <-- set the APP_ENV to the dev environment
    "build-live": "node scripts/build.js APP_ENV=live ", // <-- set the APP_ENV to the live environment
    "build-test": "node scripts/build.js APP_ENV=testing " // <-- set the APP_ENV to the testing environment
  },
  ...

Now, you are able to run your code and the loader will know which file need to be loaded base on the APP_ENV.

APP_ENV is not set  ------->  config.env.js
APP_ENV=dev         ------->  config.env.dev.js
APP_ENV=testing     ------->  config.env.testing.js
APP_ENV=live        ------->  config.env.live.js

If you are not happy with the name 'env', you can customise it yourself by setting the loader option:

webpack.config.js

    ...
      {
        test: /\.(js|mjs|jsx|ts|tsx)$/,
        include: paths.appSrc,
        use: [
          {
            loader: require.resolve('babel-loader'),
            options: {
              ...
            },
          },
          {
            loader: 'app-env-loader', 
            options: {
              test: /\.environment\./ // <-- set the RegExp the match the name you like. 
            }
          }
        ]
      }
    ...

and now you files name can be like down below:

config.environment.js
config.environment.dev.js
config.environment.testing.js
config.environment.live.js