Skip to content

feat: add replaceAll option #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/getOptionsArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const optionsSchema = {
}
]
},
replaceAll: {
type: 'boolean'
},
flags: {
type: 'string',
},
Expand All @@ -39,6 +42,7 @@ const optionsSchema = {
const defaultOptions = {
search: null,
replace: null,
replaceAll: false,
flags: null,
strict: false
}
Expand Down
4 changes: 3 additions & 1 deletion lib/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ function replace (source, options, context) {
throw new Error('Replace failed (strict mode) : options.search and options.replace are required')
}

const newSource = source.replace(search, replace)
const newSource = options.replaceAll
? source.replaceAll(search, replace)
: source.replace(search, replace)

if (strict && (newSource === source)) {
throw new Error('Replace failed (strict mode) : ' + options.search + ' → ' + options.replace)
Expand Down
49 changes: 49 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,53 @@ describe('Webpack replace loader ...', () => {
}
)
})

it('should replace only one occurrence', done => {
webpack(getTestWebPackConfig(
{
test: /\.js$/,
loader: '__this-loader',
options: {
search: 'abcd',
replace: 'efhg'
}
}),
(error, stats) => {
expect(error).to.equal(null)

fs.readFile(outputFilePath, 'utf8', (error, contents) => {
expect(error).to.equal(null)
expect(contents).to.be.a('string')
expect(contents.match(/abcd/g)).to.have.lengthOf(1);
expect(contents.match(/efhg/g)).to.have.lengthOf(1);
done()
})
}
)
})

it('should replace all occurrences', done => {
webpack(getTestWebPackConfig(
{
test: /\.js$/,
loader: '__this-loader',
options: {
search: 'abcd',
replace: 'efgh',
replaceAll: true
}
}),
(error, stats) => {
expect(error).to.equal(null)

fs.readFile(outputFilePath, 'utf8', (error, contents) => {
expect(error).to.equal(null)
expect(contents).to.be.a('string')
expect(contents).not.to.include('abcd')
expect(contents.match(/efgh/g)).to.have.lengthOf(2);
done()
})
}
)
})
})
5 changes: 5 additions & 0 deletions test/source/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ var bar = require('./bar');
var value = 'baz';

console.log(foo + bar + value);

var first = 'abcd';
var second = 'abcd'

console.log(first + second);