简体   繁体   English

webpack 包大小与 requirejs 包大小

[英]webpack bundle size vs requirejs bundle size

I'm trying to migrate a requireJS based app to webpack.我正在尝试将基于 requireJS 的应用程序迁移到 webpack。

This app doesn't have many dependencies - actually it only needs a promise polyfill - and I've already figured out how to make webpack using the minified one.这个应用程序没有很多依赖项——实际上它只需要一个 promise polyfill——而且我已经想出了如何使用缩小的 webpack 来制作 webpack。

The bundle size with requireJS used to be 43KB, when using webpack it's 121KB.使用 requireJS 的包大小曾经是 43KB,使用 webpack 时是 121KB。

While 121KB isn't really huge it is a notable size increase.虽然 121KB 并不是很大,但它是一个显着的大小增加。

From running webpack --display-reasons --display-modules I have learned that there seems to be some node_module dependencies included in my bundle.通过运行webpack --display-reasons --display-modules我了解到我的包中似乎包含一些 node_module 依赖项。 Way more than I expected.比我预期的要多得多。

I see things like buffer , readable-stream , stream-http , stream-browserify , core-util-is , buffer-shims , ...我看到诸如bufferreadable-streamstream-httpstream-browserifycore-util-isbuffer-shims ……

Is this expected / part of the webpack wrapper code?这是预期的/ webpack 包装器代码的一部分吗?

Is there anything I can do to exclude these dependencies?我可以做些什么来排除这些依赖项?

This is my webpack.config.js:这是我的 webpack.config.js:

 var webpack = require('webpack'); module.exports = { entry: { "mynexuz": "./js/mynexuz-api.js", "kws": "./js/kws-api.js", "main": "./js/main.js", "quest": "./js/quest.js" }, output: { filename: "./dist/[name]-bundle.js", }, plugins: [ new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }), new webpack.DefinePlugin({ 'process.env': { 'NODE_ENV': JSON.stringify('production'), } }) ], node: { //stream: false, //process: false, //global: false }, // Enable sourcemaps for debugging webpack's output. devtool: "source-map", resolve: { modules: ['js', 'js/lib', 'node_modules'], // Add '.ts' and '.tsx' as resolvable extensions. extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"] }, module: { loaders: [ // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. { test: /\\.js$/, loader: "source-map-loader", exclude: /node_modules/ }, // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'. { test: /\\.tsx?$/, loader: "awesome-typescript-loader", exclude: /node_modules/ } ] }, };

This doesn't work for all libraries you are using, but when possible you can save on file size by only importing the actual function/component you need to use.这不适用于您正在使用的所有库,但如果可能,您可以通过仅导入您需要使用的实际功能/组件来节省文件大小。

Here is an example with lodash这是一个 lodash 的例子

import has from 'lodash/has';

That way above will ONLY import the has method.上面的那种方式只会导入has方法。

However if you do either of the following:但是,如果您执行以下任一操作:

import { has } from 'lodash';

Or或者

import _ from 'lodash';

Then you will import ALL of the lodash library which will bump up your file size.然后您将导入所有 lodash 库,这将增加您的文件大小。

However with other libraries (ie current version of moment.js) it's not so simple to import just the PART of the library you need.然而,对于其他库(即当前版本的 moment.js),仅导入您需要的库的一部分并不是那么简单。

There are a few other ways to try to solve this problem (ie tweaking your webpack settings) but I would start with this method.还有其他一些方法可以尝试解决这个问题(即调整你的 webpack 设置),但我会从这个方法开始。

After looking deeper into the issue I've found the reason for the large size of the bundle.在深入研究这个问题后,我找到了捆绑包过大的原因。 In true requireJS style I had:在真正的 requireJS 风格中,我有:

define(['http', 'config'], function (Http, Config) { ... });

This 'http' thing was supposed to refer to my own library, but webpack resolved this to some NPM module, bringing in all the aforementioned dependencies.这个“http”的东西应该是指我自己的库,但是 webpack 将它解析为一些 NPM 模块,引入了所有上述依赖项。

I've now changed the code to:我现在已将代码更改为:

define(['./http', 'config'], function (Http, Config) { ... });

And the bundle sizes are back to around 44KB.并且包的大小又回到了 44KB 左右。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM