All Projects → liady → Webpack Node Externals

liady / Webpack Node Externals

Licence: mit
Easily exclude node modules in Webpack

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Webpack Node Externals

Webpack Virtual Modules
Webpack Virtual Modules is a webpack plugin that lets you create, modify, and delete in-memory files in a way that webpack treats them as if they were physically presented in the file system.
Stars: ✭ 286 (-74.21%)
Mutual labels:  webpack, modules
Minipack
📦 A simplified example of a modern module bundler written in JavaScript
Stars: ✭ 2,625 (+136.7%)
Mutual labels:  webpack, modules
Cjstoesm
A tool that can transform CommonJS to ESM
Stars: ✭ 109 (-90.17%)
Mutual labels:  webpack, modules
Html Sass Babel Webpack Boilerplate
Webpack 4 + Babel + ES6 + SASS + HTML Modules + Livereload
Stars: ✭ 35 (-96.84%)
Mutual labels:  webpack, modules
Craco Alias
A craco plugin for automatic aliases generation for Webpack and Jest
Stars: ✭ 56 (-94.95%)
Mutual labels:  webpack
React Webpack Tutorial
This is a tutorial on how to get started developing a client side application using ReactJS, Webpack and Npm
Stars: ✭ 54 (-95.13%)
Mutual labels:  webpack
React Boilerplate
Production-ready boilerplate for building universal web apps with React and Redux
Stars: ✭ 53 (-95.22%)
Mutual labels:  webpack
Js Toolbox
CLI tool to simplify the development of JavaScript apps/libraries with little to no configuration. (WORK IN PROGRESS/PACKAGE NOT PUBLISHED).
Stars: ✭ 53 (-95.22%)
Mutual labels:  webpack
Node Express Typescript Boilerplate
A boilerplate for developing web apps with Node.js, Express.js & TypeScript. Demonstrates helpful recipes in 8 steps in individual branches.
Stars: ✭ 60 (-94.59%)
Mutual labels:  webpack
Prestashop
Free PWA & SPA for PrestaShop
Stars: ✭ 59 (-94.68%)
Mutual labels:  webpack
React Cool Starter
😎 🐣 A starter boilerplate for a universal web app with the best development experience and a focus on performance and best practices.
Stars: ✭ 1,083 (-2.34%)
Mutual labels:  webpack
Vue Multiple Pages
A multiple Pages Starter use Vue-cli3
Stars: ✭ 1,079 (-2.71%)
Mutual labels:  webpack
Egg React Typescript Boilerplate
Egg React TypeScript Server Side Render (SSR) / Client Side Render (CSR)
Stars: ✭ 56 (-94.95%)
Mutual labels:  webpack
Ts React Redux Startup
TypeScript redux configuration project, check also https://github.com/brunolm/react-how-to
Stars: ✭ 54 (-95.13%)
Mutual labels:  webpack
React Redux Typescript Boilerplate
A bare minimum frontend boilerplate with React 16, Typescript 3 and Webpack 4
Stars: ✭ 1,100 (-0.81%)
Mutual labels:  webpack
Interactive Image
A jQuery plugin to embed interactive images on your website.
Stars: ✭ 53 (-95.22%)
Mutual labels:  webpack
Chatinder
Unofficial desktop messaging client for Tinder.
Stars: ✭ 55 (-95.04%)
Mutual labels:  webpack
React App Rewired
Override create-react-app webpack configs without ejecting
Stars: ✭ 8,630 (+678.18%)
Mutual labels:  webpack
Vue Dropload
vue下拉加载,简单路由,模态框组件等开发
Stars: ✭ 55 (-95.04%)
Mutual labels:  webpack
Samples Module Loading Comparison
Some tests for comparing performance between bundling and unbundling JS
Stars: ✭ 54 (-95.13%)
Mutual labels:  modules

Webpack node modules externals

Easily exclude node modules in Webpack

Version Downloads Build Status

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 node_modules dependencies. This library creates an externals function that ignores node_modules when bundling in Webpack.
(Inspired by the great Backend apps with Webpack series)

Quick usage

npm install webpack-node-externals --save-dev

In your webpack.config.js:

const nodeExternals = require('webpack-node-externals');
...
module.exports = {
    ...
    target: 'node', // in order to ignore built-in modules like path, fs, etc.
    externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
    ...
};

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

Note: For Webpack 5, replace target: 'node' with the externalsPreset object:

// Webpack 5

const nodeExternals = require('webpack-node-externals');
...
module.exports = {
    ...
    externalsPresets: { node: true }, // in order to ignore built-in modules like path, fs, etc.
    externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
    ...
};

Detailed overview

Description

This library scans the node_modules folder for all node_modules 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.allowlist (=[])

An array for the externals to allow, 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 node_modules, you need to allowlist 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 commonjs for node modules. May be one of documented options or function callback(moduleName) which returns custom code to be returned as import type, e.g:

options.importType = function (moduleName) {
    return 'amd ' + moduleName;
}

options.modulesDir (='node_modules')

The folder in which to search for the node modules.

options.additionalModuleDirs (='[]')

Additional folders to look for node modules.

options.modulesFromFile (=false)

Read the modules from the package.json file instead of the node_modules folder.
Accepts a boolean or a configuration object:

{
    modulesFromFile: true,
    /* or */
    modulesFromFile: {
        fileName: /* path to package.json to read from */,
        includeInBundle: [/* whole sections to include in the bundle, i.e 'devDependencies' */],
        excludeFromBundle: [/* whole sections to explicitly exclude from the bundle, i.e only 'dependencies' */]
    }
}

Usage example

var nodeExternals = require('webpack-node-externals');
...
module.exports = {
    ...
    target: 'node', // important in order not to bundle built-in modules like path, fs, etc.
    externals: [nodeExternals({
        // this WILL include `jquery` and `webpack/hot/dev-server` in the bundle, as well as `lodash/*`
        allowlist: ['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 node_modules folder, so it only leaves unbundled the actual node modules that are being used.

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

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

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

Thanks @wmertens for this idea.

Why is not bundling node_modules a good thing?

When writing a node library, for instance, you may want to split your code to several files, and use Webpack to bundle them. However - you wouldn't want to bundle your code with its entire node_modules dependencies, for two reasons:

  1. It will bloat your library on npm.
  2. It goes against the entire npm dependencies management. If you're using Lodash, and the consumer of your library also has the same Lodash dependency, npm makes sure that it will be added only once. But bundling Lodash in your library will actually make it included twice, since npm is no longer managing this dependency.

As a consumer of a library, I want the library code to include only its logic, and just state its dependencies so they could me merged/resolved with the rest of the dependencies in my project. Bundling your code with your dependencies makes it virtually impossible.

In short: It's useful if your code is used by something that has dependencies managed by npm

Contribute

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

Test

npm run test

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].