Environment variables

Environment variables provide a more streamlined way to manage your build process. We can also use it to define "build toggles", or environment-based variables that will be injected into your scripts to be used during runtime.

Setting it up

Let's say that we want to set NODE_ENV to production for uploading to our main branch, and development for uploading to our Simulation branch. First we'll catch the environment variable and assign the compile target based on it.

// rollup.config.js

const isProduction = process.env.NODE_ENV === 'production'

// use the `main` target from `screeps.json` in production mode
const cfg = isProduction ? 'main' : 'sim';

export default {
  // ...
  plugins: [
    // ...
    screeps({
      config: require("./screeps")[cfg],
      // if `NODE_ENV` is local, perform a dry run
      dryRun: process.env.NODE_ENV === 'local'
    })
  ]
}

Running a deploy

Then we'll change the build tasks on package.json to pass the environment variable before running the rollup command.

Now let's give it a try! Run npm run deploy-dev or npm run deploy-prod and see if your code is uploaded properly.

Setting up build toggles

You can also setup deployment-dependent variables (aka. "build toggles") that are injected to your code during build time to allow for more advanced optimisations like dead code elimination.

To do this, install rollup-plugin-replace.

Then configure your rollup.config.js to include your desired variables.

Generally, you need to ensure that rollup-plugin-replace goes before other plugins, so we can be sure Rollup replaces these variables correctly and the remaining plugins can apply any optimisations (e.g. dead code elimination) correctly.

Variables set by this plugin will be replaced in the actual output JS code. When compiling your code, Rollup will replace the variable names with the output of the supplied expression or value.

Once it's set up, you use it in your code.

Notes

Since TypeScript won't recognise these variables if you pass it blindly into your code, you will still need to declare them in a type definition (.d.ts) file.

Also, be careful not to use too common of a name, because it will replace it throughout your code without warning. A good standard is to make the variables all caps, and surrounded by double underscores, so they stand out (e.g. __REVISION__).

Last updated