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

pika-plugin-inline-env

v0.1.0

Published

> Note: this plugin in intended to be used with `@pika/plugin-ts-standard-pkg`. If you're using `@pika/plugin-standard-pkg` see that [section for more details](#using-with--pikaplugin-standard-pkg).

Downloads

17

Readme

pika-plugin-inline-env

Note: this plugin in intended to be used with @pika/plugin-ts-standard-pkg. If you're using @pika/plugin-standard-pkg see that section for more details.

A really simple, tiny pika plugin to inline process.env environment variables in dist-src so downstream builds don't become bloated with extra rollup plugins or custom solutions.

Problem

Using the standard ts plugin from pika, there's no way to inline/inject environment variables in the generated dist-src folder. For example:

// some other code...
export const VERSION = process.env.VERSION;

after tsc generates the bundle used by the node/web/umd builder, we're left with dist-src/index.js containing something like:

// some other code...
export const VERSION = process.env.VERSION;

There are two issues here:

  1. App authors using bundlers like Webpack have no way of knowing the version of your library
  2. If a UMD distribution exists for your library, rollup doesn't replace the variable for free

How it Works

This plugin works by reading your dist-src/index.js (or other configured file) file using node fs, performing a simple search and replace and writing the file back to disk. All operations are blocking so make sure there are no other operations on the same file while the plugin is executing.

Install

npm i @pika/pack pika-plugin-inline-env --save-dev

Usage

Note: All items under the env array are considered to live on process.env therefore you can omit process.env if you wish.

{
    "name": "example-package-json",
    "version": "1.0.0",
    "@pika/pack": {
        "pipeline": [
            ["@pika/plugin-ts-standard-pkg"],
            [
                "pika-plugin-inline-env",
                {
                    "env": ["npm_package_version", "process.env.DEBUG"]
                }
            ]
        ]
    }
}

and to build:

DEBUG=false && npx pika build

Tip: use cross-env to ensure this works on windows too

cross-env MY_VERSION=1.2.3 && npx pika build

At the env of the build, we'll be left with a clean, inline environment variable in dist-src/index.js:

// other code...
export const VERSION = '1.2.3';

For more information about @pika/pack & help getting started, check out the main project repo.

Options

All options are optional.

| Option | Type | Default Value | Description | | ------------ | ------------------------------------------------------------------------------------------- | ------------- | ----------------------------------------------------------------------------------------------- | | "env" | string[] | [] | An array of env variables to replace. If it does not exist an empty string will be used instead | | "debug" | boolean | 'trace' | false | Set true to enable debugging info on build failures | | "encoding" | BufferEncoding | 'utf-8' | Override the file read and write encoding | | "bail" | boolean | false | If an environment variable is missing, fail the build | | "file" | string | 'index.js' | Override the file to read and write to |

Using with @pika/plugin-standard-pkg

This is not intended to be used with @pika/plugin-standard-pkg, use babel-plugin-transform-inline-environment-variables to inline environment variables using the standard packager.

Gotchas

Passing environment variables from NPM scripts has some issues. Considering the following:

{
    "scripts": {
        "build": "pika build",
        "pretest": "MY_VAR=1.2.3 npm run build",
        "test": "node test/assert.js"
    }
}

I suspect npm run is launched in a new child process. The solution is to place the env vars on the same line:

{
    "scripts": {
-        "build": "pika build",
+        "build": "MY_VAR=1.2.3 pika build",
-        "pretest": "MY_VAR=1.2.3 npm run build",
+        "pretest": "npm run build",
        "test": "node test/assert.js"
    }
}

or, cross-env-shell can be used:

{
    "scripts": {
-        "build": "pika build",
+        "build": "cross-env-shell pika build",
-        "pretest": "MY_VAR=1.2.3 npm run build",
+        "pretest": "cross-env MY_VAR=1.2.3 npm run build",
        "test": "node test/assert.js"
    }
}