“publicHost”选项将不会被使用,因为它不受“@angular-devkit/build-angular:browser-esbuild”构建器的支持

问题描述 投票:0回答:1

我有一个适用于 Angular 13 的 Angular 应用程序,我正在尝试升级到 Angular 18 并面临公共主机的问题。我需要一个自定义 URL 来启动 Angular 应用程序,该应用程序从 Tomcat 上托管的 Java 应用程序获取数据。

我使用以下命令在 Angular 13 中启动

**npm run start-d**

package.json

{
  "name": "dashboard",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start-d": "ng serve --host 0.0.0.0 --public-host dw.localhost.s.org:4200 a",

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "d": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "inlineStyle": true,
          "style": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [],
            "styles": [
              "node_modules/primeicons/primeicons.css",
              "node_modules/primeng/resources/themes/fluent-light/theme.css",
              "node_modules/primeng/resources/primeng.min.css",
              "node_modules/bootstrap/dist/css/bootstrap.css",
              "node_modules/@fortawesome/fontawesome-free/css/all.css",
              {
                "input": "./src/sass/static/css/styles.scss",
                "bundleName": "a_styles",
                "inject": true
              }
              
            ],
            "scripts": [],
            "vendorChunk": true,
            "extractLicenses": false,
            "buildOptimizer": false,
            "sourceMap": true,
            "optimization": false,
            "namedChunks": true
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            },
            "development": {}
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "proxyConfig": "proxy.conf.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "a:build:production"
            },
            "development": {
              "browserTarget": "a:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "a:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": ["src/styles.scss"],
            "scripts": [],
            "assets": ["src/favicon.ico", "src/assets"]
          }
        }
      }
    },

proxy.config.json

{
  "/a": {
    "target": "http://localhost:9080",
    "secure": false
  }
}

以上所有代码都适用于 Angular 13 版本

但是一旦我升级到 Angular 18,我的 api 调用就会失败并出现错误

HttpErrorResponse
error
: 
{error: SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON at JSON.parse (<anonymous>…, text: '<!doctype html>\r\n<html lang="en">\r\n<head>\n  <scrip…ain.js" type="module">\x3C/script></body>\r\n</html>\r\n'}
headers
: 
_HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message
: 
**"Http failure during parsing for http://localhost:4200/a/Action/P"**
name
: 
"HttpErrorResponse"
ok
: 
false
status
: 
200
statusText
: 
"OK"
url
: 
"http://localhost:4200/a/Action/P"
[[Prototype]]
: 
HttpResponseBase

Angular 18 package.json

{
  "name": "w-test",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "start-d": "ng serve --disable-host-check --port 4200 --public-host=dw.localhost.s.org:4200 a --host=0.0.0.0 ",

角度 18 角度.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "w-test": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "outputPath": "dist/w-test",
            "index": "src/index.html",
            "browser": "src/main.ts",
            "polyfills": [
              "zone.js"
            ],
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              {
                "glob": "**/*",
                "input": "public"
              }
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kB",
                  "maximumError": "1MB"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kB",
                  "maximumError": "4kB"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "optimization": false,
              "extractLicenses": false,
              "sourceMap": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "options": {
              "proxyConfig": "proxy.conf.json"
            },
            "production": {
              "buildTarget": "w-test:build:production"
            },
            "development": {
              "buildTarget": "w-test:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n"
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": [
              "zone.js",
              "zone.js/testing"
            ],
            "tsConfig": "tsconfig.spec.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              {
                "glob": "**/*",
                "input": "public"
              }
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    },

角度 18 proxy.config.json

{
  "/a/**": {
      "target": "http://dw.localhost:9080",
      "secure": false,
      "changeOrigin": true,
      "pathRewrite": {
          "^/a": ""
      }
  }
}

我尝试了一切,但无法弄清楚 Angular 18 构建系统发生了什么变化以及我需要做什么才能使其工作。

任何帮助将不胜感激。

javascript reactjs angular angular-cli angular18
1个回答
0
投票

从 Angular 18 文档开始,

public-host
选项在使用 esbuild 时没有任何效果。

public-host
The URL that the browser client (or live-reload client, if enabled) should use
to connect to the development server. Use for a complex dev server setup, such
as one with reverse proxies. This option has no effect when using the
'application' or other esbuild-based builders.

您可以尝试在 angular.json 中使用 webpack 构建器

"builder": "@angular-devkit/build-angular:application"
-->
"builder": "@angular-devkit/build-angular:browser"

以下是可供您参考的可用构建器列表。 https://www.npmjs.com/package/@angular-devkit/build-angular

© www.soinside.com 2019 - 2024. All rights reserved.