Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack and external libraries

I’m trying out webpack (http://webpack.github.io/) and it looks really nice, however I’m kind of stuck here.

Say that I’m using a CDN for a library, f.ex jQuery. Then in my code, I want the require('jquery') to automatically point to the global jquery instance, instead of trying to include it from my modules.

I’ve tried using plugins like the IgnorePlugin:

new webpack.IgnorePlugin(new RegExp("^(jquery|react)$")) 

This works for ignoring the library, but it still says that the required module is "missing" when I run the webpacker.

Somehow I need to tell webpack that jquery should be picked up from the global context instead. Seems like a common use case, so I’m kind of surprised that the docs doesn’t target this specifically.

like image 550
David Hellsing Avatar asked Mar 20 '14 10:03

David Hellsing


People also ask

What is external in webpack?

May 4, 2020. Webpack externals tell Webpack to exclude a certain import from the bundle. Often externals are used to exclude imports that will be loaded via CDN. For example, suppose you are implementing server-side rendering with Vue and Express, but your client-side code imports Vue via a CDN.

What is the use of webpack node externals?

Webpack node modules externals. Webpack allows you to define externals - modules that should not be bundled. When bundling with Webpack for the backend - you usually don't want to bundle its node_modules dependencies. This library creates an externals function that ignores node_modules when bundling in Webpack.

What is the main purpose of webpack?

Webpack is a tool that lets you compile JavaScript modules, also known as module bundler. Given a large number of files, it generates a single file (or a few files) that run your app. It can perform many operations: helps you bundle your resources.

Is webpack a library or framework?

Big three frameworks like Angular, React, Vue came into play the past few years. Then came these so-called “module bundlers” or build tools, namely webpack, browserify, and gulp.


1 Answers

According to the Webpack documentation, you can use the externals property on the config object "to specify dependencies for your library that are not resolved by webpack, but become dependencies of the output. This means they are imported from the enviroment while runtime [sic]."

The example on that page illustrates it really well, using jQuery. In a nutshell, you can require jQuery in the normal CommonJS style:

var jQuery = require('jquery'); 

Then, in your config object, use the externals property to map the jQuery module to the global jQuery variable:

{     externals: {         // require("jquery") is external and available         //  on the global var jQuery         "jquery": "jQuery"     } } 

The resulting module created by Webpack will simply export the existing global variable (I'm leaving out a lot of stuff here for brevity):

{     1: function(...) {         module.exports = jQuery;     } } 

I tried this out, and it works just as described.

like image 168
Garrett Avatar answered Sep 20 '22 23:09

Garrett