@opensource365/spfx-env
v2.0.0
Published
Add environments for your SPFx solution
Downloads
156
Maintainers
Readme
spfx-env
⚠️ Breaking Change In 2.0.0
From version 2.0.0, spfx-env supports only Heft-based SPFx projects.
If your project still uses gulp-based SPFx build tooling, stay on a 1.x version of 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:
- Configuring Heft webpack patches to inject environment config logic
- Adding reusable scripts
- Creating
.env.devand.env.prodand.envfiles - Modifying
package-solution.jsoninto a template form - Updating
package.jsonwith 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
spfx-env --dry-runThis will apply all enhancements in one go using a command pipeline. Use
--dry-runto preview planned changes without modifying files or installing dependencies.
🧪 Development Serve npm run serve
Usage:
npm run serve
npm run serve -- dev
npm run serve -- prod- Launches
heft startfor the given environment. - Internally sets
SPFX_ENVto your provided value. npm run servewill run on dev environment
🚧 Pack for single environment
Usage:
npm run pack
npm run pack -- dev
npm run pack -- prodWhat It Does:
- Sets
SPFX_ENV(by defaultdev) - Runs the full
packprocess - Use
process.env.SPFX_ENVin your SPFx code for the selected environment name
🚀 Pack for all environments
Usage:
npm run pack:allWhat It Does:
- Executes your custom
scripts/pack-all.js, which:- Iterates over all available environments (
env/.env.{SPFX_ENV}) - Sets
SPFX_ENVaccordingly for each environment - Builds and packages the SPFx solution for each environment
- Packages will be stored in
sharepoint/solution/{SPFX_ENV}
- Iterates over all available environments (
🧰 Core Build Script npm run pack:core
- Performs the full SPFx packaging pipeline for the current
SPFX_ENV. - Assumes you’ve set
SPFX_ENVbefore calling this script.
🔧 Pipeline Overview
When running spfx-env, the following pipeline steps are executed sequentially:
1. Setup Environment Files
- Creates environment-specific files:
config/env/.envconfig/env/.env.devconfig/env/.env.prod
- Both files are based on a
.envtemplate located insrc/templates. - The base
.envfile acts as the base for all environments. Values defined in.env.devor.env.prodoverride the base variables as needed. - Existing env files are preserved and are not overwritten.
2. Setup Scripts
- Copies custom build scripts from
src/templates/scriptsinto the rootscripts/directory.
3. Configure Build Pipeline
- Creates
config/webpack-patch.jsonandscripts/spfx-env-webpack-patch.js. - Preserves existing
patchFilesentries and appends thespfx-envpatch only once. - Upgrades older
config/webpack-patch/*patch locations toscripts/spfx-env-webpack-patch.jsif needed.
4. Patch package.json
- Adds or updates essential scripts:
pack:corepackservepack:all
- Installs necessary devDependencies:
dotenvdotenv-webpack@types/webpack-env
5. Patch package-solution.json
- Renames
package-solution.jsontopackage-solution.tpl.json. - Enables use of environment variables inside the template:
{ "solution": { "path": "solution/$SPFX_ENV$/<your-package>.sppkg" } } - Supports
$ENV_VARIABLE_NAME$placeholders.
6. Install Dependencies
- Installs all newly added dependencies and scripts.
🧪 Development
To test locally:
npm run build
npm linkThen, in any SPFx project:
spfx-env💡 Notes
- Custom
.envtemplates should be placed insrc/templates/.env. - Assumes
package-solution.jsonis located inconfig/. - Generated Heft integration lives in
config/webpack-patch.jsonandscripts/spfx-env-webpack-patch.js.
📄 License
MIT © OpenSource365
