Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@yued/autodll-webpack-plugin

Package Overview
Dependencies
Maintainers
6
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@yued/autodll-webpack-plugin

[![Build Status](https://travis-ci.org/asfktz/autodll-webpack-plugin.svg?branch=master)](https://travis-ci.org/asfktz/autodll-webpack-plugin)

  • 0.3.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
6
Weekly downloads
 
Created
Source

Build Status

AutoDllPlugin

Webpack's DllPlugin without the boilerplate

npm install --save-dev autodll-webpack-plugin


Table of contents

Introduction

Webpack's own DllPlugin it great, it can drastically reduce the amount of time needed to build (and rebuild) your bundles by reducing the amount of work needs to be done.

If you think about it, most of the code in your bundles come from NPM modules that you're rarely going to touch. You know that, but Webpack doesn't. So every time it compiles it has to analyze and build them too - and that takes time.

The DllPlugin allows you to to create a separate bundle in advance for all of those modules, and teach Webpack to reference them to that bundle instead.

That leads to a dramatic reduction in the amount of time takes Webpack to build your bundles.

For example, these are the measurements for the performance test that you can find in the examples folder:

Without DllPluginWith DllPlugin
Build Time16461ms - 17310ms2991ms - 3505ms
DevServer Rebuild2924ms - 2997ms316ms - 369ms

The DllPlugin sounds great! So why AutoDllPlugin?

While the DllPlugin has many advantages, it's main drawback is that it requires a lot of boilerplate.

AutoDllPlugin serves as a high-level plugin for both the DllPlugin and the DllReferencePlugin, and hides away most of their complexity.

When you build your bundle for the first time, the AutoDllPlugin Compiles the DLL for you, and references all the specified modules from your bundle to the DLL.

The next time you compile your code, AutoDllPlugin will skip the build and read from the cache instead.

AutoDllPlugin will rebuild your DLLs every time you change the Plugin's configuration, install or remove a node module.

When using Webpack's Dev Server, the bundle are loaded into the memory preventing unnecessary reads from the FileSystem.

With the way the DLLPlugin works, you must load the DLL bundles before your own bundle. This is commonly accomplished by adding an additional script tag to the HTML.

Because that is such a common task, AutoDllPlugin can do this for you (in conjunction with the HtmlPlugin ).

plugins: [
  new HtmlWebpackPlugin({
    inject: true,
    template: './src/index.html',
  }),
  new AutoDllPlugin({
    inject: true, // will inject the DLL bundles to index.html
    filename: '[name]_[hash].js',
    entry: {
      vendor: [
        'react',
        'react-dom',
        'moment'
      ]
    }
  })
]

Will Result in:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Test</title>
</head>
<body>

  ...

  <script src="dist/vendor.dll.js"></script>
  <script src="dist/main.bundle.js"></script>
</body>
</html>

Basic Usage (example):

const webpack = require('webpack');
const path = require('path');
const AutoDllPlugin = require('autodll-webpack-plugin');

module.exports = {
  entry: {
    app: './src/index.js'
  },

  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
    publicPath: '/'
  },

  plugins: [
    new AutoDllPlugin({
      filename: '[name].dll.js',
      entry: {
        vendor: [
          'react',
          'react-dom',
          'moment'
        ]
      }
    })
  ]
};

While it's not required, using AutoDllPlugin together with HtmlWebpackPlugin is highly recommended, because its saves you the trouble of manually adding the DLL bundles to the HTML by yourself.

Use AutoDllPlugin's inject option to enable this feature.

const webpack = require('webpack');
const path = require('path');
const AutoDllPlugin = require('autodll-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',

  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  },

  plugins: [
    new HtmlWebpackPlugin({
      inject: true, // will inject the main bundle to index.html
      template: './src/index.html',
    }),
    new AutoDllPlugin({
      inject: true, // will inject the DLL bundle to index.html
      debug: true,
      filename: '[name]_[hash].js',
      path: './dll',
      entry: {
        vendor: [
          'react',
          'react-dom',
          'moment'
        ]
      }
    })
  ]
};

Options

OptionTypeDefaultDescription
entryObject{}

The entry points for the DLL's.
Think of it as the entry option in your webpack config.
Each entry point represents a DLL bundle and expects an array of modules.

entry: {
    // Create a DLL from NPM modules:
    vendor: [
      'react',
      'react-dom',
      'moment',
      'lodash'
    ],
    // Create a DLL from a part of your app
    // that you rarely change:
    admin: [
        './src/admin/index.js'
    ]
}
if you have multiple vendor and one has depend on another, for example, redux depend react:
entry: {
    vendor: [
      'react',
      'react-dom',
      'moment',
      'lodash'
    ],
    vendorRedux: [
      'redux',
      'react-redux'
    ]
}
you must make sure the keys in entry in order by the dependcy, like follow:
entry: {
    vendor0: [
      'react',
      'react-dom',
      'moment',
      'lodash'
    ],
    vendor1Redux: [
      'redux',
      'react-redux'
    ]
}
filenameString"[name].js"

