html-webpack-dynamic-env-plugin
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

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.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.0.20latest

Version History

VersionDownloads (Last 7 Days)Published
0.0.20
0.0.10

Package Sidebar

Install

npm i html-webpack-dynamic-env-plugin

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

17.8 kB

Total Files

25

Last publish

Collaborators

  • serhii-ohorodnyk