1

Is there any way to pass an argument to the string-replace-loader ?

I try to achieve something like that -

Write in the shell - webpack arg1=HelloWorld

In the webpack.config.js -

var value = arg1 // get it from the shell

{
    test: /testFile\.js$/,
    loader: 'string-replace-loader',
    options: {
        search: '$',
        replace: value,
    }
}

And $ in testFile.js would become to be "HellowWorld".

How Could I get that ?

1
  • inside webpack config module.exports = function(env, argv) {.., try argv.arg1 || 'defaultValue' (or env.arg1) Commented Jun 11, 2018 at 0:34

1 Answer 1

1

You could use argv or env, let's assume you would like to achieve something by running webpack --env.text=test, then after executing this in your console, you can access environment variable inside your javascript code, so while you are in testFile.js you can just write:

console.log(process.env.text); //returns 'test' and from here you can do whatever you want with that value, perhaps replace $ sign with it, or maybe directly printing it somewhere.

More info here: https://webpack.js.org/guides/environment-variables/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.