webpack之DllPlugin和DllReferencePlugin使用的demo

建议在github阅读,文中内容会及时更新

1. webpack.DllPlugin

 "scripts": {
    "clean": "rimraf dist",//rimraf , oppsite of node-mkdirp
    "build:dll": "webpack --config webpack.dll.js",
    "watch": "npm run build:dll && webpack --config webpack.dev.js --watch --progress"
    //in watch mode, we will always build with webpack.dll.js to generate a new vendor.manifest.json for webpack DllReferencePlugin!
  }

webpack.dll.js as follows:

var path = require("path");
var webpack = require("webpack");
module.exports = {
    entry: {
        vendor: [path.join(__dirname, "client", "vendors.js")]
        //vendors.js is where for vendor files, it is also an entry file
    },
    output: {
        path: path.join(__dirname, "dist", "dll"),
        filename: "dll.[name].js",
        library: "[name]"
        //Combine this plugins with output.library option to expose the dll function i. e. into the global scope.
    },
    //output file is in dist/dll folder
    plugins: [
        new webpack.DllPlugin({
            path: path.join(__dirname, "dll", "[name]-manifest.json"),
            //The manifest is very important, it gives other Webpack configurations 
            //a map to your already built modules. Path represent where to generate manifest file
            name: "[name]",
            // the name is the name of the entry
            context: path.resolve(__dirname, "client")
            // context of requests in the manifest file, defaults to the webpack context
        }),
        // new webpack.optimize.OccurenceOrderPlugin(),
        // webpack2 has already built-in
        new webpack.optimize.UglifyJsPlugin()
    ],
    resolve: {
        // root: path.resolve(__dirname, "client"),
        // modulesDirectories: ["node_modules"]
        // webpack2 use modules to replace all this configurations
        modules:["node_modules",path.resolve(__dirname, "client")]
    }
};

Combine this plugins with output.library option to expose the dll function i. e. into the global scope.

vendor-manifest.json file contents:

{
  "name": "vendor",
  "content": {
    "./vendors.js": {
      "id": 0,
      "meta": {
        "harmonyModule": true
      }
    },
    //jquery
    "../node_modules/jquery/dist/jquery.js": {
      "id": 1,
      "meta": {}
    },
    //mustache
    "../node_modules/mustache/mustache.js": {
      "id": 2,
      "meta": {}
    }
  }
}

That is because we input jquery and mustache in our path.join(__dirname, “client”, “vendors.js”).

import mustache from 'mustache';
import jquery from 'jquery';

dist/dll.vendor.js will include both jquery and mustache file conent!

2. webpack.DllReferencePlugin

We can simply put our dll/vendor-manifest.json to webpack.DllReferencePlugin configuration.

 new webpack.DllReferencePlugin({
            context: path.join(__dirname, "client"),
            //same as DLLPlugin
            manifest: require("./dll/vendor-manifest.json")
        }),

.context: (absolute path) context of requests in the manifest (or content property)

.scope (optional): prefix which is used for accessing the content of the dll
manifest (object): an object containing content and name

.name (optional): the name where the dll is exposed (defaults to manifest.name) (see also externals)

.sourceType (optional): the type how the dll is exposed (defaults to “var”) (see also externals)

.content (optional): the mappings from request to module id (defaults to manifest.content)

SOURCE FILES:

OPTIMIZING WEBPACK FOR FASTER REACT BUILDS

rimraf

list-of-plugin

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值