@opensource365/spfx-env
v1.3.0
Published
Add environments for your SPFx solution
Maintainers
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.jsto 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-envThis 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-serveorgulp servefor the given environment. - Internally sets
NODE_ENVto your provided value. - Controlled via
scripts/serve.js. 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
NODE_ENV(by defaultdev) - Runs the full
packprocess
🚀 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.{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}
- Iterates over all available environments (
🧰 Core Build Script npm run pack:core
- Performs the full SPFx packaging pipeline for the current
NODE_ENV. - Assumes you’ve set
NODE_ENVbefore 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/.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.
3. Setup Scripts
- Copies custom build scripts from
src/templates/scriptsinto the rootscripts/directory. - Additional script adjustments are made based on Fast-Serve detection.
4. Patch Gulpfile.js
- Inserts
gulpCreatePackageSolution()andgulpDeletePackageSolution(). - Injects
webpack.gulpSetupEnv()intobuild.configureWebpack.mergeConfig(...). - Ensures insertion before
build.initialize(require('gulp')).
5. Patch package.json
- Adds or updates essential scripts:
pack:corepackservepack:all
- Installs necessary devDependencies:
gulp-renamedotenv-webpack@types/webpack-envtypescript
6. Patch package-solution.json
- Renames
package-solution.jsontopackage-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 linkThen, in any SPFx project:
spfx-env💡 Notes
- Custom
.envtemplates should be placed insrc/templates/.env. - Assumes
package-solution.jsonis located inconfig/.
📄 License
MIT © OpenSource365
