Force jQuery Slim Import In Webpack

Jan 8, 2018

We could use easily use jQuery slim in webpack (20% smaller in size, excludes the ajax and effects module).

// import 'jquery'import 'jquery/dist/jquery.slim.js'

Problem arise when other packages import 'jquery' which causes both jquery.js and jquery.slim.js to be included.

To enforce that only jquery.slim.js is used, you can use resolve.alias to auto convert import 'jquery' to import 'jquery/dist/jquery.slim.js'.

Edit webpack config file.

module.exports = {  ...  resolve: {    extensions: ['.js'],    alias: {      'jquery': 'jquery/dist/jquery.slim.js',    }  },  ...}

Use webpack.ProvidePlugin to make jquery accessible globally using $ and jQuery.

var webpack = require('webpack');module.exports = {  ...  plugins: [    new webpack.ProvidePlugin({      '$': 'jquery',      jQuery: 'jquery',    }),  ]  ...}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.