README
Example work in Vue.
webpack.config.js
module.exports = {
...,
module: {
rules: [{
test: /\.vue$/,
use: {
loader: 'xh-regex-replace-loader',
options: {
enable: process.env.NODE_ENV === 'production',
regex: /href=["|'](.+?).html["|']/, // can also be a RegExp object (required)
flags: 'gi' // ignored if a RegExp is used (optional)
value: function (match) {
// modify all href="/xxx.html" to href="/xxx"
return match[0].replace('.html', '')
},
'vue-loader',
}
}
}]
}
}
Modify from James Abney
regex-replace-loader https://github.com/jabney/regex-replace-loader
regex-replace-loader
A webpack loader that uses regex to replace values in files, or transform source into another form.
The regex-replace-loader
takes a file's content as input, runs it against a user-supplied regular expression, and makes substitutions based on the user-supplied replace value
, which can be either a string or a function that returns a value.
Example usage
Replace a specific string in a file with another.
Input
All must depart the auditorium.
All must exit through the side door.
webpack.config.js
module.exports = {
...,
module: {
rules: [{
test: /\.source.txt$/,
use: {
loader: 'regex-replace-loader',
options: {
regex: 'All', // can also be a RegExp object (required)
flags: 'g' // ignored if a RegExp is used (optional)
value: 'y\'all', // the replace value (required, can also be a function)
}
}
}]
}
}
Output
y'all must depart the auditorium.
y'all must exit through the side door.
Multiple replace stages
The regex-replace-loader
supports running multiple replace operations in stages, where the output of each stage is the input source for the next.
Input
Today's date is THE_DATE
The time is THE_TIME
webpack.config.js
module.exports = {
...,
module: {
rules: [{
test: /\.source.txt$/,
use: {
loader: 'regex-replace-loader',
options: {
stages: [{
regex: 'THE_DATE',
flags: '',
value: new Date().toDateString(),
},
{
regex: 'THE_TIME',
flags: '',
value: new Date().toTimeString(),
}]
}
}
}]
}
}
Output
Today's date is Sat Dec 09 2017
The time is 10:14:52 GMT-0800 (PST)
Typescript
Using import
instead of require
may cause issues when using Typescript to import text files. In this case, include a declarations.d.ts
file in your project:
declarations.d.ts
declare module '*.txt' {
const txt: any
export default txt
}
declare module '*.json' {
const json: any
export default json
}
declare module '*.whatever' {
const value: any
export default value
}
Then you should be able to import the file:
import text from './somefile.txt'
import json from './someinfo.json'
Options object
options: {
regex: '<search expression>' | /search expression/<flags>,
flags: 'g', // Ignored if regex is a RegExp object
value: '<replace value>' | function (match) { return 'some value' },
stages: [ {options}, {options}, ... ]
}
regex (string|RegExp)
(required) can be a string or RegExp object. For strings make sure escape characters use a double backslash, e.g., \\w+
.
flags (string)
(optional) used if regex
is a string, otherwise ignored. If g
(global) is specified either in the flags
property or in the supplied regex
, a replace operations will be performed for each match in the source.
value (string|function)
(required) the replace value.
stages (object)
(optional) a list of regex
, flags
and value
objects for performing multiple match/replace operations on the same source:
stages: [
{ regex: 'a', flags: 'gi', value: '1' },
{ regex: 'b', flags: 'gi', value: '2' },
{ regex: 'c', flags: 'gi', value: '3' }
]
value
option
Using the The options.value
parameter can be a string
or function
. While using a function is more flexible and powerful, there are some special uses when options.value
is a string.
as a string value
When options.value
is a string, certain special replacement patterns are available.
Pattern | Inserts |
---|---|
$ | Inserts a " |