So I have a JS file that I want to mix and copy and compress using Laravel mix
; my webpack.mix.js
file looks like so:
JavaScript
x
6
1
const mix = require('laravel-mix');
2
3
mix.js('resources/js/foo/foo.js', 'public/foo.js');
4
mix.copy('public/foo.js', 'public/js/foo.js');
5
mix.version();
6
The thing is that foo.js
has a variable called env
, set to dev
on local envs, and we set it to app
on production.
Is there a way I can automate this? For example, can I pass the variable according to the env
while mixing the file and set this?
Advertisement
Answer
https://laravel-mix.com/extensions/string-replace
npm i laravel-mix-string-replace
to install the package
This is how I solved it in mix file:
JavaScript
1
10
10
1
const pixelEnv = process.env.APP_ENV == "local" ? 'dev' : 'app';
2
mix.js('resources/js/foo/foo.js', 'public/foo.js').stringReplace({
3
test: /foo.js/,
4
loader: 'string-replace-loader',
5
options: {
6
search: 'PIXEL_ENV',
7
replace: pixelEnv,
8
}
9
});
10