7

Since a CDN version of jQuery is very likely to be cached. Is there a way to include the CDN version in webpack as opposed to the local npm version.

What is the best way to include jQuery in Webpack if it's not loaded on the page?

Ricky Han
  • 1,309
  • 1
  • 15
  • 25
  • 3
    Did you read the documentation? *"You can also use the `externals` option to import an existing API into applications. For example, if you want to use jQuery from a CDN via a separate ` – Felix Kling Sep 30 '16 at 00:35
  • Yeah. But what if the page doesn't have jquery – Ricky Han Sep 30 '16 at 00:39
  • Can you not edit the HTML file and add the tag? – Felix Kling Sep 30 '16 at 00:42
  • I'm building an embed.js for any website so the answer is no I can't. – Ricky Han Sep 30 '16 at 00:56
  • Then you might want what is proposed in https://github.com/webpack/webpack/issues/240 (which I found through https://www.google.com/search?q=webpack+cdn+loader). – Felix Kling Sep 30 '16 at 00:57
  • Perfect. Thank you. Can you put into an answer? – Ricky Han Sep 30 '16 at 01:00
  • 1
    Not a dupe. This is about proactively loading external libs on webpages, not just importing it. – Ricky Han Sep 30 '16 at 01:06

1 Answers1

4

This seems to have been discussed before, see https://github.com/webpack/webpack/issues/150. The response was:

webpack is a module bundler not a javascript loader. It package files from local disk and don't load files from the web (except its own chunks).

Use a javascript loader, i. e. script.js

var $script = require("scriptjs");
$script("//ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js", function() {
    // ... 
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143