Snippets

killerspaz webpack-useref-plugin

Created by killerspaz last modified
/**
 * Simple `useref` in-line Plugin that ties into `html-webpack-plugin`.
 *
 * This will simply run `useref` and replace the HTML that will be processed.
 **/

const HtmlWebpackPlugin = require('html-webpack-plugin');
const useref = require('useref');

const AppName = JSON.parse(require('fs').readFileSync('./package.json')).name;

module.exports = {
  entry: { frontend: './frontend/index.js' },
  plugins: [
    new HtmlWebpackPlugin({
      title: AppName,
      template: './frontend/index.html'
    }),
    
    //NOTE: it's best to put this in it's own module, and only include if targeting a Production build.
    new class WebpackUseref {
      apply(compiler) {
        compiler.plugin('compilation', function (compilation) {
          console.log('[webpack-useref-plugin] - Initializing');
          compilation.plugin('html-webpack-plugin-before-html-processing', function (htmlPluginData, callback) {
            let result = useref(htmlPluginData.html);

            htmlPluginData.html = result[0];
            callback(null, htmlPluginData);
          })
        });
      }
    }
  ]
}

Comments (1)

  1. Telltims

    Tim Hortons is one of the famous fast-food chain in entire Canada. They want to know their customers level of satisfaction of their services. So they conducts a customer feedback survey called Telltims at Telltims.ca survey. And also they offering some amazing gifts to participants. It means you can grab a chance to win a $1 Hot Chocolate, French Vanilla, or Iced Coffee by participating in this survey.

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.