How to make a custom npm command and use it in mix file How would I got about making a custom command? Basically I want to run npm run custom-command and do something in my mix file. Is this possible? If so, where could I find some documentation on it?
Yes, add it to the "scripts" section of package.json
"scripts": {
"dev": "NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --watch-poll --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"hot": "NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"production": "NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"yourCustomScript": //your stuff here
},
npm run yourCustomScript
How would i go about making that to where i could call a function. For instance production has an inProduction() function. So if inCustom do something.
"scripts": {
// ... the rest of the existing scripts ...
"yourScript": "node scripts/custom_script.js"
},
in which the custom_script.js or whatever you name it, would contain the Mix commands of your choosing. If you also want production/development:
"scripts": {
// ... the rest of the existing scripts ...
"yourScript": "NODE_ENV=development node scripts/custom_script.js",
"yourScriptProd": "NODE_ENV=production node scripts/custom_script.js",
},
So how would I check for the NODE_ENV=production flag in my webpack.mix.js file?
if (mix.inProduction()) {
// do your stuff
}
Ok, but what if I have 2 production tasks, or should I? I guess I could just make a custom command and use the production env variable.
I think I am just overthinking this. Thanks for the help guys. I really appreciate it.
if (mix.inProduction()) {
mix.task1(); //custom command
mix.task2(); //custom command
mix.version(); //version the assets
mix.minify(); //minify the assets
// etc
}
I'd recommend watching this series:
https://laracasts.com/series/learn-laravel-mix
Please sign in or create an account to participate in this conversation.