/html-webpack-dynamic-env-plugin

Extension plugin for html-webpack-plugin that allows specifying dynamic environment for static html apps

Primary LanguageTypeScriptMIT LicenseMIT

Build Status

HTML Webpack Dynamic Env Plugin

This is an extension plugin for the webpack plugin html-webpack-plugin that allows configure environment variables for static html app after build step and before deployment (e.g. push to cdn or docker run)

How does it work?

This plugin subscribes on html-webpack-plugin hooks and webpack hooks, enhances output html files with default environment variables, plus generates template files for every output html with a script file to replace environment variables.

Why do I need it?

If you need/want to be able to configure your environment variables after app bundle and you don't want/can't setup heavy server side.

Installation

npm i -D html-webpack-dynamic-env-plugin

Basic Usage

Require the plugin in your webpack config:

var HtmlWebpackDynamicEnvPlugin = require('html-webpack-dynamic-env-plugin');

Add the plugin to your webpack config (remember to add it after html-webpack-plugin):

plugins: [
  new HtmlWebpackPlugin(),
  new HtmlWebpackDynamicEnvPlugin({
    envVars: {
      YOUR_ENV_VARIABLE: "default-value-here",
      ANOTHER_ENV_VAR: process.env.SET_DEFAULT_VALUE_FROM_BUILD_ENV || ''
    }
  })
]  

This will generate index.html file from HtmlWebpackPlugin with injected script tag that defines your environment defaults. It should look something like this:

window.CLIENT_ENV = { YOUR_ENV_VARIABLE: "default-value-here", ANOTHER_ENV_VAR: "" }

Plus you'll have your config-env.sh and index.html.template files generated that you can use to configure your environment again, like this:

YOUR_ENV_VARIABLE=newvalue ./config-env.sh index.html.template index.html

Configuration

List of available configuration options:

Name Type Default Description
envVars { [key: string]: string | undefined } `` Key-value map of environment variables. Values will be used as defaults in generated html output file
windowKeyName string 'CLIENT_ENV' Property name to be used on window object to store environment variables
injectEnvVars (string, string, string) => string headAppend Function that injects environment variables into html and template files. takes html, script tag with env and filename as parameters, returns html with injected tag. By default appeands to head tag
configFileName string 'config-env.sh' File name for the generated configuration script
configFactoryFunc string | envs => string 'shell' Configuration script factory function. Can be either pre-defined type, e.g. 'shell' or a custom function-factory of the type: takes envVars type in and returns script file content as a string
templateFileNames { [key: string]: string } {} Key-value map, where: key - file name of generated by html-webpack-plugin html file; value - template file name for this html.For every not specified html filename - template with default pattern-name will be generated. I.e. { "index.html": "index.html.template" }

Examples

You can find examples in Examples folder.