【问题标题】:Webpack creating large file with small projectWebpack用小项目创建大文件
【发布时间】:2015-06-25 02:48:13
【问题描述】:

我让我的 webpack 生成了一个大型 main.js 文件 (1.7mb),其中包含一个大约 20-30 个文件的小项目,每个文件少于 100 行。所需的依赖项数量很少(React、Fluxible),我正在使用我能理解的每个优化插件:

module.exports = {
  output: {
    path: './build',
    publicPath: '/public/',
    filename: '[name].js'
  },
  debug: false,
  devtool: 'eval',
  target: 'web',
  entry: [
  'bootstrap-sass!./bootstrap-sass.config.js',
  './client.js',
  ],
  stats: {
    colors: true,
    reasons: false
  },
  resolve: {
    extensions: ['', '.js'],
    alias: {
      'styles': __dirname + '/src/styles',
      'components': __dirname + '/src/scripts/components',
      'actions': __dirname + '/src/scripts/actions',
      'stores': __dirname + '/src/scripts/stores',
      'constants': __dirname + '/src/scripts/constants',
      'mixins': __dirname + '/src/scripts/mixins',
      'configs': __dirname + '/src/scripts/configs',
      'utils': __dirname + '/src/scripts/utils'
    }
  },
  module: {
    loaders: [
      { test: /\.css$/, loader: 'style!css' },
      { test: /\.js$/, exclude: /node_modules/, loader: require.resolve('babel-loader') },
      { test: /\.json$/, loader: 'json-loader'},
      { test: /\.(png|svg|jpg)$/, loader: 'url-loader?limit=8192' },
      { test: /\.(ttf|eot|svg|woff|woff(2))(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url?name=/[name].[ext]"},
      { test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style-loader',
          'css!sass?outputStyle=expanded&' +
          "includePaths[]=" +
          (path.resolve(__dirname, "./node_modules"))
          )
      }
    ]
  },
  plugins: [
    new webpack.NoErrorsPlugin(),
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      "windows.jQuery": "jquery"
    }),
    new ExtractTextPlugin("[name].css", {allChunks: true}),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.AggressiveMergingPlugin()
  ],

};

我做错了什么或者我可以在哪里进一步改进文件的大小?

【问题讨论】:

    标签: webpack webpack-dev-server


    【解决方案1】:

    我的反应从 2.1mb 压缩到 160kb,只需在此处执行操作(devtools: 'source-map'),使用带有默认设置的 uglifyjs(没有 gzip,它最终约为 670kb)。

    它可能没有那么很棒,但至少它不再疯狂了。

    为了后代,这是我的 webpack 配置:

    // webpack.config.js
    var webpack = require('webpack');
    
    module.exports = {
        devtool: 'source-map',
        entry: [
            'webpack-dev-server/client?http://127.0.0.1:2992',
            'webpack/hot/only-dev-server',
            './js/main'
        ],
        output: {
            path: './out/',
            filename: 'main.js',
            chunkFilename: '[name]-[chunkhash].js',
            publicPath: 'http://127.0.0.1:2992/out/'
        },
        module: {
            loaders: [
                {
                    test: /\.jsx?$/,
                    exclude: /(node_modules|bower_components)/,
                    loaders: ['react-hot', 'babel?optional=runtime&stage=0&plugins=typecheck']
                }
            ]
        },
        progress: true,
        resolve: {
            modulesDirectories: [
                'js',
                'node_modules'
            ],
            extensions: ['', '.json', '.js']
        },
        plugins: [
            new webpack.HotModuleReplacementPlugin(),
            new webpack.NoErrorsPlugin(),
            new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
            new webpack.DefinePlugin({
                'process.env': {
                    // This has effect on the react lib size
                    'NODE_ENV': JSON.stringify('production'),
                }
            }),
            new webpack.optimize.UglifyJsPlugin()
        ]
    };
    

    【讨论】:

    • 我的项目产生的文件大小与您的相似,同样它并不疯狂,但是我的网站上几乎没有任何内容,所以我希望文件大小仍然小得多。我正在链接它;检查例如package.json 以查看您的项目是否还使用了可能占用大量空间的任何内容:github.com/amcsi/szeremi/tree/f93671a
    • 我为我的产品版本取出了 HotModuleReplacementPlugin
    【解决方案2】:

    你应该至少设置

    plugins: [
      new webpack.DefinePlugin({
        'process.env': {
          // This has effect on the react lib size
          'NODE_ENV': JSON.stringify('production'),
        }
      }),
      ...
    ],
    

    这将对 React 有很大帮助。

    此外,在生产环境中最好将devtool 设置为source-map。请参阅official documentation 了解更多信息。

    您可以尝试使用the analyse tool 检查输出。要获得它期望的 JSON,您需要执行类似 webpack --json > stats.json 的操作,然后将 stats.json 传递给该工具。这可能会给你一些见解。

    【讨论】:

    • 是的,我一定会看看这个,看看它如何影响代码。另外,我发现了我的错误,建议添加到答案中,我正在做devtools: true,这导致我的文件很大,而对于生产来说它不需要是真的。
    • 酷,我补充说。完全忘记了devtool
    • 另一种用于 webpack 包分析的命令行工具:github.com/robertknight/webpack-bundle-size-analyzer
    【解决方案3】:

    你也可以看看Webpack Bundle Analyzer

    它将捆绑内容表示为方便的交互式可缩放树状图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 2012-06-21
      • 1970-01-01
      • 2021-07-20
      相关资源
      最近更新 更多