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

nuxt-env-injector

v0.0.5

Published

Nuxt.js module that helps to inject env vars on runtime

Downloads

12

Readme

What problem does it solve

It only helps to have better Docker images for better CI/CD.

Let's imagine you want to build a Docker image and deploy it to two different environments: qa-env and staging-env. You don't want to build Nuxt twice for that (because of security, integrity, CI-performance, etc.), so Docker image should already have buildDir (.nuxt) inside. The thing is, almost all environmental variables you pass to such image on docker run, won't be applied, since they already hardcoded by Webpack into nuxt bundle. You only need to install this module to solve this issue.

Why other solutions don't work

If you make a research, there are basically three solutions:

  1. Use Vuex to store the variables. Or use nuxt-env module that stores variables in nuxt context this.$env. In most cases, you need your variables outside of nuxt context, for example baseURL for http-client, so it doesn't work well.
  2. Use regex-powered script that replaces variables on every nuxt-server start. It works, but you'll have to store your variables keys both in script-file and docker-file to make it work. Not perfect.
  3. Use window.__CONFIG__ = {} object hardcoded into page template. This only works for SPA-projects.

How does it work

Basically it is the second solution I mentioned above (marker-replacer-script), but this time you don't need to think about implementation, it just works*.

  1. Before Nuxt started building its bundle, the module iterates every variable you mentioned in env option of nuxt.config.js and replaces it with a special marker: %%INJECTED_VARKEY%%
  2. Nuxt builds a bundle that has markers instead of actual variables values, for example baseURL: '%%INJECTED_VARKEY%%'. At this point the bundle will be saved inside of Docker image.
  3. You are trying to start the Docker image I mentioned above. Before Nuxt starts, the module iterates though every .js file inside of .nuxt dir and replaces markers with actual variables you passed to your Docker image.
  4. Voilà. You have your Nuxt-app running only with variables that you used for docker run command (or in your CD config).

How to use

WIP. Check /example/README.md for now.

Caveats ⚠️ ⚠️ ⚠️

Don't use with NODE_ENV or flag-variables. Doesn't work with variables that are used in head section of nuxt.config. You need all your env vars to be passed to docker run, values assigned on build-time will be ereased. Module logic is not ideal, it is ⚠️ Work In Progress ⚠️.

Turns out there is a simillar project, you can try it instead.