繁体   English   中英

与angular-cli相比,使用webpack + angular无法达到相同水平的捆绑优化

[英]Unable to achieve the same level of bundle optimization with webpack + angular as compared to angular-cli

我有两个相同的Angular项目,一个是angular-cli,另一个是使用@ ngtools / webpack的Webpack版本。 两个项目都运行Angular 7.1.4和@ angular-devkit 0.13.5。 Angular代码是使用angular-cli生成的初始应用程序模块/组件。

我遇到的问题是Webpack版本正在生成450 KB的压缩应用程序捆绑包,但是angular-cli的捆绑包是238 KB。 如果没有TerserPlugin,则angular-cli的应用程序捆绑包为1.51 MB,Webpack的输出为1.62 MB。

对于angular-cli,我使用以下配置运行“ ng build --prod = true”:

"production": {
  "fileReplacements": [
    {
      "replace": "src/environments/environment.ts",
      "with": "src/environments/environment.prod.ts"
    }
  ],
  "optimization": true,
  "outputHashing": "all",
  "sourceMap": false,
  "extractCss": true,
  "namedChunks": false,
  "aot": true,
  "extractLicenses": true,
  "vendorChunk": false,
  "buildOptimizer": true,
  "budgets": [
    {
      "type": "initial",
      "maximumWarning": "2mb",
      "maximumError": "5mb"
    }
  ]
}

这是我的Webpack配置:

{
    mode: 'production',
    entry: {
        app: './src/main',
    },
    output: {
        path: path.resolve(__dirname, 'public'),
        filename: "[id].[chunkhash].js",
        chunkFilename: "[id].[chunkhash].js",
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.mjs', '.js'],
    },
    module: {
        rules: [
            {
                test: /\.html$/,
                loader: 'raw-loader',
            },
            {
                test: /\.scss$/,
                use: [
                    'to-string-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: false
                        }
                    },
                    {
                        loader: 'sass-loader',
                        options: {
                            sourceMap: false
                        }
                    },
                ]
            },
            {
                test: /\.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)$/,
                loader: 'file-loader',
            },
            {
                test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
                loader: '@ngtools/webpack'
            },
            {
                test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/,
                parser: { system: true },
            },
            {
                test: /\.js$/,
                use: [
                    {
                        loader: '@angular-devkit/build-optimizer/webpack-loader',
                        options: { sourceMap: false },
                    },
                ],
            },
        ]
    },
    optimization: {
        minimizer: [
            new TerserPlugin({
                cache: true,
                terserOptions: {
                    compress: {
                        pure_getters: true,
                        passes: 3,
                        global_defs: {
                            ngDevMode: false,
                        },
                    },
                    output: {
                        ascii_only: true,
                        comments: false,
                        safari10: true,
                        webkit: true,
                    },
                },
            })
        ],
        providedExports: true,
        usedExports: true,
        concatenateModules: true,
        runtimeChunk: true,
    },
    plugins: [
        new AngularCompilerPlugin({
            tsConfigPath: path.resolve(__dirname, './tsconfig.json'),
            entryModule: path.resolve(__dirname, './src/app/app.module#AppModule'),
            mainPath: path.resolve(__dirname, './src/main.ts'),
        })
    ]
};

从BundleAnalyzerPlugin的捆绑包来看,两个项目的node_modules统计信息大小均为1.52 MB,但是angular-cli的解析大小为238 KB,而Webpack为441 KB。 据我所知,即使包(例如rxjs)是同一版本,node_modules仍有大约100 KB的差异。

角-CLI:

的WebPack:

这是怎么回事,如何在两个项目中达到相同的优化水平?

看起来Webpack项目中的问题是tsconfig的“模块”设置为“ commonjs”。 将其更改为“ es2015”,就像在angular-cli应用程序中将其默认设置一样,现在可以将应用程序编译为248 KB。

暂无
暂无

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

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