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

gatsby-plugin-pagenv

v0.2.2

Published

Inject ENV Vars into Gatsby pageQueries at build time.

Downloads

528

Readme

Description

Using ENV variables inside of your Page Queries is not as easy as it might seem at first. This plugin handles injecting a whitelist of allowed ENV variables into your createPage context so that they can be used when making queries.

Note: Static Queries in components are NOT supported.

How to install

Overview of Steps:

  • Install Plugin as Dependency

  • Create .env files

  • Add plugin to config

  • Modify/Create PageQuery

Install Plugin as Dependency

npm install -D gatsby-plugin-pagenv

or

yarn add -D gatsby-plugin-pagenv

Create .env Files

The development .env file is required as a base.

MY_ENV_VAR=devValue

From there, you will likely want to create a separate .env.* file where * is the name of your other environment. Such as .env.staging.

MY_ENV_VAR=stagingValue

Add Plugin to Config

{
  plugins: {
    {
      resolve: "gatsby-plugin-pagenv",
      options: {
        allowedVariables: [
          {
            key: "INDEX_LOGO_IMAGE",
            type: "String", // Optional since String is default
          },
          {
            key: "INDEX_LOGO_IMAGE_WIDTH",
            type: "Int",
          },
        ],
      },
    }
  }
}

Modify/Create pageQuery

There are a couple important things to take note of.

You will pass the env variables you allowed into the query with a type annotation:

query ($INDEX_LOGO_IMAGE: String!)

Lastly, use the variable in your query somewhere:

(relativePath: { eq: $INDEX_LOGO_IMAGE }) {

When you put them together, it will look something like this:

export const pageQuery = graphql`
  query ($INDEX_LOGO_IMAGE: String!) {
    newPlaceholderImage: file(relativePath: { eq: $INDEX_LOGO_IMAGE }) {
      childImageSharp {
        fluid(maxWidth: 300) {
          ...GatsbyImageSharpFluid
        }
      }
    }
  }
`;

Available options

  • allowedVariables: [] - an array of objects containing the ENV variables you would like to be grabbed from process.env and their scalar types
{
  key: "INDEX_LOGO_IMAGE_WIDTH",
  type: "Int",
},

You can find out more about GraphQL types here: https://graphql.org/learn/schema/#scalar-types

Passing Environment

By default, pagenv looks at the ENVIRONMENT env variable.

eg:

ENVIRONMENT=staging yarn build

If you would like to change the env variable name that pagenv looks for you can do so with PAGENV_ENVIRONMENT_VAR.

eg:

PAGENV_ENVIRONMENT_VAR=DEPLOY_ENV DEPLOY_ENV=staging yarn build

When do I use this plugin?

Examples:

  • Loading mock data in Development

  • Changing image processing values to speed up Development

Examples of usage

You can see a full example within the examples/ folder of this repo. We will add more as more use cases are discovered.

How to run tests

?

How to develop locally

?

How to contribute

If you have unanswered questions, would like help with enhancing or debugging the plugin, it is nice to include instructions for people who want to contribute to your plugin.