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?
Yes, you can use Mix outside of Laravel; it is an NPM package after all.
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.
A Laravel Mix extension to include polyfills by using Babel, core-js, and regenerator-runtime.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With