From 3e7e6668bd7bffcdc80bf71ca9e95a2822566878 Mon Sep 17 00:00:00 2001 From: sashless Date: Fri, 13 Nov 2015 14:40:06 +0100 Subject: [PATCH 1/7] can pass array as option to replace more at once can pass a complete array instead of just single values --- index.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 91b2f74..bc010e5 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,13 @@ var utils = require('loader-utils'); -module.exports = function (source) { +module.exports = function(source) { this.cacheable(); - var query = utils.parseQuery(this.query); - if (typeof query.search !== 'undefined' && typeof query.replace !== 'undefined') { - if (typeof query.flags !== 'undefined') { - query.search = new RegExp(query.search, query.flags); - } + var query = utils.parseQuery(this.query); + for (var i = 0; i < query.replace.length; i++) { + var option = query.replace[i]; - source = source.replace(query.search, query.replace); + source = source.split(option.search).join(option.replace) } return source; From b77bd43afdc62bd657c0c402815dd28b5112841f Mon Sep 17 00:00:00 2001 From: sashless Date: Fri, 4 Dec 2015 14:22:13 +0100 Subject: [PATCH 2/7] support single replacement using regex + flags again --- index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index bc010e5..5be0900 100644 --- a/index.js +++ b/index.js @@ -4,10 +4,23 @@ module.exports = function(source) { this.cacheable(); var query = utils.parseQuery(this.query); - for (var i = 0; i < query.replace.length; i++) { - var option = query.replace[i]; - source = source.split(option.search).join(option.replace) + if (Array.isArray(query.replace)) { + for (var i = 0; i < query.replace.length; i++) { + var option = query.replace[i]; + + source = source.split(option.search).join(option.replace) + } + + return source; + } + + if (typeof query.search !== 'undefined' && typeof query.replace !== 'undefined') { + if (typeof query.flags !== 'undefined') { + query.search = new RegExp(query.search, query.flags); + + return source.replace(query.search, query.replace); + } } return source; From 163413f3487442f83d133dc10e5239275a52ed75 Mon Sep 17 00:00:00 2001 From: sashless Date: Fri, 4 Dec 2015 14:33:39 +0100 Subject: [PATCH 3/7] update readme to mention array replacement --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index af8bf60..28316a6 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,29 @@ module.exports = { } ``` +### Array replacement: + +In your `webpack.config.js`: + +```javascript +module.exports = { + // ... + module: { + loaders: [ + { + test: /fileInWhichJQueryIsUndefined\.js$/, + loader: 'string-replace', + query: { + replace: [ + {search: 'AAAAA', replace: 'BBBBB'}, + ] + } + } + ] + } +} +``` + ## Contributing: Feel free to open issues to propose stuff and participate. Pull requests are also welcome. From fec12d0fb872ea36d0688a744e99e4ae6d7e25a7 Mon Sep 17 00:00:00 2001 From: sashless Date: Fri, 4 Dec 2015 14:34:34 +0100 Subject: [PATCH 4/7] fix usage of regex without flags --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5be0900..e677bcb 100644 --- a/index.js +++ b/index.js @@ -18,9 +18,9 @@ module.exports = function(source) { if (typeof query.search !== 'undefined' && typeof query.replace !== 'undefined') { if (typeof query.flags !== 'undefined') { query.search = new RegExp(query.search, query.flags); - - return source.replace(query.search, query.replace); } + + return source.replace(query.search, query.replace); } return source; From 67ac75d2b555db15050e6a05704bb4a9c09a1523 Mon Sep 17 00:00:00 2001 From: sashless Date: Fri, 4 Dec 2015 14:37:52 +0100 Subject: [PATCH 5/7] improve readme about array replacements --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 28316a6..8b7a337 100644 --- a/README.md +++ b/README.md @@ -69,11 +69,12 @@ module.exports = { module: { loaders: [ { - test: /fileInWhichJQueryIsUndefined\.js$/, + test: /\.js$/, loader: 'string-replace', query: { replace: [ - {search: 'AAAAA', replace: 'BBBBB'}, + {search: 'framework', replace: 'flamewar'}, + {search: 'ants', replace: 'super ants'}, ] } } From adef248908916da6a57be2434a9789759dcb9358 Mon Sep 17 00:00:00 2001 From: sashless Date: Fri, 4 Dec 2015 15:15:10 +0100 Subject: [PATCH 6/7] simplify replacement and get rid of flags for regex --- index.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index e677bcb..b0e96f7 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,28 @@ var utils = require('loader-utils'); +function processQuery(source, option) { + if (typeof option.search !== 'undefined' && typeof option.replace !== 'undefined') { + return source.split(option.search).join(option.replace); + } + + return source; +} + module.exports = function(source) { this.cacheable(); var query = utils.parseQuery(this.query); - if (Array.isArray(query.replace)) { - for (var i = 0; i < query.replace.length; i++) { - var option = query.replace[i]; + if (Array.isArray(query.multiple)) { + var length = query.multiple.length; - source = source.split(option.search).join(option.replace) + for (var i = 0; i < length; i++) { + var option = query.multiple[i]; + source = processQuery(source, option); } return source; } - if (typeof query.search !== 'undefined' && typeof query.replace !== 'undefined') { - if (typeof query.flags !== 'undefined') { - query.search = new RegExp(query.search, query.flags); - } - - return source.replace(query.search, query.replace); - } - - return source; + return processQuery(source, query); }; From 47b9740ce50be7bb67a3c92f9182f0e0086c33df Mon Sep 17 00:00:00 2001 From: sashless Date: Fri, 4 Dec 2015 15:17:52 +0100 Subject: [PATCH 7/7] update readme --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8b7a337..3a68e73 100644 --- a/README.md +++ b/README.md @@ -49,9 +49,8 @@ module.exports = { test: /fileInWhichJQueryIsUndefined\.js$/, loader: 'string-replace', query: { - search: 'jquery', - replace: 'window.$', - flags: 'i' + search: /jquery/i, + replace: 'window.$' } } ] @@ -72,7 +71,7 @@ module.exports = { test: /\.js$/, loader: 'string-replace', query: { - replace: [ + multiple: [ {search: 'framework', replace: 'flamewar'}, {search: 'ants', replace: 'super ants'}, ]