【问题标题】:add defer / async in production bundle using angular cli 7使用 Angular cli 7 在生产包中添加延迟/异步
【发布时间】:2019-02-22 20:42:48
【问题描述】:

我在使用 Angular cli 7 生产的新网站中遇到了一些性能问题,专门用于移动浏览器,经过检查后我发现性能不佳的主要原因是它缺少为 angular cli 生成的 javascript 包中的 pre/async .

我想知道是否有任何替代方法可以使用 angular cli 7 以便在最终包中添加延迟/异步,我尝试搜索并发现旧 angular cli 版本的许多替代方案包括一个功能建议但没有对于 news 版本,因为从 Angular 版本 6 开始,它无法弹出 webpack 配置和自定义、添加插件等。

【问题讨论】:

    标签: angular webpack


    【解决方案1】:

    angular cli 部分没有神奇的解决方案,但是我发现自定义构建器对我很有效。

    https://www.npmjs.com/package/@angular-builders/custom-webpack#custom-webpack-config-object

    angular-cli.json

      "build": {
              "builder": "@angular-builders/custom-webpack:browser",
              "options": {
                "outputPath": "dist/browser",
                "index": "src/index.html",
                "main": "src/main.ts",
                "tsConfig": "src/tsconfig.app.json",
                "polyfills": "src/polyfills.ts",
                "assets": [
                  "src/assets",
                  "src/favicon.ico"              
                ],
                "styles": [
                  "node_modules/bootstrap/dist/css/bootstrap.min.css",
                  "node_modules/photoswipe/dist/photoswipe.css",
                  "node_modules/photoswipe/dist/default-skin/default-skin.css",
                  "src/styles.scss"
                ],
                "scripts": [],
                "customWebpackConfig": {
                  "path": "./webpack-extra.config.js",
                  "mergeStrategies": {"plugins": "replace"}             
                }
              },

    webpack-extra.config

    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
    const CompressionPlugin = require('compression-webpack-plugin');
    
    module.exports = {
        plugins: [
            new HtmlWebpackPlugin({
                     "template": "./src\\index.html",
                     "filename": "./index.html",
                     "hash": false,
                     "inject": true,
                     "compile": true,
                     "favicon": false,
                     "minify": {
                       "caseSensitive": true,
                       "collapseWhitespace": true,
                       "keepClosingSlash": true,
                       "removeComments": true,
                       "removeRedundantAttributes": true
                     },
                     "cache": true,
                     "showErrors": true,
                     "chunks": "all",
                     "excludeChunks": [],
                     "title": "Webpack App",
                     "xhtml": true,
                     "chunksSortMode": "none"
                   }),
            new ScriptExtHtmlWebpackPlugin({
                defaultAttribute: 'defer'
              }),
            new CompressionPlugin({
              test: /\.js(\?.*)?$/i          
            })
        ]
    };

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 2017-05-17
      • 1970-01-01
      相关资源
      最近更新 更多