webpack-replace-loader
Advanced tools
+36
-21
@@ -9,4 +9,5 @@ | ||
| var arr = [ | ||
| '[webpack-replace-loader: warning] The configuration rule of webpack is not allowed! -> https://github.com/beautifulBoys/webpack-replace', | ||
| '[webpack-replace-loader: warning] The property "search" and "replace" is essential' | ||
| '[webpack-replace-loader: Error] The configuration rule of webpack is not allowed! -> https://github.com/beautifulBoys/webpack-replace', | ||
| '[webpack-replace-loader: Error] The property "search" and "replace" is essential', | ||
| '[webpack-replace-loader: Error] The property "arr" should be an Array.' | ||
| ]; | ||
@@ -32,2 +33,8 @@ throw new Error(arr[num]); | ||
| } | ||
| function replaceFunc (configArray, source) { | ||
| for (let i = 0; i < configArray.length; i++) { | ||
| source = source.replace(new RegExp(stringEscape(configArray[i].search), configArray[i].attr), configArray[i].replace); | ||
| } | ||
| return source; | ||
| } | ||
@@ -37,29 +44,37 @@ module.exports = function (source, map) { | ||
| var options = loaderUtils.getOptions(this); | ||
| if (!options.arr) { | ||
| warning(0); | ||
| } else { | ||
| for (let i = 0; i < options.arr.length; i++) { | ||
| if (!options.arr[i].replace || !options.arr[i].search) { | ||
| warning(1); | ||
| } else { | ||
| let replace = options.arr[i].replace; | ||
| let search = options.arr[i].search; | ||
| let attr; | ||
| if (options.arr[i].hasOwnProperty('attr')) { | ||
| attr = options.arr[i].attr; | ||
| } else if (options.hasOwnProperty('attr') && !options.arr[i].hasOwnProperty('attr')) { | ||
| attr = options.attr; | ||
| let configArray = []; | ||
| if (options.hasOwnProperty('arr')) { | ||
| if (Array.isArray(options.arr)) { | ||
| for (let i = 0; i < options.arr.length; i++) { | ||
| let option = options.arr[i]; | ||
| if (option.hasOwnProperty('search') && option.hasOwnProperty('replace')) { | ||
| configArray.push({ | ||
| search: option.search, | ||
| replace: option.replace, | ||
| attr: option.attr ? option.attr : '' | ||
| }); | ||
| } else { | ||
| attr = ''; | ||
| warning(1); | ||
| } | ||
| source = source.replace(new RegExp(stringEscape(search), attr), replace); | ||
| } | ||
| } else { | ||
| warning(2); | ||
| } | ||
| } else { | ||
| if (options.hasOwnProperty('search') && options.hasOwnProperty('replace')) { | ||
| configArray.push({ | ||
| search: options.search, | ||
| replace: options.replace, | ||
| attr: options.attr ? options.attr : '' | ||
| }); | ||
| } else { | ||
| warning(0); | ||
| } | ||
| } | ||
| source = replaceFunc(configArray, source); | ||
| this.callback(null, source, map); | ||
| return source; | ||
| }; |
+1
-1
| { | ||
| "name": "webpack-replace-loader", | ||
| "version": "1.2.4", | ||
| "version": "1.2.5", | ||
| "description": "A loader for replace string by webpack", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
10291
5.27%65
32.65%