Skip to content
This repository has been archived by the owner on Aug 25, 2020. It is now read-only.

A Webpack plugin that will replace popular dependencies with CDN script refs in your HTML entry file

License

Notifications You must be signed in to change notification settings

cultureamp/jsdelivr-cdn-webpack-plugin

 
 

Repository files navigation

jsdelivr-cdn-webpack-plugin

npm Build Status codecov David XO code style

This plugin extracts out your chosen dependencies from your build, and replaces them with a Script tag in your output html file referencing your desired package version in the JSDelivr CDN network. This plugin works alongside HtmlWebpackPlugin.

Why is this cool?

Do you manage your large dependencies via npm or yarn, or insert a script tag in your html template?

Using jsdelivr-cdn-webpack-plugin you get the best of both worlds. You can manage your package versions using your favourite package management tool, but leverage the power of JSDeliver network, your browser and the web to cache commonly used dependencies making your site start faster.

JSDelivr serves files from their large distributed network

JSDelivr Network

Requirements

This module requires a minimum of Node v6.9.0 and Webpack v4.0.0.

Install

npm i --save-dev jsdelivr-cdn-webpack-plugin html-webpack-plugin
yarn add jsdelivr-cdn-webpack-plugin html-webpack-plugin --dev

Use your favourite libraries such as React

app.js

import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';

// ... do react stuff

Then add the plugin to your webpack config. For example:

webpack.config.js

// Example Webpack configuration
const HtmlWebpackPlugin = require('html-webpack-plugin');
const JSDelivrCDNWebpackPlugin = require('jsdelivr-cdn-webpack-plugin');

module.exports = {
    ...
    plugins : [
        HtmlWebpackPlugin({
            title: 'My App',
            filename: 'dist/index.html'
        }),
        JSDelivrCDNWebpackPlugin({
            env: 'production',
            include: ['react', 'prop-types', 'lodash']
        }),
    ]
};

And run webpack via your preferred method.

Your generated index.html should look like

<!-- Example Generated HTML Output -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>My App</title>
</head>
<body>
    <div id="app"></div>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/react@16.5.2/react.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/prop-types@15.6.2/prop-types.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
    <script type="text/javascript" src="myapp-3e34c5eda4a2e99d1c0d.js"></script></body>
</html>

Options

include

env

Resources

License

MIT © Matthew McLeod

About

A Webpack plugin that will replace popular dependencies with CDN script refs in your HTML entry file

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%