This guide explains the provided Laravel Mix Webpack configuration file in a detailed, step-by-step manner.
Laravel Mix provides a clean and fluent API for defining Webpack build steps for your Laravel applications. By default, it compiles CSS and bundles JavaScript files.
const mix = require('laravel-mix');
const webpack = require('webpack');
const { VueLoaderPlugin } = require('vue-loader');
Here, we import the necessary modules: laravel-mix
for Mix, webpack
for Webpack configurations, and VueLoaderPlugin
for Vue.js support.
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/
This comment block explains the purpose of Laravel Mix and its usage in asset management.
mix.js('resources/js/app.js', 'public/js')
.setResourceRoot('../')
.js('resources/js/jquery-comments.js', 'public/js')
.js('resources/js/internal.js', 'public/js')
.js('resources/js/frontend/app.js', 'js/frontend.js')
.js('resources/js/backend/app.js', 'js/backend.js')
.styles('resources/css/jquery-comments.css', 'public/css/jquery-comments.css')
.styles('resources/css/design.css', 'public/css/design.css')
.sass('resources/sass/frontend/app.scss', 'css/frontend.css')
.sass('resources/sass/backend/app.scss', 'css/backend.css')
.sass('resources/sass/app.scss', 'css/app.css')
.extract([
'alpinejs',
'axios',
'sweetalert2',
'lodash'
])
.sourceMaps()
.postCss('resources/css/app.css', 'public/css', [
//
])
.vue();
This section configures the compilation of various JavaScript and CSS files. It includes:
mix.autoload({
jquery: ['$', 'window.jQuery', 'jQuery'],
});
This configures autoloading for jQuery, making it globally available in the project.
mix.webpackConfig({
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
})
],
resolve: {
extensions: ['.js', '.jsx', '.vue', '.json']
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
},
});
This section extends the default Webpack configuration with custom settings:
ProvidePlugin
makes jQuery available globally.resolve
specifies file extensions to resolve.module.rules
defines loaders for specific file types, such as vue-loader
for .vue files.
if (mix.inProduction()) {
mix.version();
mix.webpackConfig({
optimization: {
minimize: true,
},
devtool: false
});
} else {
mix.webpackConfig({
devtool: 'inline-source-map'
});
}
This conditional block sets different configurations for production and development environments:
This guide provides a detailed explanation of the Laravel Mix Webpack configuration file, covering each part of the setup for JavaScript, CSS, and Vue.js support. Understanding these steps helps in efficiently managing and compiling assets in Laravel projects.
Published By: Krishanu Jadiya
Updated at: 2024-07-20 00:15:08