Skip to content

Get your dependencies from a cdn rather than bundling them in your app

License

Notifications You must be signed in to change notification settings

forivall/dynamic-cdn-webpack-plugin

Β 
Β 

Repository files navigation

@forivall/dynamic-cdn-webpack-plugin

build status dependency status coverage status npm version XO code style

Dynamically get your dependencies from a cdn rather than bundling them in your app

NOTE: This module is a fork of https://github.com/mastilver/dynamic-cdn-webpack-plugin to support Webpack 5, and use Typescript.

Install

$ npm install --save-dev @forivall/dynamic-cdn-webpack-plugin

Usage with HtmlWebpackPlugin

webpack.config.js

const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const DynamicCdnWebpackPlugin = require('@forivall/dynamic-cdn-webpack-plugin');

module.exports = {
    entry: {
        'app.js': './src/app.js'
    },

    output: {
        path.resolve(__dirname, './build'),
    },

    plugins: [
        new HtmlWebpackPlugin(),
        new DynamicCdnWebpackPlugin()
    ]
}

app.js

import React from 'react';
import { BrowserRouter } from 'react-router-dom';

// ... do react stuff

webpack --mode=production will generate:

/* 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);

    /* ... */
})]
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
  </head>
  <body>
    <script type="text/javascript" src="https://unpkg.com/react@15.5.3/dist/react.min.js"></script><script type="text/javascript" src="https://unpkg.com/react-router-dom@4.1.1/umd/react-router-dom.min.js"></script><script src="build/app.js"></script></body>
</html>

Usage with ManifestPlugin

webpack.config.js

const path = require('path');

const ManifestPlugin = require('webpack-manifest-plugin');
const DynamicCdnWebpackPlugin = require('@forivall/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

import React from 'react';
import { BrowserRouter } from 'react-router-dom';

// ... do react stuff

webpack --mode=production will generate:

/* 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);

    /* ... */
})]
{
    "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"
}

API

DynamicCdnWebpackPlugin(options)

webpack.config.js

const DynamicCdnWebpackPlugin = require('@forivall/dynamic-cdn-webpack-plugin');

module.exports = {
    mode: 'production',
    plugins: [
        new DynamicCdnWebpackPlugin(options)
    ]
}

options.disable

Type: boolean
Default: false

Useful when working offline, will fallback to webpack normal behaviour

options.env

Type: string
Default: mode
Values: development, production

Determine if it should load the development or the production version of modules

options.only

Type: Array<string> Default: null

List the only modules that should be served by the cdn

options.exclude

Type: Array<string> Default: []

List the modules that will always be bundled (not be served by the cdn)

options.verbose

Type: boolean
Default: false

Log whether the library is being served by the cdn or is bundled

options.resolver

Type: string, function
Default: 'module-to-cdn'

Allow you to define a custom module resolver, it can either be a function or an npm module. The resolver should return (or resolve as a Promise) either null or an object with the keys: name, var, url, version.

options.html

Type: boolean
Default: !loadScripts (true, unless options.loadScripts is true)

Inject the CDN script tags into the HtmlWebpackPlugin, if available

options.loadScripts

Type: boolean
Default: false

Instead of expecting the scripts to already be loaded via a <script src="..."></script> in the html, load them dynamically. Uses webpack externalsType.script

Related

Contributors

Thanks goes to these wonderful people (emoji key):


Thomas Sileghem

πŸ’» πŸ“– ⚠️

​Faizaan

πŸ’¬ πŸ’» πŸ“–

MICHAEL JACKSON

πŸ’‘

fedeoo

πŸ’»

Kevin Malakoff

πŸ’»

Krystian Kuczek

πŸ’» ⚠️

Emily Marigold Klassen

πŸ’» πŸš‡

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT Β© Thomas Sileghem

About

Get your dependencies from a cdn rather than bundling them in your app

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 82.8%
  • TypeScript 17.2%