Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup external libraries with laravel mix

I need to use an external library on web pack with laravel-mix. On web pack I should do something like this as described in the webpack docs

{
    output: {
        // export itself to a global var
        libraryTarget: "var",
        // name of the global var: "Foo"
        library: "Foo"
    },
    externals: {
        // require("jquery") is external and available
        //  on the global var jQuery
        "jquery": "jQuery"
    }
}

But I can do this with laravel mix?

like image 207
Angus Simons Avatar asked May 01 '17 14:05

Angus Simons


People also ask

Can I use Laravel mix outside Laravel?

Yes, you can use Mix outside of Laravel; it is an NPM package after all.

WHAT IS MIX () in Laravel?

Laravel Mix, a package developed by Laracasts creator Jeffrey Way, provides a fluent API for defining webpack build steps for your Laravel application using several common CSS and JavaScript pre-processors. In other words, Mix makes it a cinch to compile and minify your application's CSS and JavaScript files.

Does Laravel mix use Babel?

A Laravel Mix extension to include polyfills by using Babel, core-js, and regenerator-runtime.


Video Answer


1 Answers

On your webpack.mix.js file

Just below

let mix = require('laravel-mix');

Add the following code

mix.webpackConfig({
    externals: {
        "jquery": "jQuery"
    }
});

Add any other external as required. For example I decided to load external React and ReactDOM so my config is

mix.webpackConfig({
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    }
});

Note that you can override any webpack default config inside mix.webpackConfig parameter object just like we did externals here

like image 105
Afzal Hossain Avatar answered Oct 03 '22 02:10

Afzal Hossain