首页 > 解决方案 > 在 Visual Studio 中以本地生产模式构建 Angular

问题描述

我想在本地测试我的 Angular 应用程序的生产版本(即从dist文件夹中提供服务),使用 Visual Studio(作为 .net 核心应用程序的一部分)&& IIS Express - 即获得F5为生产版本提供服务的行为。

我认为这就像添加--prodstart(and/or build) 一样简单,但这不起作用,来自package.json

"scripts": {
    "ng": "ng",
    "start": "ng serve --live-reload false --prod",
    "build": "ng build --prod",
    "build:ssr": "ng run Website:server:dev",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
},

我怎样才能让它正常工作?

标签: angularvisual-studiovisual-studio-2019

解决方案


不完全是F5,但到目前为止我最接近的是使用 Powershell 脚本:

cls

# build angular in prod to get compiled site (including service worker, etc) in dist folder
Set-Location -Path C:\Users\Sean\Desktop\ngsw\ngsw\ClientApp
ng build --prod

# iis params
$iisExpressExe = '"C:\Program Files\IIS Express\iisexpress.exe"'
$path = "C:\Users\Sean\Desktop\ngsw\ngsw\ClientApp\dist"
Write-host "Starting site on port: 8080"
$params = "/port:8080 /path:$path"
$command = "$iisExpressExe $params"

# open chrome in incognito to avoid cache 
[System.Diagnostics.Process]::Start("chrome.exe","--incognito http://localhost:8080/")

# start iis
cmd /c start cmd /k "$command"
Write-Host "Site started"

# sleep a long time...
Start-Sleep -m 1000

推荐阅读