npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rollup-plugin-replace-html-vars

v1.0.3

Published

A rollup.js plugin to replace variables in html files

Downloads

4,173

Readme

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
import replaceHtmlVars from 'rollup-plugin-replace-html-vars';

// 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_
    replaceHtmlVars({
        files: 'path/to/files/example.html',
        from: '_THIS_IS_THE_VARIABLE_',
        to: '1.0.0',
    })
]
// Use in example.html
<p>Current version: _THIS_IS_THE_VARIABLE_</p>
// Results in value of options.to, here '1.0.0'
<p>Current version: 1.0.0</p>

Replace multiple variables

Replace first occurrence only:

// Import the plugin
import replaceHtmlVars from 'rollup-plugin-replace-html-vars';

// 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
    replaceHtmlVars({
        files: 'path/to/files/example.html',
        from: ['FIRST_VARIABLE', 'second-variable'],
        to: ['Number one', 'Only second'],
    })
]
// Use in example.html
<p>Jane Doe is: FIRST_VARIABLE</p>
<p>John Doe is: second-variable</p>
// Results in value of options.to, here '1.0.0'
<p>Jane Doe is: Number one</p>
<p>John Doe is: Only second</p>

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
import replaceHtmlVars from 'rollup-plugin-replace-html-vars';

// 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}
    replaceHtmlVars({
        files: 'path/to/files/*.html',
        from: /\${timestamp}/g,
        to: Date.now(),
    })
]
// Use in an array of html files
<link rel="stylesheet" type="text/css" href="static/bundle.css?t=${timestamp}" />
<script src="static/bundle.js?t=${timestamp}"></script>
// Results in options.to, in this example Date.now()
<link rel="stylesheet" type="text/css" href="static/bundle.css?t=1510343208773" />
<script src="static/bundle.js?t=1510343208773"></script>

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