npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@likun7981/webpack-cdn-plugin

v1.7.4

Published

A webpack plugin that use externals of CDN urls for production and local node_modules for development

Downloads

10

Readme

CDN extension for the HTML Webpack Plugin

Build Status codecov

Enhances html-webpack-plugin functionality by allowing you to specify the modules you want to externalize from node_modules in development and a CDN in production.

Basically this will allow you to greatly reduce build time when developing and improve page load performance on production.

Installation

It is recommended to run webpack on node 5.x or higher

Install the plugin with npm:

npm install webpack-cdn-plugin --save-dev

or yarn

yarn add webpack-cdn-plugin --dev

Basic Usage

Require the plugin in your webpack config:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackCdnPlugin = require('webpack-cdn-plugin');

Add the plugin to your webpack config:

module.exports = {
  // ...
  plugins: [
    new HtmlWebpackPlugin(),
    new WebpackCdnPlugin({
      modules: [
        {
          name: 'vue',
          var: 'Vue',
          style: 'dist/vue.css'
        },
        {
          name: 'vue-router'
        }
      ],
      publicPath: '/node_modules'
    })
  ]
  // ...
};

This will generate an index.html file with something like below:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
    <link href="//unpkg.com/[email protected]/dist/vue.css" rel="stylesheet">
  </head>
  <body>
  <script type="text/javascript" src="//unpkg.com/[email protected]/dist/vue.runtime.common.js"></script>
  <script type="text/javascript" src="//unpkg.com/[email protected]/dist/vue-router.common.js"></script>
  <script type="text/javascript" src="/assets/app.js"></script>
  </body>
</html>

When you set prod to false, it will output urls using publicPath, so you might need to expose it as some sort of static route.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
    <link href="/node_modules/vue/dist/vue.css" rel="stylesheet">
  </head>
  <body>
  <script type="text/javascript" src="/node_modules/vue/dist/vue.runtime.common.js"></script>
  <script type="text/javascript" src="/node_modules/vue-router/dist/vue-router.common.js"></script>
  <script type="text/javascript" src="/assets/app.js"></script>
  </body>
</html>

You can also use your own custom html template, please refer to html-webpack-plugin.

Please see the example folder for a basic working example.

Configuration

You can pass an object options to WebpackCdnPlugin. Allowed values are as follows:

modules:array

The available options for each module, which is part of an array.

name:string

The name of the module you want to externalize

cdn:string (optional)

If the name from the CDN resource is different from npm, you can override with this i.e. moment is moment.js on cdnjs

var:string (optional)

A variable that will be assigned to the module in global scope, webpack requires this. If not supplied than it will be the same as the name.

path:string (optional)

You can specify a path to the main file that will be used, this is useful when you want the minified version for example if main does not point to it.

style:string (optional)

If the module comes with style sheets, you can also specify it as a path.

cssOnly:boolean | false

If the module is just a css library, you can specify cssOnly to true, it will ignore path.

localScript:string (option)

Useful when you wanted to use your own build version of the library for js files

localStyle:string (option)

Useful when you wanted to use your own build version of the library for css files

prod:boolean | true

prod flag defaults to true, which will output uri using the CDN, when false it will use the file from node_modules folder locally.

prodUrl:string | //unpkg.com/:name@:version/:path

You can specify a custom template url with the following replacement strings:

:name: The name of the module e.g. vue

:version: The version of the module e.g. 1.0.0

:path: The path to the file e.g. lib/app.min.js

A common example is you can use cdnjs e.g. //cdnjs.cloudflare.com/ajax/libs/:name/:version/:path. If not specified it will fallback to using unpkg.com.

devUrl:string | /:name/:path

Similar to prodUrl, this option overrides the default template url for when prod is false

publicPath:string (optional)

Prefixes the assets with this string, if none is provided it will fallback to the one set globally in webpack.options.output.publicPath, note that this is always empty when prod is true so that it makes use of the CDN location because it is a remote resource.

Contribution

This is a pretty simple plugin and caters mostly for my needs. However, I have made it as flexible and customizable as possible.

If you happen to find any bugs, do please report it in the issues or can help improve the codebase, pull requests are always welcomed.

Resources

Contributors

Many thanks to the following contributors: