Skip to content

wac925/laravel-bundler

Repository files navigation

Laravel Bundler

downloads npm-version github-tag license tests install-size

Modern and fast asset building tool for Laravel framework with better defaults.

Installation

💡 This package does not work with laravel-mix; you must remove laravel-mix before using this one

# yarn
yarn add --dev laravel-bundler

# npm
npm install --save-dev laravel-bundler 

# pnpm
pnpm add --save-dev laravel-bundler

Usage

Create a resources/js/app.js file like

// Import libraries, eg: Vue.js v2 with bootstrap-vue
import 'vue';
import {BootstrapVue} from 'bootstrap-vue'
// You can import styles like this
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

const LazyLoadedComponent = () => import('./HeavyComponent.vue')

Vue.use(BootstrapVue);

new Vue({
    el: "#app",
    components: {
        LazyLoadedComponent,
    }
})

Create a webpack.config.js file on your project root and remove webpack.mix.js if exists.

const webpack = require('webpack');

module.exports = require('laravel-bundler')({
        entry: {
            app: './resources/js/app.js',
        },
        plugins: [
            //
        ],
        // Other webpack configs may go here
    },
    // Include vue v2 recipe
    // Dont forget to install required packages by this recipe
    require('laravel-bundler/src/recipes/vue-2.js')
);

Update your package.json file

{
    "scripts": {
        "dev": "webpack --node-env=development --progress",
        "watch": "webpack watch --node-env=development --progress",
        "hot": "webpack serve --node-env=development --progress --hot",
        "prod": "webpack --node-env=production --progress"
    },
    "browserslist": [
        "> 2%",
        "not dead"
    ],
    "babel": {
        "presets": [
            [
                "@babel/preset-env",
                {
                    "bugfixes": true
                }
            ]
        ],
        "plugins": []
    }
}

Update your blade template

<!-- header -->
<link href="{{ mix('css/app.css', 'dist') }}" rel="stylesheet">

<!-- footer -->
<script src="{{ mix('js/manifest.js', 'dist') }}"></script>
<script src="{{ mix('js/vendor.js', 'dist') }}"></script>
<script src="{{ mix('js/app.js', 'dist') }}"></script>

Update your .gitignore file

/public/dist

Features

  • Webpack 5 and Babel 7 with @babel/preset-env
  • Use esbuild to minify CSS and JS 🚀
  • Vue.js v2 support - Recipe
  • Vue.js v3 support - Recipe
  • CSS and SASS|SCSS support
  • PostCSS loader pre-configured with autoprefixer
  • Font and image files handling
  • Full HMR support for Vue, even for CSS 🔥
  • Extract all css to a separate file (based on entry)
  • Accepts css/scss file as entry
  • Extract all vendor js to a separate file
  • Dynamic import (code splitting) support
  • Clean the output directory before emitting the assets
  • mix-manifest.json compatible with Laravel's mix() helper
  • Load environment variables from .env file that are prefixed with MIX_ 😉
  • Intelligent SourceMap based on mode
  • Can auto-reload web-browser when blade templates gets changed 😉

Documentation

Not in the plan

These features are not in the plan but can be enabled on demand 🤷‍♂️

License

MIT License

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published