The filename template.
Same as webpack's output.filename.

Examples:

  • [name]_[hash].dll.js
  • [id].bundle.js
contextString

process.cwd()

The base directory, an absolute path, for resolving entry points and loaders from the configuration.

Same as webpack's context

It is very important to make sure the context is set correctly,
otherwise, you'll end up with having the same modules both in the DLL bundles and in your main bundles!

Most of the time, the defaults (the current directory) should work for you, here's how it should work:

If your webpack config is stored at the base of your project:

~/my-project/webpack.config.js

Set it up like this:

{
  context: __dirname
}

If your webpack config is stored in a nested directory:

~/my-project/config/webpack.config.js

It should look like this:

{
  context: path.join(__dirname, '..')
}
injectBooleanfalse

By setting inject to true, AutoDLL will inject the DLL bundles into the HTML for you.

Note: HtmlWebpackPlugin is required for this feature to work.

pathString"" The path for the DLL bundles, relative to webpack's output.publicPath
debugBooleanfalseUse debug mode to see more clearly what AutoDLL is doing.
pluginsArray[]

Plugins for the DLL compiler. Same as webpack's plugins.

plugins: [
  new webpack.optimize.UglifyJsPlugin()
]

FAQ

I added my dependencies to the DLL, and now, when I make a change to one of them I don't see it! Why?

When you run webpack for the first time, AutoDLL builds the DLL bundles and stores them in the cache for next time.

That leads to faster builds and rebuilds (using webpack's dev server).

There are two conditions for triggering a new build on the next run:

  1. Running npm install / remove / update package-name (or Yarn equivalent).
  2. Changing the plugin's configurations.

For performance considerations, AutoDLL is not aware of any changes made to module's files themselves.

So as long as you intend to work on a module, just exclude it from the DLL.

For example, let's say you configured the plugin like so:

new AutoDllPlugin({
  entry: {
    vendor: [
      'react',
      'react-dom',
      'moment'
    ]
  }
})

And then, while working on your project, you encountered some weird behavior with moment and decided to put a console.log statement in one of its files to see how it behaves.

As explained above, AutoDLL is not going to invalidate its cache in this case, and you might get surprised that you don't see the changes.

To fix that, all you have to do is comment out moment from the DLL, and uncomment it when you're done.

new AutoDllPlugin({
  entry: {
    vendor: [
      'react',
      'react-dom'
     // 'moment'
    ]
  }
})

The modules I added to the DLL are duplicated! They included both in the DLL bundle AND the main bundle.

That is most likely caused by using an incorrect context.

AutoDLL will try its best to set the context for you, but as with webpack's own context property, sometimes it is better to do it manually.

The context property should be an absolute path, pointing the base of your project.

For example, let's consider a project structured like so:

my-project
├── node_modules
│   └── react
│   └── react-dom
├── src
│   └── index.js
│   └── module.js
├── webpack.config.js
└── package.json

Then, inside webpack.config.js, You'll setup the context like so:

__dirname;   // '/Users/username/my-project'

...

new AutoDllPlugin({
  context: __dirname,
  entry: {
    vendor: [
      'react',
      'react-dom'
    ]
  }
})

Note that the __dirname variable is node's way to get the absolute path of the current module's directly, which is exactly what we need because webpack.config.js stored in the base of our project.

On the other hand, let's say your project is structured like so:

my-project
├── node_modules
│   └── react
│   └── react-dom
├── src
│   └── index.js
│   └── module.js
├── config
│   └── webpack.config.js
└── package.json

Notice that now our config is no longer stored at the base of our project, but in a subdirectory of its own.
That means that now we have to subtract the relative path to our config file from __dirname.

We can use node's path module to help us with that:

var path = require('path');

__dirname;                   // '/Users/username/my-project/config'
path.join(__dirname, '..');  // '/Users/username/my-project'

...

new AutoDllPlugin({
  context: path.join(__dirname, '..'),
  entry: {
    vendor: [
      'react',
      'react-dom'
    ]
  }
})

If you still encounter an issue with the context set up correctly, please open an issue. I'll be happy to help you.

Running Examples

  1. git clone git@github.com:asfktz/autodll-webpack-plugin.git
  2. cd autodll-webpack-plugin
  3. npm install
  4. cd examples/recommended
  5. npm install
  6. npm start or npm run build

FAQs

Package last updated on 14 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc