Version function does not work for JS files in Laravel Mix

Viewed 3531

I wrote the following code that combined CSS and JS files with the Webpack.mix.js and the correct result, but the version function does not work for the JS file! What can I do to get it right, and both CSS and JS become version?

webpack.mix.js

mix.setPublicPath('/')
    .copy('node_modules/bootstrap/dist/js/bootstrap.js', 'resources/js')
    .copy('node_modules/jquery/dist/jquery.js', 'resources/js')
    .scripts([
        'resources/js/bootstrap.js',
        'resources/js/jquery.js'
    ], 'resources/js/app.js');

mix.sass('resources/sass/app.scss', 'public/css')
    .styles([
        'public/css/app.css',
        'public/css/myCss.css'
    ], 'public/css/all.css')
    .js('resources/js/app.js', 'public/js')
    .version();

mix-manifest.json

{
    "/css/app.css": "/css/app.css?id=35978cdc6a38f7d64fbb",
    "/css/all.css": "/css/all.css?id=eda19119aa76ef087fde"
}

i added <script src="{{ mix('js/all.js') }}"></script> into my Blade file

1 Answers

Instead, build your webpack.mix.js configuration like so.

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css')
    .styles([
        'public/css/myCss.css'
    ], 'public/css/all.css')
    .version();

Make sure jQuery and Bootstrap are installed. You also may want to update Laravel Mix to the latest.

npm install bootstrap
npm install jquery
npm install laravel-mix

In /resources/js/bootstrap.js at the top, add or modify these lines. This loads jQuery and Bootstrap in your application.

window._ = require('lodash');

try {
    window.Popper = require('popper.js').default;
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
} catch (e) {}

In /resources/sass/app.scss:

@import 'variables';

// Bootstrap
@import '~bootstrap/scss/bootstrap';

Compile your assets.

npm run production

You now have two CSS file references, both at the top of your template Blade file.

<script src="{{ mix('css/app.css') }}"></script>
<script src="{{ mix('css/all.css') }}"></script>

At the bottom of the template, use app.js:

<script src="{{ mix('js/app.js') }}"></script>

Take note that your mix-manifest file now has this content.

{
    "/js/app.js": "/js/app.js?id=0b5e26cf3116bf443cac",
    "/css/app.css": "/css/app.css?id=a99cdff2ed9d413fcb8a",
    "/css/all.css": "/css/all.css?id=a99cdff2ed9d413fcb8a"
}
Related