Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

audunru's avatar

Run php artisan command before mix build

I have a couple of php artisan commands that I'd like to run before my javascript is compiled. So I learned about then(), but that happens after the build. Any suggestions?

Here's my webpack.mix.js

const mix = require('laravel-mix');
const { exec } = require('child_process');

mix
   // Kind of like this, but exactly the opposite...
    .then(() => {
        exec('php artisan routes:javascript');
        exec('php artisan lang:js --no-lib resources/assets/js/translations.js');
    })
    .sass('resources/assets/sass/app.scss', 'public/css')
    .react('resources/assets/js/app.js', 'public/js')
    .browserSync({
        proxy: 'localhost:8000',
    });

if (mix.inProduction()) {
    mix.version();
}
0 likes
4 replies
audunru's avatar
audunru
OP
Best Answer
Level 4

And the other command I was able to use WebpackShellPlugin. Here's what I ended up with:

const mix = require('laravel-mix');
const WebpackShellPlugin = require('webpack-shell-plugin');

mix.webpackConfig({
    plugins: [
        new WebpackShellPlugin({ onBuildStart: ['php artisan routes:javascript'], onBuildEnd: [] }),
    ],
    module: {
        rules: [
            {
                // Matches all PHP or JSON files in `resources/lang` directory.
                test: /resources(\|\/)lang.+\.(php|json)$/,
                loader: 'laravel-localization-loader',
            },
        ],
    },
});

mix.sass('resources/assets/sass/app.scss', 'public/css')
    .react('resources/assets/js/app.js', 'public/js')
    .browserSync({
        proxy: 'localhost:8000',
    });

if (mix.inProduction()) {
    mix.version();
}
zshan63's avatar

Hi , @audunru your solution is help full i've a question.

How to run command like this?

php artisan vendor:publish --provider="Nero\Ecomm\Providers\EcommServiceProvider" --tag="nero_public"

Simple command is working like this.

php artisan vendor:publish

audunru's avatar

Hi @zshan63 sorry for the late reply.

Generally I think php artisan vendor:publish is most commonly used to copy eg. configuration files so that they will become part of your codebase. You typically run the command once, make necessary changes to the files that have been published, and then commit the files to your version control system (eg. Git).

Therefore I can't really see why you would want to include that command in your webpack.mix.js

My original question concerns running shell commands that need to be run ever ytime something changes, specifically generating JS language files when the Laravel translation files change.

Please or to participate in this conversation.