Skip to content

Commit 76405ad

Browse files
author
Val
committed
npmignore update. query params replacement. readme
1 parent a533f79 commit 76405ad

File tree

5 files changed

+89
-18
lines changed

5 files changed

+89
-18
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
*.log
44
*.swp
55
node_modules/
6+
test/

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Replace loader for [Webpack](http://webpack.github.io/)
2+
3+
Perform replacements (plain and regular expression) in the contents loaded by the loader.
4+
5+
## Install:
6+
7+
```bash
8+
$ npm install --save-dev replace-loader
9+
```
10+
11+
## Usage:
12+
13+
### Plain replacement:
14+
15+
In your `webpack.config.js`:
16+
17+
```javascript
18+
module.exports = {
19+
// ...
20+
module: {
21+
loaders: [
22+
{
23+
test: /fileInWhichJQueryIsUndefined\.js$/,
24+
loader: 'replace',
25+
query: {
26+
search: 'jQuery',
27+
replace: 'window.$'
28+
}
29+
}
30+
]
31+
}
32+
}
33+
```
34+
35+
### Regex replacement:
36+
37+
To achieve regular expression replacement you should specify the `flags` query param
38+
(as an empty string if you do not want any flags). In this case, `search` and `flags` are being
39+
passed to the [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor.
40+
41+
In your `webpack.config.js`:
42+
43+
```javascript
44+
module.exports = {
45+
// ...
46+
module: {
47+
loaders: [
48+
{
49+
test: /fileInWhichJQueryIsUndefined\.js$/,
50+
loader: 'replace',
51+
query: {
52+
search: 'jquery',
53+
replace: 'window.$',
54+
flags: 'i'
55+
}
56+
}
57+
]
58+
}
59+
}
60+
```
61+
62+
## Contributing:
63+
64+
Feel free to open issues to propose stuff and participate. Pull requests are also welcome.
65+
66+
## Licence:
67+
68+
[MIT](http://en.wikipedia.org/wiki/MIT_License)

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ var utils = require('loader-utils');
33
module.exports = function (source) {
44
var query = utils.parseQuery(this.query);
55

6-
if (typeof query.subject !== 'undefined' && typeof query.replacement !== 'undefined') {
6+
if (typeof query.search !== 'undefined' && typeof query.replace !== 'undefined') {
77
if (typeof query.flags !== 'undefined') {
8-
query.subject = new RegExp(query.subject, query.flags);
8+
query.search = new RegExp(query.search, query.flags);
99
}
1010

11-
source = source.replace(query.subject, query.replacement);
11+
source = source.replace(query.search, query.replace);
1212
}
1313

1414
return source;

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "replace-loader",
3-
"version": "0.0.1",
4-
"description": "",
3+
"version": "0.1.0",
4+
"description": "Replace loader for Webpack",
55
"keywords": [
66
"webpack",
7-
"webpack-loader"
7+
"loader",
8+
"webpack-loader",
9+
"replace"
810
],
911
"scripts": {
1012
"test": "mocha"

test/index.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var outputFileName = 'build.js';
99
var outputFilePath = path.join(outputDirPath, outputFileName);
1010

1111
describe('Webpack replace loader ...', function () {
12-
it('should replace with string subject', function (done) {
12+
it('should replace with string search', function (done) {
1313
webpack(
1414
{
1515
entry: entryFilePath,
@@ -23,8 +23,8 @@ describe('Webpack replace loader ...', function () {
2323
test: /\.js$/,
2424
loader: '__this',
2525
query: {
26-
subject: 'var value',
27-
replacement: 'var a'
26+
search: 'var value',
27+
replace: 'var a'
2828
}
2929
}
3030
]
@@ -44,7 +44,7 @@ describe('Webpack replace loader ...', function () {
4444
);
4545
});
4646

47-
it('should replace with pattern subject', function (done) {
47+
it('should replace with pattern search', function (done) {
4848
webpack(
4949
{
5050
entry: entryFilePath,
@@ -58,8 +58,8 @@ describe('Webpack replace loader ...', function () {
5858
test: /\.js$/,
5959
loader: '__this',
6060
query: {
61-
subject: 'var VALUE = \'\.*\'',
62-
replacement: 'var a = \'\'',
61+
search: 'var VALUE = \'\.*\'',
62+
replace: 'var a = \'\'',
6363
flags: 'i'
6464
}
6565
}
@@ -94,16 +94,16 @@ describe('Webpack replace loader ...', function () {
9494
test: /\.js$/,
9595
loader: '__this',
9696
query: {
97-
subject: 'var value',
98-
replacement: 'var a'
97+
search: 'var value',
98+
replace: 'var a'
9999
}
100100
},
101101
{
102102
test: /bar\.js$/,
103103
loader: '__this',
104104
query: {
105-
subject: 'var value',
106-
replacement: 'var bar'
105+
search: 'var value',
106+
replace: 'var bar'
107107
}
108108
}
109109
]
@@ -137,8 +137,8 @@ describe('Webpack replace loader ...', function () {
137137
{
138138
test: /\.js$/,
139139
loaders: [
140-
'__this?subject=var value&replacement=var a',
141-
'__this?subject=module.exports = value&replacement=module.exports = a'
140+
'__this?search=var value&replace=var a',
141+
'__this?search=module.exports = value&replace=module.exports = a'
142142
]
143143
}
144144
]

0 commit comments

Comments
 (0)