You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+86-7
Original file line number
Diff line number
Diff line change
@@ -8,8 +8,9 @@ file-replace-loader is webpack loader that allows you replace files in compile t
8
8
## Features
9
9
10
10
* Compatibility with webpack 3.x, 4.x;
11
-
*Supports watch webpack mode;
11
+
*Support watch webpack mode;
12
12
* Replace files in compile time without change source files;
13
+
* Multiple replacement;
13
14
* Sync and async modes;
14
15
* Compatibility with other loaders;
15
16
* Support binary files.
@@ -43,11 +44,86 @@ module.exports = {
43
44
}
44
45
```
45
46
46
-
This example rule will replace all of imports `/\.config.js$/` to `config.local.js` file, <br/>if replacement exists (condition `if-replacement-exists`).
47
+
This example rule replaces all of imports `/\.config.js$/` to `config.local.js` file, <br/>if replacement exists (condition `if-replacement-exists`).
47
48
48
-
After example build in bundle file will some code from `config.local.js` and original sources
49
+
After this build a bundle file will contain code from `config.local.js` and original sources
49
50
won't changed.
50
51
52
+
## Multiple replace
53
+
54
+
To describe replace rules for two or more files you can use function as replacement value.<br/>
55
+
56
+
How does it work?
57
+
1. Webpack runs file-replace-loader according to `test` rule, `include` and `exclude` rule options;
58
+
2. file-replace-loader looks on `replacement` option. If it is string then the loader just replace a file. If it is a function
59
+
then file-replace-loader checking what it returns. If the function returns a path to file then the loader
60
+
replaces, if returns nothing then current match skips.
61
+
3. If `replacement` function returns a path then file-replace-loader looks to `condition`. If condition is `always` then it replace every match. If `condition` is
62
+
`if-replacement-exists` then loader checking existing file, etc;
63
+
64
+
For example:
65
+
66
+
```javascript
67
+
const { resolve } =require('path');
68
+
69
+
module.exports= {
70
+
//...
71
+
module: {
72
+
rules: [{
73
+
test:/\.js$/,
74
+
loader:'file-replace-loader',
75
+
options: {
76
+
condition:'always', // <-- Note that the rule applies for all files!
77
+
replacement(resourcePath) {
78
+
if (resourcePath.endsWith('foo.js')) {
79
+
returnresolve('./bar.js');
80
+
}
81
+
if (resourcePath.endsWith('foo-a.js')) {
82
+
returnresolve('./bar-a.js');
83
+
}
84
+
},
85
+
async:true,
86
+
}
87
+
}]
88
+
}
89
+
}
90
+
```
91
+
92
+
file-replace-loader passes to `replacement` function `resourcePath` for every matching.
93
+
file-replace-loader doesn't care what developer does with this path but if `repalcement` function returns a new path then file-replace-loader replaces file.
94
+
If `replacement` function returns nothing then file-replace-loading skip replace for current `resourcePath`.
95
+
96
+
Example with mapping:
97
+
98
+
```javascript
99
+
const { resolve } =require('path');
100
+
101
+
module.exports= {
102
+
//...
103
+
module: {
104
+
rules: [{
105
+
test:/\.js$/,
106
+
loader:'file-replace-loader',
107
+
options: {
108
+
condition:'always', // <-- Note that the rule applies for all files! But you can use other conditions too
varHELP_INFO_MESSAGE=`If you are experiencing difficulties to solve this problem please create an issue on ${packageJson.bugs.url}`;
62
65
varERROR_MESSAGES=[];
63
66
exports.ERROR_MESSAGES=ERROR_MESSAGES;
64
-
ERROR_MESSAGES[0]=`File ($1) doesn't exist but specified in ${LOADER_NAME} options with \n`+` condition ${LOADER_REPLACEMENT_CONDITIONS[1]} or '${LOADER_REPLACEMENT_CONDITIONS[2]}'. \n`+` Perhaps this is due replacement isn't full path. Make sure that file exists and replacement\n`+` option is full path to file.\n`+` If you are experiencing difficulties to solve this problem, you can create an issue on ${packageJson.bugs.url}`;
65
-
ERROR_MESSAGES[1]=`File ($1) doesn't exist but specified in replacement. ${LOADER_NAME} can't replace\n`+` it by '${LOADER_REPLACEMENT_CONDITIONS[5]}' condition. Make sure that replacement file exists. \n`+` If you are experiencing difficulties to solve this problem, you can create an issue on ${packageJson.bugs.url}`;
66
-
ERROR_MESSAGES[2]=`should be equal to one of the allowed values: [$1]. \n`+` If you are experiencing difficulties to solve this problem, you can create an issue on ${packageJson.bugs.url}`;
67
-
ERROR_MESSAGES[3]=`${LOADER_NAME} must executes before other loaders. Check your Webpack config file.\n`+` NOTE: Webpack reads loaders from right to left. So ${LOADER_NAME} have to be the last in array of loaders. \n`+` If you are experiencing difficulties to solve this problem, you can create an issue on ${packageJson.bugs.url}`;
67
+
ERROR_MESSAGES[0]=`File ($1) doesn't exist but specified in ${LOADER_NAME} options with \n`+` condition ${LOADER_REPLACEMENT_CONDITIONS[1]} or '${LOADER_REPLACEMENT_CONDITIONS[2]}'. \n`+` Perhaps this is due replacement isn't full path. Make sure that file exists and replacement\n`+` option is full path to file.\n`+` ${HELP_INFO_MESSAGE}`;
68
+
ERROR_MESSAGES[1]=`File ($1) doesn't exist but specified in replacement. ${LOADER_NAME} can't replace\n`+` it by '${LOADER_REPLACEMENT_CONDITIONS[5]}' condition. Make sure that replacement file exists. \n`+` ${HELP_INFO_MESSAGE}`;
69
+
ERROR_MESSAGES[2]=`should be equal to one of the allowed values: [$1]. \n`+` ${HELP_INFO_MESSAGE}`;
70
+
ERROR_MESSAGES[3]=`${LOADER_NAME} must executes before other loaders. Check your Webpack config file.\n`+` NOTE: Webpack reads loaders from right to left. So ${LOADER_NAME} have to be the last in array of loaders. \n`+` ${HELP_INFO_MESSAGE}`;
71
+
ERROR_MESSAGES[4]=`should be full path to file or function returning full path to file. \n`+` ${HELP_INFO_MESSAGE}`;
0 commit comments