Dynamically get your dependencies from a cdn rather than bundling them in your app
npm install dynamic-cdn-webpack-plugin    
> Dynamically get your dependencies from a cdn rather than bundling them in your app
```
$ npm install --save-dev dynamic-cdn-webpack-plugin module-to-cdn
If you are using webpack --version <= 3 then you should be installing with the following command.
``
$ npm install --save-dev dynamic-cdn-webpack-plugin@3.4.1 module-to-cdn
webpack.config.js`js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const DynamicCdnWebpackPlugin = require('dynamic-cdn-webpack-plugin');
module.exports = {
entry: {
'app.js': './src/app.js'
},
output: {
path.resolve(__dirname, './build'),
},
plugins: [
new HtmlWebpackPlugin(),
new DynamicCdnWebpackPlugin()
]
}
`
app.js`js
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
// ... do react stuff
`
webpack --mode=production will generate:
`js
/ simplified webpack build /
[function(module, __webpack_exports__, __webpack_require__) {
module.exports = React;
}),
(function(module, __webpack_exports__, __webpack_require__) {
module.exports = ReactRouterDOM;
}),
(function(module, __webpack_exports__, __webpack_require__) {
var react = __webpack_require__(0);
var reactRouterDOM = __webpack_require__(1);
/ ... /
})]
`
`html`
webpack.config.js`js
const path = require('path');
const ManifestPlugin = require('webpack-manifest-plugin');
const DynamicCdnWebpackPlugin = require('dynamic-cdn-webpack-plugin');
module.exports = {
entry: {
'app': './src/app.js'
},
output: {
path.resolve(__dirname, './build'),
},
plugins: [
new ManifestPlugin({
fileName: 'manifest.json'
}),
new DynamicCdnWebpackPlugin()
]
}
`
app.js`js
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
// ... do react stuff
`
webpack --mode=production will generate:
`js
/ simplified webpack build /
[function(module, __webpack_exports__, __webpack_require__) {
module.exports = React;
}),
(function(module, __webpack_exports__, __webpack_require__) {
module.exports = ReactRouterDOM;
}),
(function(module, __webpack_exports__, __webpack_require__) {
var react = __webpack_require__(0);
var reactRouterDOM = __webpack_require__(1);
/ ... /
})]
`
`json`
{
"app.js": "app.js",
"react.js": "https://unpkg.com/react@15.5.3/dist/react.min.js",
"react-router-dom.js": "https://unpkg.com/react-router-dom@4.1.1/umd/react-router-dom.min.js"
}
webpack.config.js`js
const DynamicCdnWebpackPlugin = require('dynamic-cdn-webpack-plugin');
module.exports = {
mode: 'production',
plugins: [
new DynamicCdnWebpackPlugin(options)
]
}
`
#### options.disable
Type: booleanfalse
Default:
Useful when working offline, will fallback to webpack normal behaviour
#### options.env
Type: stringmode
Default: development
Values: , production
Determine if it should load the development or the production version of modules
#### options.only
Type: Arraynull
Default:
List the only modules that should be served by the cdn
#### options.exclude
Type: Array[]
Default:
List the modules that will always be bundled (not be served by the cdn)
#### options.verbose
Type: booleanfalse
Default:
Log whether the library is being served by the cdn or is bundled
#### options.resolver
Type: string, function'module-to-cdn'
Default:
Allow you to define a custom module resolver, it can either be a function or an npm module.null
The resolver should return (or resolve as a Promise) either or an object with the keys: name, var, url, version`.
- html-webpack-plugin
- module-to-cdn
Thanks goes to these wonderful people (emoji key):
|
Thomas Sileghem
💻 📖 ⚠️ |
​Faizaan
đź’¬ đź’» đź“– |
MICHAEL JACKSON
đź’ˇ |
fedeoo
đź’» |
| :---: | :---: | :---: | :---: |
This project follows the all-contributors specification. Contributions of any kind welcome!
MIT © Thomas Sileghem