Replace variables in HTML with rollup
A rollup.js plugin to replace variables in html files during the build process, based on replace-in-file. Make a single replacement or multiple replacements at once.
Installation
# Using npm npm install rollup-plugin-replace-html-vars --save-dev # Using yarn yarn add rollup-plugin-replace-html-vars
Simple example
Replace first occurrence only:
// Import the plugin; // Load the library and specify options// rollup.js configuration...plugins: // Place at the end of your rollup plugin section // Any string is fine as variable, // here we use _THIS_IS_THE_VARIABLE_
// Use in example.htmlCurrent version: _THIS_IS_THE_VARIABLE_
// Results in value of options.to, here '1.0.0'Current version: 1.0.0
Replace multiple variables
Replace first occurrence only:
// Import the plugin; // Load the library and specify options// rollup.js configuration...plugins: // Place at the end of your rollup plugin section // Any string is fine as variable
// Use in example.htmlJane Doe is: FIRST_VARIABLEJohn Doe is: second-variable
// Results in value of options.to, here '1.0.0'Jane Doe is: Number oneJohn Doe is: Only second
Regular expressions and easy cache-buster
Replace all occurrences of ${timestamp}
in your code, a simple way to create a cache-buster with rollup.js:
// Import the plugin; // Load the library and specify options// rollup.js configuration...plugins: // Place at the end of your rollup plugin section // Any string is fine as variable, here we use ${timestamp}
// Use in an array of html files
// Results in options.to, in this example Date.now()
More Options
rollup-plugin-replace-html-vars
is a plugin for rollup.js which wraps replace-in-file
. All options of replace-in-file are available for rollup-plugin-replace-html-vars
.
License
(MIT License)
Copyright 2017, Andreas Jacob