Answers to Is Webpack smart enough to make jQuery lighter? ( 1 )

  1. 2017-01-06 22:01

    No, webpack will not only include the ajax portions of jquery in your bundle, even if you are using Webpack 2 (webpack 1 and previous does not implement tree-shaking; that is, the entire module will be included in your bundle, not just those that you import) this is because of how jquery is vended in NPM: as one, large jquery.js module.

    If you are intent on using jquery, there are ways to build jquery using only the ajax module, but this will require some manual effort (eg. you can't just do import {ajax} from 'jquery'). There is a babel plugin for lodash, which does vend each method in its own module so you don't have to include all of lodash in your webpack build. It is a babel plugin because it relies on the import { some_method } from 'library' syntax.

    If you are looking for an easy ajax library, I highly recommend using isomorphic-fetch (which, in browsers, just uses 'whatwg-fetch'). It keeps your code very tidy.

Leave a reply to - Is Webpack smart enough to make jQuery lighter?

◀ Go back