How to include external libraries via CDN asynchro

2019-06-26 06:39发布

Prior to posting this question, I consulted the following threads on this topic:

How to include external CDN link into webpack project

Webpack and external libraries

how to use webpack to load CDN or external vendor javascript lib in js file, not in html file How to include jQuery CDN in Webpack?

Webpack externals documentation

Based on all the above, it appears that in order to load a third-party library via a CDN, the standard method would be to use webpack externals, which produces the following script tag in the index.html file (per Webpack docs).

<script
  src="https://code.jquery.com/jquery-3.1.0.js"
  integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="
  crossorigin="anonymous">
</script>

While this solution works if you have one or two libraries, how are you supposed to load 10 libraries (for arguments sake) via CDNs asynchronously (to maximise pageload performance) using externals in Webpack?

My current solution to this problem is to manually include the following script tags in my index.html file with relevant "async" and "defer" tags; however, I need to update the index.html file in my dist folder every time I include a new library, which leads me to believe I'm doing this completely wrong. Below is what my index.html file currently looks like, which is also replicated in the dist folder.

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>App</title>
        <link rel="stylesheet" href="https://afeld.github.io/emoji-css/emoji.css">
    </head>

    <body>
        <div id="app"></div>
    </body>

    <script src="https://cdn.jsdelivr.net/npm/moment@2.20.1/moment.min.js" async defer></script>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js" async defer></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/4.0.0/math.min.js" async defer></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js" async defer></script>
    <script src="https://widget.cloudinary.com/global/all.js" async defer></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js" async defer></script>
    <script src="/dist/build.js" async defer></script>
</html>

While I'm sure there's a "hack-ey" way of achieving my goal (which I'm open to seeing), my question is whether I can accomplish the above through an officially supported method (like through externals). If not, how does most of the web development world use libraries given that CDNs are known to almost always improve pageload performance?

EDIT: For some context, I'm using Webpack because I'm using vue-loader to build my VueJS SPA (as a learning project).

EDIT 2: This question was asked using incorrect assumptions about Webpack and how web development works. The conclusion is that you shouldn't be using CDNs as liberally as I have if you're using Webpack. Refer to the comment section for clarification.

0条回答
登录 后发表回答