@helloample/netlify-plugin-replace

1.1.4 • Public • Published

netlify-plugin-replace

build status npm version

Replace environment variables in your publish directory before Netlify processes your build. Check out an example implementation here.

Why?

Netlify will deploy whatever you stick in your publish directory. This is great for simple use-cases but for projects that span multiple environments or rely heavily on environment variables, it can be tricky manage those differences without resorting to gnarly sed scripts or other unmentionable approaches.

This build plugin solves that problem by replacing references to environment variables after your project is built. A great example of this plugin's power is dynamically updating your _redirects file based on its deployment context.

Install

Add the plugin to your project's package.json file...

$ npm i @helloample/netlify-plugin-replace -D

Add the plugin to your project's netlify.toml file (see configuration for available options)...

[[plugins]]
  package = "@helloample/netlify-plugin-replace"

Commit & push changes back to your repository.

Usage

By default, this plugin will look for strings that match the following pattern...

${env:SOME_VALUE}

...and will attempt to replace that string with the value of the ENV variable by the same name. If the variable doesn't exist, it will not be replaced.

Following this example, any file in your build's output directory containing the string ${env:SOME_VALUE} will have that string replaced with the contents of process.env.SOME_VALUE when this plugin runs.

Configuration

This plugin offers some limited customization options as described below. Simply add these key/values to netlify.toml to customize the behavior for your needs.

  • delimiter: Regex pattern that tells the plugin how to find values to replace. Note, this pattern should contain a single capture group which encapsulates the entire name of the variable
  • fileTypes: Regex pattern defining the extension of the files to operate upon

The default configuration, if none is specified, is:

[[plugins]]
  package = "@helloample/netlify-plugin-replace"
  [plugins.inputs]
    delimiter = "\${env:([^}]_)}"
    fileTypes = ".*$"

License

This project is licensed under the MIT License.

Readme

Keywords

Package Sidebar

Install

npm i @helloample/netlify-plugin-replace

Weekly Downloads

13

Version

1.1.4

License

MIT

Unpacked Size

22.3 kB

Total Files

18

Last publish

Collaborators

  • tcmacdonald