从Webpack DLL构build中排除模块

我有一个Webpack构build一个通用的JavaScript应用程序。 我正在使用DLL插件预先构build我的所有node_modules。 我添加了一个导致DLL生成错误的库(见下文)。

我可以添加一个JSON加载器来解决这个问题。 但是我根本不需要React代码中的lib。 我将它添加到了我的排除列表中,但仍然出现错误。

这是错误:

Building the Webpack DLL... Hash: a69a927bfa72ddef88d5 Version: webpack 2.1.0-beta.15 Time: 7152ms Asset Size Chunks Chunk Names reactBoilerplateDeps.dll.js 5.58 MB 0 [emitted] reactBoilerplateDeps chunk {0} reactBoilerplateDeps.dll.js (reactBoilerplateDeps) 5.07 MB [rendered] [1135] dll reactBoilerplateDeps 12 bytes {0} [built] + 1137 hidden modules ERROR in ./~/constants-browserify/constants.json Module parse failed: /Users/steve/Projects/elucidate/node_modules/constants-browserify/constants.json Unexpected token (2:12) You may need an appropriate loader to handle this file type. | { | "O_RDONLY": 0, | "O_WRONLY": 1, | "O_RDWR": 2, @ ./~/graceful-fs/polyfills.js 2:16-36 

Webpack DLL构build脚本:

 const { join } = require('path'); const defaults = require('lodash/defaultsDeep'); const webpack = require('webpack'); const pkg = require(join(process.cwd(), 'package.json')); const dllPlugin = require('../config').dllPlugin; if (!pkg.dllPlugin) { process.exit(0); } const dllConfig = defaults(pkg.dllPlugin, dllPlugin.defaults); const outputPath = join(process.cwd(), dllConfig.path); module.exports = { context: process.cwd(), entry: dllConfig.dlls ? dllConfig.dlls : dllPlugin.entry(pkg), devtool: 'eval', output: { filename: '[name].dll.js', path: outputPath, library: '[name]', }, node: { fs: "empty", }, plugins: [ new webpack.DllPlugin({ name: '[name]', path: join(outputPath, '[name].json') }), // eslint-disable-line no-new ], }; 

来自package.json的DLL插件configuration:

 "dllPlugin": { "path": "node_modules/react-boilerplate-dlls", "exclude": [ "chalk", "compression", "cross-env", "express", "ip", "minimist", "sanitize.css", "multiparty", "cloudinary", "winston", "morgan", "body-parser", "request-promise", "winston-graylog2", "yauzl", "busboy", "graceful-fs" ], "include": [ "core-js", "lodash", "eventsource-polyfill" ] }, 

注意:这仅用于反应样板。

要从DllPlugin中排除模块,您需要添加模块(或者在您的排除序列数组中标有其依赖项的模块):

 excludes = { "constants-browserify", ... } 

如果需要注意的是,如果您没有直接安装常量 – browserify模块,您将需要find已标记为依赖项的模块。

另外,正如你所说,如果你想加载模块,那么你需要指定一个加载器的.json文件,DllPlugin试图parsing:

地点:

 module: { loaders: [ { test: /\.json$/, loader: 'json-loader', } ], }, 

在你的里面

 module.exports = { ... } 

这将允许WebPack正确parsing.json文件。

我想你可以用IgnorePluginignore-loader来排除一些模块。

https://webpack.github.io/docs/list-of-plugins.html#ignoreplugin

https://github.com/cherrry/ignore-loader