All Projects → indutny → Webpack Common Shake

indutny / Webpack Common Shake

CommonJS Tree Shaker plugin for WebPack

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Webpack Common Shake

Webpack Core Usage
webpack2完整系列课程,欢迎阅读。同时欢迎移步我的react全家桶文章全集: https://github.com/liangklfangl/react-article-bucket
Stars: ✭ 94 (-89.26%)
Mutual labels:  webpack, webpack-plugin, tree-shaking
Moment Locales Webpack Plugin
Easily remove unused Moment.js locales with webpack
Stars: ✭ 396 (-54.74%)
Mutual labels:  webpack, webpack-plugin
Webpack Chrome Extension Reloader
🔥 Hot reloading while developing Chrome extensions with webpack 🔥
Stars: ✭ 365 (-58.29%)
Mutual labels:  webpack, webpack-plugin
Babel Minify Webpack Plugin
[DEPRECATED] Babel Minify Webpack Plugin
Stars: ✭ 502 (-42.63%)
Mutual labels:  webpack, webpack-plugin
Web Webpack Plugin
alternative for html-webpack-plugin
Stars: ✭ 318 (-63.66%)
Mutual labels:  webpack, webpack-plugin
Webpack Extension Reloader
A upgrade from 🔥webpack-chrome-extension-reloader🔥, now on all browsers
Stars: ✭ 355 (-59.43%)
Mutual labels:  webpack, webpack-plugin
Offline Plugin
Offline plugin (ServiceWorker, AppCache) for webpack (https://webpack.js.org/)
Stars: ✭ 4,444 (+407.89%)
Mutual labels:  webpack, webpack-plugin
React Dynamic Route Loading Es6
Auto chunking and dynamic loading of routes with React Router and Webpack 2
Stars: ✭ 297 (-66.06%)
Mutual labels:  webpack, tree-shaking
Webpack Config Plugins
Provide best practices for webpack loader configurations
Stars: ✭ 529 (-39.54%)
Mutual labels:  webpack, webpack-plugin
Webpack Deep Scope Analysis Plugin
A webpack plugin for deep scope analysis
Stars: ✭ 589 (-32.69%)
Mutual labels:  webpack, webpack-plugin
Duplicate Package Checker Webpack Plugin
🕵️ Webpack plugin that warns you when a build contains multiple versions of the same package
Stars: ✭ 635 (-27.43%)
Mutual labels:  webpack, webpack-plugin
Filemanager Webpack Plugin
Copy, move, archive (zip/tar/tar.gz), delete files and directories before and after Webpack builds. Win32/Mac/*Nix supported
Stars: ✭ 310 (-64.57%)
Mutual labels:  webpack, webpack-plugin
Webpack Cdn Plugin
A webpack plugin that use externals of CDN urls for production and local node_modules for development
Stars: ✭ 306 (-65.03%)
Mutual labels:  webpack, webpack-plugin
Browser Sync Webpack Plugin
Easily use BrowserSync in your Webpack project.
Stars: ✭ 356 (-59.31%)
Mutual labels:  webpack, webpack-plugin
Webpack Sentry Plugin
Webpack plugin to upload source maps to Sentry
Stars: ✭ 299 (-65.83%)
Mutual labels:  webpack, webpack-plugin
Webpack Parallel Uglify Plugin
A faster uglifyjs plugin.
Stars: ✭ 456 (-47.89%)
Mutual labels:  webpack, webpack-plugin
Prerender Spa Plugin
Prerenders static HTML in a single-page application.
Stars: ✭ 7,018 (+702.06%)
Mutual labels:  webpack, webpack-plugin
Webpack Assets Manifest
This Webpack plugin will generate a JSON file that matches the original filename with the hashed version.
Stars: ✭ 269 (-69.26%)
Mutual labels:  webpack, webpack-plugin
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 (-67.31%)
Mutual labels:  webpack, webpack-plugin
Optimize Plugin
Optimized Webpack Bundling for Everyone. Intro ⤵️
Stars: ✭ 525 (-40%)
Mutual labels:  webpack, webpack-plugin

CommonJS Tree Shaker plugin for Webpack

NPM version Build Status

Fancy shaking logo

Please file an issue if anything is broken.

See common-shake for abstract bundler-independent implementation.

NOTE: webpack version 4 may be needed in order to run this. If you're using webpack 3, please use [email protected].

NOTE: Logo is a modified version of webpack's logo

Why?

There are vast amount of CommonJS modules out there. Thus CommonJS Tree Shaking is as important as the ESM module import/export shaking.

How?

This plugin removes unused assignments to exports properties leaving removal of the (presumably) dead code to UglifyJS. If, for example, you had a module:

exports.used = 1;
var tmp = exports.unused = 2;

This plugin will transform it to:

exports.used = 1;
var tmp = 2;

It is up to UglifyJS (or some other optimizer) to decide, whether tmp is used or not and delete it. Luckily it is much simpler for it to do if the uses are not clouded by exporting the values.

Usage

Example webpack.config.js:

const ShakePlugin = require('webpack-common-shake').Plugin;

module.exports = [{
  entry: 'entry.js'
  output: {
    path: 'dist',
    filename: 'output.js'
  },
  plugins: [ new ShakePlugin() ]
}];

Demonstration

See webpack-common-shake-demo for size comparison of output with and without this plugin.

Options

Plugin constructor accepts options object which may have following properties:

const plugin = new ShakePlugin({
  warnings: {
    global: true,
    module: false
  } /* default */,

  // Invoked on every deleted unused property
  onExportDelete: (resource, property) => {},

  // See `Limitations` section for description
  onModuleBailout: (module, bailouts) => { ... },
  onGlobalBailout: (bailouts) => { ... }
});

Limitations

Although, generally this module works and helps removing unused code from the bundles. There are some limitations that may prevent it from running either partially or completely. Some examples are provided below, otherwise please use common sense (or onModuleBailout, onGlobalBailout plugin options).

Some local (partial) bailouts:

  • Dynamic exports exports[Math.random()] = ...
  • Overriding imported vars var a = require('./a'); a.lib; a = require('./b')
  • Using require in unknown way console.log(require('./lib'))
  • Destructuring require dynamically { [prop]: name } = require('./a')
  • Dynamic import var fn = require('./lib')[Math.random()]

Some global (full) bailouts:

  • Dynamic use of require require(Math.random())

This plugin will print some webpack warnings. In any case, bailouts may be obtained programmatically too:

const plugin = new ShakePlugin({
  onModuleBailout: (module, bailouts) => { ... },
  onGlobalBailout: (bailouts) => { ... }
});

Graph

For debugging and inspection purposes a graph in dot format may be obtained using onGraph option:

const plugin = new ShakePlugin({
  onGraph: (graph) => { ... }
});

LICENSE

This software is licensed under the MIT License.

Copyright Fedor Indutny, 2017.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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].