jesders88's avatar

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?

0 likes
7 replies
Cronix's avatar

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

jesders88's avatar

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.

arukomp's avatar
"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",
},
jesders88's avatar

So how would I check for the NODE_ENV=production flag in my webpack.mix.js file?

Cronix's avatar
if (mix.inProduction()) {
    // do your stuff
}
jesders88's avatar

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.

Please or to participate in this conversation.