【问题标题】:Build Angular Application for production为生产构建 Angular 应用程序
【发布时间】:2020-05-07 12:25:39
【问题描述】:

您好,我在构建用于生产的 Angular 应用程序时遇到了很大的困难。

我正在运行“ng build --prod”命令,但是当我打开“dist”文件夹时,我在那里看不到我的所有组件。还有什么我必须更改或配置的吗?打开“dist”文件夹时,我看到了一些组件。

angular.json

{
   "$schema":"./node_modules/@angular/cli/lib/config/schema.json",
   "version":1,
   "newProjectRoot":"projects",
   "projects":{
      "app":{
         "projectType":"application",
         "schematics":{
            "@schematics/angular:component":{
               "style":"scss"
            }
         },
         "root":"",
         "sourceRoot":"src",
         "prefix":"app",
         "architect":{
            "build":{
               "builder":"@angular-devkit/build-angular:browser",
               "options":{
                  "outputPath":"dist/app",
                  "index":"src/index.html",
                  "main":"src/main.ts",
                  "polyfills":"src/polyfills.ts",
                  "tsConfig":"tsconfig.app.json",
                  "aot":false,
                  "assets":[
                     "src/favicon.ico",
                     "src/assets",
                     "src/images"
                  ],
                  "styles":[
                     "src/styles.scss"
                  ],
                  "scripts":[

                  ]
               },
               "configurations":{
                  "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"
                        },
                        {
                           "type":"anyComponentStyle",
                           "maximumWarning":"6kb",
                           "maximumError":"10kb"
                        }
                     ]
                  }
               }
            },
            "serve":{
               "builder":"@angular-devkit/build-angular:dev-server",
               "options":{
                  "browserTarget":"app:build"
               },
               "configurations":{
                  "production":{
                     "browserTarget":"app:build:production"
                  }
               }
            },
            "extract-i18n":{
               "builder":"@angular-devkit/build-angular:extract-i18n",
               "options":{
                  "browserTarget":"app:build"
               }
            },
            "test":{
               "builder":"@angular-devkit/build-angular:karma",
               "options":{
                  "main":"src/test.ts",
                  "polyfills":"src/polyfills.ts",
                  "tsConfig":"tsconfig.spec.json",
                  "karmaConfig":"karma.conf.js",
                  "assets":[
                     "src/favicon.ico",
                     "src/assets"
                  ],
                  "styles":[
                     "src/styles.scss"
                  ],
                  "scripts":[

                  ]
               }
            },
            "lint":{
               "builder":"@angular-devkit/build-angular:tslint",
               "options":{
                  "tsConfig":[
                     "tsconfig.app.json",
                     "tsconfig.spec.json",
                     "e2e/tsconfig.json"
                  ],
                  "exclude":[
                     "**/node_modules/**"
                  ]
               }
            },
            "e2e":{
               "builder":"@angular-devkit/build-angular:protractor",
               "options":{
                  "protractorConfig":"e2e/protractor.conf.js",
                  "devServerTarget":"app:serve"
               },
               "configurations":{
                  "production":{
                     "devServerTarget":"app:serve:production"
                  }
               }
            }
         }
      }
   },
   "defaultProject":"app"
}

ts.config.json

{
   "compileOnSave":false,
   "compilerOptions":{
      "baseUrl":"./",
      "outDir":"./dist/out-tsc",
      "sourceMap":true,
      "declaration":false,
      "downlevelIteration":true,
      "experimentalDecorators":true,
      "module":"esnext",
      "moduleResolution":"node",
      "importHelpers":true,
      "target":"es2015",
      "typeRoots":[
         "node_modules/@types"
      ],
      "lib":[
         "es2018",
         "dom"
      ]
   },
   "angularCompilerOptions":{
      "fullTemplateTypeCheck":true,
      "strictInjectionParameters":true
   }
}

运行新构建时的输出:

我现在在本地控制台中运行时收到此错误:

加载资源失败:net::ERR_FILE_NOT_FOUND index.html:1 CORS 策略已阻止从源“null”访问“file:///C:/polyfills-es2015.0fe6949bc5ff4b784062.js”处的脚本:跨源请求仅支持协议方案:http,数据,铬,铬扩展,https。

【问题讨论】:

  • 检查 ng -> build -> outputPath 下 angular.json 文件中的路径。另外,检查相关位置。
  • 我检查了输出路径,一切似乎都是正确的,因为它显示为“dist/app”
  • 您可以使用示例应用程序和您的 angular.json 为您的应用程序创建 stackblitz 吗?
  • 您正在查看 out-tsc 文件夹,这是 TypeScript 在 WebPack 捆绑包之前使用的临时文件夹。请使用您的 angular.jsontsconfig 文件的内容更新您的问题。此外,在运行构建之前删除/dist,并使用ng build --prod 命令的输出更新您的问题。不要将这些作为图片发布!
  • 我删除了文件夹并运行了一个新版本并用结果更新了我原来的帖子

标签: javascript c# angular server angular-material


【解决方案1】:

当你运行ng build --prod 时,Angular 会做一些动作(粗略的描述):

  • 运行模块捆绑器(例如 WebPack)。
  • WebPack 将查看所有模块并创建依赖关系树。然后基于依赖树的 WebPack 将在最终文件(包)中仅包含使用的组件。这称为摇树。
  • 然后发生丑化。 Uglify 表示删除所有不必要的空格,缩短变量名

所以这就是您看不到任何组件以及无法调试捆绑文件的原因。

【讨论】:

    【解决方案2】:

    是的,它应该是这样的,你有 index.html、favicon.ico、一个 css 文件和一堆 .js 文件。它们的名字很奇怪,因为 Angular 以这种方式“编译”它们。如果您访问您的网站,它将正常工作

    【讨论】:

    • 上传到iis后我只看到一个空白页
    • 您可以先从浏览器访问您的 index.html 在本地尝试吗?对于 IIS,您需要一些额外的步骤(创建网站、安装 URL 重写模块、创建 web.config 文件等...我发现这篇博客文章包含一些步骤sundeepkamath.in/posts/…
    • 是的,我在本地运行时得到相同的结果,所以编译过程中有问题
    • 好的,在这种情况下,在浏览器上打开控制台,您可能会收到一些消息(F12 在大多数浏览器上都可以打开开发者工具并查看控制台)
    • 我用我收到的控制台错误更新了我的原始帖子
    猜你喜欢
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 2020-04-18
    • 2019-07-13
    • 2021-05-11
    • 2020-12-22
    相关资源
    最近更新 更多