Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
niminjie committed Sep 13, 2016
1 parent 197cc91 commit 00d363f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
/**
* @file index
* @author niminjie([email protected])
*/

module.exports = require('./lib/HtmlStringReplace');
43 changes: 35 additions & 8 deletions lib/HtmlStringReplace.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
/**
* @file HtmlStringReplace
* @author niminjie([email protected])
*/

var util = require('./util');

/**
* @constructor
*
* @param {Object} options plugin options
* @param {Array} options.patterns regex patterns and replacement function
* @param {Boolean} enable whether enable this plugin or not
*/
function HtmlStringReplace(options) {
this.patterns = options.patterns || []
this.enable = options.enable === undefined ? true : options.enable;
this.patterns = options.patterns || [];

// enable the plugin when not passing `enable` param
this.enable = options.enable === undefined
? true
: options.enable;
}

/**
* apply plugin
*
* @param {Object} compiler webpack compiler
*/
HtmlStringReplace.prototype.apply = function (compiler) {
var that = this;

util.getCompilation(compiler, function (htmlPluginData, callback) {
util.afterHtmlProcessing(compiler, function (htmlPluginData, callback) {
if (that.enable) {
htmlPluginData.html = that.replaceString(htmlPluginData.html, htmlPluginData.plugin.options);
}

callback(null, htmlPluginData);
})
});
};

/**
* replace html string
*
* @param {String} html html source
* @param {Object} htmlPluginOptions `HtmlWebpackPlugin` options
*
* @return {String} replaced html string
*/
HtmlStringReplace.prototype.replaceString = function (html, htmlPluginOptions) {
var patterns = this.patterns;

patterns.forEach(function (pattern) {
this.patterns.forEach(function (pattern) {
var match = pattern.match || new RegExp();
var replacement = pattern.replacement || util.defaultReplace;
var replacement = pattern.replacement || util.defaultReplacement;

html = html.replace(match, replacement);
});
Expand Down
19 changes: 18 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
/**
* @file util
* @author niminjie([email protected])
*/

module.exports = {
getCompilation: function(compiler, callback) {
/**
* Get `html-webpack-plugin-after-html-processing` callback
*
* @param {Object} compiler webpack compiler
* @param {Function} callback callback
*/
afterHtmlProcessing: function(compiler, callback) {
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-after-html-processing', callback);
});
},

/**
* default replacement function
*
* @param {String} match matched string
* @return {String}
*/
defaultReplace(match) {
return match;
}
Expand Down

0 comments on commit 00d363f

Please sign in to comment.