Skip to content

Commit 1d9f574

Browse files
authored
Merge pull request #7 from vyushin/0.1.3
Release 0.1.3
2 parents 8f3e424 + c5dc270 commit 1d9f574

File tree

12 files changed

+127
-52
lines changed

12 files changed

+127
-52
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
file-replace-loader is webpack loader that allows you replace files in compile time by some condition.
77

8-
### Features
8+
## Features
99

1010
* Compatibility with webpack 3.x, 4.x
1111
* Replaces files which importing in compile time
1212
* Sync and Async modes
1313
* Replaces files only in compile time, without changes source files
1414
* Compatibility with other loaders
15+
* Support binary files
1516

1617
## Usage
1718

@@ -41,6 +42,37 @@ This example rule will replace all of imports `/\.config.js$/` to `config.local.
4142
After example build in bundle file will be some code from `config.local.js` and original sources
4243
won't changed.
4344

45+
## Using with binary files
46+
47+
File replace loader allows replace binary files. <br/>For example:
48+
49+
```javascript
50+
//webpack.config.js
51+
52+
const { resolve } = require('path');
53+
54+
module.exports = {
55+
//...
56+
module: {
57+
rules: [{
58+
test: /\.png$/,
59+
use: [{
60+
loader: 'file-loader',
61+
options: {
62+
name: '[name].[ext]',
63+
},
64+
}, {
65+
loader: 'file-replace-loader',
66+
options: {
67+
condition: 'if-replacement-exists',
68+
replacement: resolve('./src/replacement.png')
69+
}
70+
}]
71+
}]
72+
}
73+
}
74+
```
75+
4476
## Using with other loaders
4577

4678
File replace loader must executes before other loaders. It means that in webpack config file the loader must be last in list. <br/>For example:
@@ -121,10 +153,24 @@ cd ./example/dist
121153
node ./script.js
122154
```
123155

156+
That should be message in terminal:
157+
158+
```bash
159+
Message from replacement.js
160+
```
161+
162+
This means that file replace loader works.
163+
164+
In `example/src` folder you can see example source code. Let's see on file `replacement.js`.
165+
This is file containing message above. File replace loader replaced `example/src/source.js` to `example/src/replacement.js` then webpack copied files to `example/dist` folder.
166+
You can open `example/webpack.config.js` and see how file replace loader did it.
167+
168+
After each change `example/webpack.config.js` you should run `npm run build_example_wp_4x` or `npm run build_example_wp_3x`.
169+
124170
## Installation
125171

126172
###### NPM
127-
`npm i -D file-replace-loader`
173+
`npm install --save-dev file-replace-loader`
128174

129175
## License
130176
[MIT LICENSE](https://github.com/vyushin/file-replace-loader/blob/master/LICENSE)

dist/constants.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.IS_DEBUG_MODE = exports.IS_PROGRESS_MODE = exports.ERROR_MESSAGES = exports.ERROR_TYPES = exports.LOADER_OPTIONS_SCHEMA = exports.LOADER_REPLACEMENT_CONDITIONS = exports.MAIN_LOADER_FILE = exports.LOADER_NAME = exports.ENCODING = undefined;
6+
exports.IS_DEBUG_MODE = exports.IS_PROGRESS_MODE = exports.ERROR_MESSAGES = exports.ERROR_TYPES = exports.LOADER_OPTIONS_SCHEMA = exports.LOADER_REPLACEMENT_CONDITIONS = exports.MAIN_LOADER_FILE = exports.LOADER_NAME = undefined;
77

88
var _path = require('path');
99

@@ -14,20 +14,14 @@ var packageJson = _interopRequireWildcard(_package);
1414
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
1515

1616
/**
17-
* Usually used in read file functions
17+
* Loader name
1818
* @const
1919
*/
2020
/**
2121
* Constants of file replace loader
2222
* {@link https://github.com/vyushin/file-replace-loader/blob/master/src/constants.js}
2323
*/
2424

25-
var ENCODING = 'utf8';
26-
27-
/**
28-
* Loader name
29-
* @const
30-
*/
3125
var LOADER_NAME = 'file-replace-loader';
3226

3327
/**
@@ -113,7 +107,6 @@ var IS_DEBUG_MODE = (process.argv || []).some(function (arg) {
113107
return arg === '--debug';
114108
});
115109

116-
exports.ENCODING = ENCODING;
117110
exports.LOADER_NAME = LOADER_NAME;
118111
exports.MAIN_LOADER_FILE = MAIN_LOADER_FILE;
119112
exports.LOADER_REPLACEMENT_CONDITIONS = LOADER_REPLACEMENT_CONDITIONS;

dist/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6+
exports.raw = undefined;
67

78
exports.default = function (source) {
89
var options = getOptions(this);
@@ -65,11 +66,9 @@ exports.default = function (source) {
6566
return isAsync ? readFile(options.replacement, true, function (content) {
6667
callback(null, content);
6768
}) : readFile(options.replacement, false);
69+
} else {
70+
return isAsync ? callback(null, source) : source;
6871
}
69-
/**
70-
* We don't need any errors here, because it isn't error when replacement doesn't exist by
71-
* condition 'if-replacement-exists'
72-
*/
7372
}
7473

