We can separate our custom application code from the common libraries we leverage, such as React and ReactDOM. In this lesson we'll configure webpack to treat React and ReactDOM as external dependencies. Then we'll update our HTML template to conditionally include the CDN links for those libraries for production builds.

Why to do this?

One of the main advantages of using a CDN is reduced latency, so if you cannot, for some reason, host your main bundle on a CDN, you can at least speed up loading of some dependencies. In addition to that, if you use a commonly used CDN, you can potentially take advantage of the browser's cached copy of React and ReactDOM from other sites that are also hosting from that CDN

The reduced latency comes from the fact that a CDN will serve files from a POP (point of presence) that is geographically closer to the requester. The reduced latency is a pure physics problem of distance. This doesn't solve other network issues like congestion and throughput limits, but it's a step in the right direction.

We only want to do this for production:

// webpack.config.prod.js

const merge = require('webpack-merge')
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer')
const baseConfig = require('./webpack.config.base') module.exports = merge(baseConfig, {
mode: 'production',
plugins: [new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: 'bundle_sizes.html'
})],
externals: {
react: 'React',
'react-dom': 'ReactDOM'
}
})

We add 'exernals' prop, it is a key value pair, key is the package name, value is the variable name:

import React from 'react'
import ReactDOM from 'react-dom'

Github

[Webpack] Externalize Dependencies to be Loaded via CDN with webpack的更多相关文章

  1. 利用免费cdn加速webpack单页应用

    回顾现状 在之前的学习过程中,react单页应用经过webpack打包之后会输出大概如下的目录结构,它就是站点的所有前端组成了:   1 2 3 4 5 6 MacBook-Pro:output ba ...

  2. webpack打包 The 'mode' option has not been set, webpack will fallback to

    webpack 打包报错 The 'mode' option has not been set, webpack will fallback to 'production' for,Module no ...

  3. Webpack实战(五):轻松读懂Webpack如何分离样式文件

    在上一篇文章中我给大家分享了预处理器(loader),里面讲到了style-loader 和css-loader,有关样式引入的问题,但是上面的样式文件只是引入到style标签里面,并不是我想要的样式 ...

  4. [Webpack 2] Add Code Coverage to tests in a Webpack project

    How much of your code runs during unit testing is an extremely valuable metric to track. Utilizing c ...

  5. webpack学习(二):先写几个webpack基础demo

    一.先写一个简单demo1 1-1安装好webpack后创建这样一个目录: 1-2:向src各文件和dist/index.html文件写入内容: <!DOCTYPE html> <h ...

  6. 前端笔记之ES678&Webpack&Babel(下)AMD|CMD规范&模块&webpack&Promise对象&Generator函数

    一.AMD和CMD规范(了解) 1.1传统的前端开发多个js文件的关系 yuan.js中定义了一个函数 function mianji(r){ return 3.14 * r * r } main.j ...

  7. webpack根据开发与生产环境配置不同变量--webpack.DefinePlugin

    webpack有一个DefinePlugin接口,可以实现根据开发与生产环境配置不同变量.范例如下: 需求:开发环境请求baseUrl = '':生产环境请求 baseUrl = 'http://lo ...

  8. 全局安装的 webpack运行时 报错 Error: Cannot find module 'webpack' ......

    全局安装的webpack   安装指令如下 cnpm install wepack -save-dev -g 但是 在我的项目空间运行webpack指令的时候 会报如下错误 为了方便抓取{ Error ...

  9. gulp+webpack+angular1的一点小经验(第二部分webpack包起来的angular1)

    又一周过去了,项目也已经做得有点模样了.收集来一些小经验,分享给大家,有疏漏之处,还望指正,海涵. 上周整合了gulp与webpack,那么工具准备差不多了,我们就开始编码吧.编码的框架就是angul ...

随机推荐

  1. QT学习笔记7:C++函数默认参数

    C++中允许为函数提供默认参数,又名缺省参数. 使用默认参数时的注意事项: ① 有函数声明(原型)时,默认参数可以放在函数声明或者定义中,但只能放在二者之一.建议放在函数声明中. double sqr ...

  2. BZOJ 3876: [Ahoi2014]支线剧情 带下界的费用流

    3876: [Ahoi2014]支线剧情 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3876 Description [故事背景] 宅 ...

  3. SGU 406 Goggle

    406. Goggle Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: standardoutput: ...

  4. request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别(记录)

    1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...

  5. GIT(6)----fork和clone的区别,fetch与pull的区别

    参考资料: [1].Git学习笔记:fork和clone的区别,fetch与pull的区别 [2].在Github和Git上fork之简单指南

  6. 不同的activity使用bundle对象传值给广播接收器

    解决了一下午的问题,广播机制传值,在一个activity中发送广播给广播接收器,使用的是同一个action 在另一个activity中如果也发送广播给同一个广播接收器,使用相同的action,会导致后 ...

  7. android点滴之ContentObserver的使用

    一概念 ContentObserver用于观察(捕捉)特定Uri引起的数据的变化,继而做一些对应的处理,当ContentObserver所观察的Uri发生变化时,便会触发它. 从概念看ContentO ...

  8. vultr vs digitalocean vs linode

    vultr官方网站:www.vultr.comdigitalocean官方网站:www.digitalocean.comlinode官方网站:www.linode.com 一般来说我们买VPS的时候都 ...

  9. 多线程调试必杀技 - GDB的non-stop模式

     作者:破砂锅  (转) 开源的GDB被广泛使用在Linux.OSX.Unix和各种嵌入式系统(例如手机),这次它又带给我们一个惊喜. 多线程调试之痛 调试器(如VS2008和老版GDB)往往只支持a ...

  10. [Node.js]OS模块

    摘要 Node.js有很多工具模块,比如os,path,net,dns,domain模块.这里先介绍os模块的使用方法.os模块提供了一些基本的系统操作函数. os模块 引入os模块 var os=r ...