-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
niminjie
committed
Sep 13, 2016
1 parent
197cc91
commit 00d363f
Showing
3 changed files
with
58 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
/** | ||
* @file index | ||
* @author niminjie([email protected]) | ||
*/ | ||
|
||
module.exports = require('./lib/HtmlStringReplace'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|