7574
/**
@@ -175,15 +174,15 @@ var progress = function () {
175174

176175
function readFile(path, isAsync, callback) {
177176
if (isAsync) {
178-
return (0, _fs.readFile)(path, _constants.ENCODING, function (err, content) {
177+
return (0, _fs.readFile)(path, null, function (err, content) {
179178
err && new Exception({
180179
title: _constants.ERROR_TYPES[2],
181180
message: err.message
182181
});
183182
callback(content);
184183
});
185184
} else {
186-
return (0, _fs.readFileSync)(path, { encoding: _constants.ENCODING, flag: 'r' });
185+
return (0, _fs.readFileSync)(path, { flag: 'r' });
187186
}
188187
}
189188

@@ -218,6 +217,16 @@ function condition(condition) {
218217
return new Proof(condition);
219218
}
220219

220+
/** Enable raw input from webpack
221+
*
222+
* This asks webpack to provide us a Buffer instead of a String.
223+
*
224+
* We need this to avoid corrupting binary files when returning
225+
* the input unmodified.
226+
*
227+
*/
228+
var raw = exports.raw = true;
229+
221230
/**
222231
* File Replace Loader function
223232
*/

example/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

example/src/replacement.png

530 Bytes
Loading

example/src/source.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(`Message from source.js`);

example/src/source.png

553 Bytes
Loading

example/webpack.config.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,51 @@ const { resolve } = require('path');
33
module.exports = {
44

55
output: {
6-
filename: 'script.js',
6+
filename: '[name].js',
77
path: resolve('./dist'),
88
},
99

1010
entry: {
11-
script: resolve('./index.js'),
11+
script: resolve('./src/source.js'),
12+
image: resolve('./src/source.png')
1213
},
1314

1415
module: {
15-
rules: [{
16-
test: /\.js$/,
17-
use: [{
18-
loader: 'babel-loader',
19-
}, {
20-
loader: 'file-replace-loader',
21-
options: {
22-
condition: 'if-source-is-empty',
23-
replacement: resolve('./replacement.js')
24-
}
25-
}]
26-
}]
16+
rules: [
17+
/**
18+
* Using file replace loader with text files (for example .js)
19+
*/
20+
{
21+
test: /\.js$/,
22+
use: [{
23+
loader: 'babel-loader',
24+
}, {
25+
loader: 'file-replace-loader',
26+
options: {
27+
condition: 'if-replacement-exists',
28+
replacement: resolve('./src/replacement.js')
29+
}
30+
}]
31+
},
32+
/**
33+
* Using file replace loader with binary files (for example .png)
34+
*/
35+
{
36+
test: /\.png$/,
37+
use: [{
38+
loader: 'file-loader',
39+
options: {
40+
name: '[name].[ext]',
41+
},
42+
}, {
43+
loader: 'file-replace-loader',
44+
options: {
45+
condition: 'if-replacement-exists',
46+
replacement: resolve('./src/replacement.png')
47+
}
48+
}]
49+
}
50+
]
2751
},
2852
resolveLoader: {
2953
modules: ['node_modules', resolve('../../')]

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "file-replace-loader",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "file-replace-loader is webpack loader that allows you replace files in compile time",
5-
"author": "Evgeny Vyushin <[email protected]> (https://www.vyushin.ru/)",
5+
"author": "Evgeny Vyushin <[email protected]> (https://github.com/vyushin)",
66
"contributors": [
7-
"Evgeny Vyushin <[email protected]> (https://www.vyushin.ru/)"
7+
"Evgeny Vyushin <[email protected]> (https://github.com/vyushin)",
8+
"Mickaël Thomas (https://github.com/mickael9)"
89
],
910
"maintainers": [
10-
"Evgeny Vyushin <[email protected]> (https://www.vyushin.ru/)"
11+
"Evgeny Vyushin <[email protected]> (https://github.com/vyushin)"
1112
],
1213
"repository": {
1314
"type": "git",
@@ -33,8 +34,9 @@
3334
},
3435
"devDependencies": {
3536
"babel-core": "^6.26.3",
36-
"babel-preset-env": "^1.7.0",
3737
"babel-loader": "^7.1.4",
38+
"babel-preset-env": "^1.7.0",
39+
"file-loader": "^4.0.0",
3840
"webpack": "^4.12.0",
3941
"webpack-cli": "^3.0.8"
4042
},

0 commit comments

Comments
 (0)