Laravel Mix Serve
This package extends Laravel Mix to run the php artisan serve
command by default, or any custom command, within the same shell after the mix command has been executed.
Table of Contents
Usage
- Install the package with npm or yarn:
npm install laravel-mix-serve --save-dev
yarn add laravel-mix-serve --dev
- Require the extension in your Mix configuration:
const mix = require('laravel-mix');
require('laravel-mix-serve');
- Enable the extension by calling
.serve()
:
mix.serve();
By default, the Laravel serve command
php artisan serve
will be executed.
And you're done!
Configuration
Defaults
mix.serve({
cmd: 'php',
args: ["artisan", "serve"],
verbose: true,
watch: true,
dev: true,
prod: false
})
The use of an Object, for the command (
cmd
) and arguments (args
), comes from the way that thespawn
method of thechild_process
module is used.
Commands
- String Commands
Instead of making changes directly to the Object, you can provide a string command. Configuration options can still be changed through the second parameter.
// String Command
mix.serve('php artisan serve');
//String Command with Any Config Options
mix.serve('php artisan serve', {
verbose: true,
watch: true,
dev: true,
prod: false
});
Since the configuration options passed to the second parameter would take precedence when assigned, the
cmd
andargs
properties within the second parameter Object are deleted to ensure that the string command is not overwritten.
- Custom Commands
Custom commands are the major caveat of this plugin, whether it is a custom Laravel or even non-Laravel command, in conjunction with the convenience of passing commands as strings.
// Custom Non-Laravel Command
mix.serve('npm run start');
// Custom Laravel Command
mix.serve('php artisan serve --port=8888');
Verbosity
You can choose whether you want the output from your server to show in the shell. By default, verbose is set to true
so that you can see any responses or errors from your server. Otherwise, you can turn this feature off by setting verbose to false
.
/* Options ( true | false | 'once' ) */
// Object
mix.serve({ verbose: false });
// with String Command
mix.serve('npm run start', { verbose: false });
For Laravel users, if you want to still be sure that your server is up while not losing the place of your compiled mix output, you can also use the option
'once'
.For example, if you only want the default Laravel output of:
Starting Laravel development server: http://127.0.0.1:8000
Watch (ver 2.1.0)
You can disable the command execution from running when watch
has been called. By default it is enabled, since that is the intention of this plugin, especially when working in conjunction with BrowserSync.
// Object
mix.serve({ watch: false });
// with String Command
mix.serve('npm run start', { watch: false });
Development (ver 2.1.0)
You can disable the command execution from running in development mode when watch
has not been called.
// Object
mix.serve({ dev: false });
// with String Command
mix.serve('npm run start', { dev: false });
To be specific, the logic by which development mode is determined, is when
mix.inProduction()
returnsfalse
.
Production (ver 2.1.0)
You can enable the command execution to run in production mode. By default, it is disabled since I have yet to determine the use case for this feature. It exists since the barebones came from the development of the dev
option.
// Object
mix.serve({ prod: true });
// with String Command
mix.serve('npm run start', { prod: true });
Hook (ver 2.2.0)
You can choose on which compiler hook your command should run. By default, the command will run on the afterCompile
hook.
// Object
mix.serve({ hook: 'beforeCompile' });
// with String Command
mix.serve('npm run start', { hook: 'beforeCompile' });
You can see all of the options available on the Webpack website.
Credits
Thank you Jeffrey, Laracasts built the foundation of my coding knowledge and techniques. There is such beauty to clean and readable code.
Laravel Mix has been godsend, even outside of the Laravel ecosystem. I have used it to set up quick template projects and it has saved me so much time.
This Laravel Mix Plugin is a small dedication to both what I learned in Laracasts and the wonderful tool that is Laravel Mix.
License
The MIT License (MIT). Please see License File for more information.