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

html-webpack-externals-plugin-modified

v2.1.1

Published

Supplemental plugin for HtmlWebpackPlugin to append script tags for external modules

Downloads

4

Readme

html-webpack-externals-plugin

This plugin supplements the fantastic html-webpack-plugin by providing a very basic interface for loading your external dependencies.

Webpack developers advise using externals to speed up compile time during development/builds as well as to leverage the caching capabilities of browsers.

This plugin allows you to load external CSS and JS dependencies with:

  • Absolute URLs. This approach lets you leverage the caching and speed benefits of CDNs. In this case, the module does not need to be in your dependencies list of your package.json and require/import statements will still behave as expected. Script/link tags with the given URLs are appended to the HTML.
  • Local module dist files. This approach lets you serve dist files of your specified dependencies instead of bundling them. At build time, the listed externals are copied from your node_modules into your build and script/link tags with relative paths are appended to the HTML.

Installation

npm install html-webpack-externals-plugin --save-dev

Usage

Add it to the plugins array of your Webpack configuration, after your HtmlWebpackPlugin instance.

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');

module.exports = {
  // ...your Webpack config
  plugins: [
    new HtmlWebpackPlugin(),
    new HtmlWebpackExternalsPlugin(
      // See the API section
    );
  ]
};

When using this plugin, do not define externals in the Webpack configuration yourself; it will be written by the plugin at runtime.

API

new HtmlWebpackExternalsPlugin(externals, options)

externals

An array of objects, each of which represents an external. Each object may have a set of properties, which are documented in the following code sample.

[
  {
    // The name of the external dependency, i.e. what is passed into `require()` calls or `import`
    // statements.
    name: 'react',
    // JS library dists typically export their API through a single global variable on the `window`
    // object, e.g. `jQuery` or `React`. If the external does not export anything (e.g. a CSS
    // dependency), omit this property.
    var: 'React',
    // The absolute URL to use when loading the dependency from a CDN.
    url: 'https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react.js'
  },
  {
    name: 'react',
    var: 'React',
    // Alternatively, you can specify a path to a dist file of one of your packages in `node_modules`.
    // This will copy it to the build directory when Webpack runs.
    path: 'react/dist/react.min.js'
  }
]

options

An object containing configuration options for the plugin. Both options apply only to local externals (i.e. externals that use path instead of url).

{
  // The absolute path to resolve locally installed externals from. Usually this is your
  // application's root directory. It is required for loading local externals. Most of the time you
  // can pass `__dirname` to use the current directory.
  basedir: __dirname,
  // The directory to copy locally installed externals to within the build directory. Defaults to
  // `'vendor'`.
  dest: 'vendor'
}

Example

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');

module.exports = {
  // ...your Webpack config
  plugins: [
    new HtmlWebpackPlugin(),
    new HtmlWebpackExternalsPlugin(
      [
        // Using a CDN for a JS library
        {
          name: 'jquery',
          var: 'jQuery',
          url: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.js'
        },
        // Using a locally installed module for a JS library
        {
          name: 'react',
          var: 'React',
          path: 'react/dist/react.min.js'
        },
        // Using a CDN for a library with no export (e.g. a CSS module)
        {
          name: 'bootstrap.css',
          url: 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css'
        }
      ],
      {
        // Resolve local modules relative to this directory
        basedir: __dirname
      }
    );
  ]
};

index.jsx

import React, {Component} from 'react';
import $ from 'jquery';
// No need to import 'bootstrap.css' because it's already been added to the page

Note that since they are externals, they are always loaded exactly once, whether they are used in source code or not. So this means that it is unnecessary to import the CSS libraries, like Bootstrap.

Assets Required by Library

For local externals (i.e. externals that use path instead of url), sometimes you need to copy other assets to the dist that the library depends on (e.g. font assets used by Bootstrap).

The easiest way to accomplish this is by complementing the HtmlWebpackExternalsPlugin with the CopyWebpackPlugin, which copies files in a directory to the build.

In your Webpack configuration's plugins array, add the plugin after your HtmlWebpackExternalsPlugin instance.

new CopyWebpackPlugin([
  {from: 'node_modules/bootstrap/dist/', to: 'vendor/bootstrap/dist/'},
  {from: 'node_modules/font-awesome/fonts/', to: 'vendor/font-awesome/fonts/'}
])