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

webpack-bower-externals

v1.5.4

Published

Easily exclude bower_components in Webpack bundle

Downloads

20

Readme

Webpack Bower Components externals

Easily exclude Bower components in Webpack (Forked from https://github.com/liady/webpack-node-externals)

Webpack allows you to define externals - modules that should not be bundled.

When bundling with Webpack for the backend - you usually don't want to bundle its bower_components dependencies. This library creates an externals function that ignores bower_components when bundling in Webpack.(Inspired by the great Backend apps with Webpack series)

Quick usage

npm install webpack-bower-externals --save-dev

In your webpack.config.js:

var bowerExternals = require('webpack-bower-externals');
...
module.exports = {
    ...
    target: 'umd', // in order to ignore built-in modules like path, fs, etc.
    externals: [bowerExternals()], // in order to ignore all modules in bower_components folder
    ...
};

And that's it. All Bower components will no longer be bundled but will be left as require('module').

Detailed overview

Description

This library scans the bower_components folder for all bower_components names, and builds an externals function that tells Webpack not to bundle those modules, or any sub-modules of theirs.

Configuration

This library accepts an options object.

options.whitelist (=[])

An array for the externals to whitelist, so they will be included in the bundle. Can accept exact strings ('module_name'), regex patterns (/^module_name/), or a function that accepts the module name and returns whether it should be included. Important - if you have set aliases in your webpack config with the exact same names as modules in bower_components, you need to whitelist them so Webpack will know they should be bundled.

options.importType (='commonjs')

The method in which unbundled modules will be required in the code. Best to leave as umd for bower components.

options.modulesDir (='bower_components')

The folder in which to search for the bower components.

options.modulesFromFile (=false)

Read the modules from the .bower.json file instead of the bower_components folder.

Example

var bowerExternals = require('webpack-bower-externals');
...
module.exports = {
    ...
    target: 'umd', // important in order not to bundle built-in modules like path, fs, etc.
    externals: [bowerExternals({
        // this WILL include `jquery` and `webpack/hot/dev-server` in the bundle, as well as `lodash/*`
        whitelist: ['jquery', 'webpack/hot/dev-server', /^lodash/]
    })],
    ...
};

For most use cases, the defaults of importType and modulesDir should be used.

Q&A

Why not just use a regex in the Webpack config?

Webpack allows inserting regex in the externals array, to capture non-relative modules:

{
    externals: [
        // Every non-relative module is external
        // abc -> require("abc")
        /^[a-z\-0-9]+$/
    ]
}

However, this will leave unbundled all non-relative requires, so it does not account for aliases that may be defined in webpack itself. This library scans the bower_components folder, so it only leaves unbundled the actual Bower components that are being used.

How can I bundle required assets (i.e css files) from bower_components?

Using the whitelist option, this is possible. We can simply tell Webpack to bundle all files with extensions that are not js/jsx/json, using this regex:

...
bowerExternals({
  // load non-javascript files with extensions, presumably via loaders
  whitelist: [/\.(?!(?:jsx?|json)$).{1,5}$/i],
}),
...

Thanks @wmertens for this idea.

Contribute

Contributions and pull requests are welcome. Please run the tests to make sure nothing breaks.

Test

npm run test

License

MIT