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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@opensource365/spfx-env

v1.3.0

Published

Add environments for your SPFx solution

Readme

spfx-env

spfx-env is a CLI utility to automate environment setup and configuration enhancements for SharePoint Framework (SPFx) projects.

It simplifies SPFx setup by:

  • Patching gulpfile.js to inject environment config logic
  • Adding reusable scripts
  • Creating .env.dev and .env.prod and .env files
  • Modifying package-solution.json into a template form
  • Updating package.json with necessary scripts and devDependencies

📦 Installation

Install globally via NPM:

npm install -g @opensource365/spfx-env

🚀 Usage

⚠️ Important: Before using spfx-env, make sure to commit (or stash) all your local changes.
The setup process modifies key project files, and having a clean working tree helps avoid merge conflicts or overwrites.

Run in the root directory of your SPFx project:

spfx-env

This will apply all enhancements in one go using a command pipeline.

🧪 Development Serve npm run serve

Usage:

npm run serve
npm run serve -- dev
npm run serve -- prod
  • Launches fast-serve or gulp serve for the given environment.
  • Internally sets NODE_ENV to your provided value.
  • Controlled via scripts/serve.js.
  • npm run serve will run on dev environment

🚧 Pack for single environment

Usage:

npm run pack
npm run pack -- dev
npm run pack -- prod

What It Does:

  1. Sets NODE_ENV (by default dev)
  2. Runs the full pack process

🚀 Pack for all environments

Usage:

npm run pack:all

What It Does:

  1. Executes your custom scripts/pack-all.js, which:
    • Iterates over all available environments (env/.env.{NODE_ENV})
    • Sets NODE_ENV accordingly for each environment
    • Builds and packages the SPFx solution for each environment
    • Packages will be stored in sharepoint/solution/{NODE_ENV}

🧰 Core Build Script npm run pack:core

  • Performs the full SPFx packaging pipeline for the current NODE_ENV.
  • Assumes you’ve set NODE_ENV before calling this script.

🔧 Pipeline Overview

When running spfx-env, the following pipeline steps are executed sequentially:

1. Detect Fast-Serve

  • Detects whether the current SPFx project uses Fast-Serve.

2. Setup Environment Files

  • Creates environment-specific files:
    • config/env/.env
    • config/env/.env.dev
    • config/env/.env.prod
  • Both files are based on a .env template located in src/templates.
  • The base .env file acts as the base for all environments. Values defined in .env.dev or .env.prod override the base variables as needed.

3. Setup Scripts

  • Copies custom build scripts from src/templates/scripts into the root scripts/ directory.
  • Additional script adjustments are made based on Fast-Serve detection.

4. Patch Gulpfile.js

  • Inserts gulpCreatePackageSolution() and gulpDeletePackageSolution().
  • Injects webpack.gulpSetupEnv() into build.configureWebpack.mergeConfig(...).
  • Ensures insertion before build.initialize(require('gulp')).

5. Patch package.json

  • Adds or updates essential scripts:
    • pack:core
    • pack
    • serve
    • pack:all
  • Installs necessary devDependencies:
    • gulp-rename
    • dotenv-webpack
    • @types/webpack-env
    • typescript

6. Patch package-solution.json

  • Renames package-solution.json to package-solution.tpl.json.
  • Enables use of environment variables inside the template:
    {
      "solution": {
        "path": "solution/$NODE_ENV$/<your-package>.sppkg"
      }
    }
  • Supports $ENV_VARIABLE_NAME$ placeholders.

7. Fast-Serve (optional)

If Fast-Serve is detected:

  • Ensures Fast-Serve configuration is extended.
  • Injects environment setup logic into fast-serve/webpack.extend.js.

8. Install Dependencies

  • Installs all newly added dependencies and scripts.

🧪 Development

To test locally:

npm run build
npm link

Then, in any SPFx project:

spfx-env

💡 Notes

  • Custom .env templates should be placed in src/templates/.env.
  • Assumes package-solution.json is located in config/.

📄 License

MIT © OpenSource365