在 CLI 中为应用相对路径使用自定义 Environment.ts 值

Using Custom Environment.ts Value for app relative paths in the CLI

我开发了一个 angular2 应用程序 - 没有 cli,现在正尝试将它移到 cli 中以利用内置的测试和构建过程。

我遇到的问题是我开发的应用程序将部署在网络内容管理系统(即 DNN、drupal、joomla 等...)中。所以应用程序不能假设它的文件相对于网站目录的根目录。 (我目前通过两个不同的配置文件用 systemjs 解决了这个问题)

例如,从 cli 构建 dist 文件夹中的 index.html 文件如下所示:

<body>
    <app-root>Loading...</app-root>
    <script type="text/javascript" src="inline.js"></script>
    <script type="text/javascript" src="styles.bundle.js"></script>
    <script type="text/javascript" src="main.bundle.js"></script>
</body>

但我需要它看起来像这样:

<body>
    <app-root>Loading...</app-root>
    <script type="text/javascript" src="/DesktopModules/MyApp/dist/inline.js"></script>
    <script type="text/javascript" src="/DesktopModules/MyApp/dist/styles.bundle.js"></script>
    <script type="text/javascript" src="/DesktopModules/MyApp/dist/main.bundle.js"></script>
</body>

如果我手动进行这些更改,那么该应用程序可以运行。 (我只移动了第一个 / 根组件,预计会出现更多问题。)

我很乐意创建一个 gulp 任务来执行此操作,但希望有一种方法可以让 angular-cli

处理这些差异

一个想法:

如果我将自定义环境文件添加到 app/environment 文件夹 (environment.cms.ts) 即:

export const environment = {
    production: false,
    envName: 'cms',
    dnnPath: '/DesktopModules/cms/'
};

有没有办法使用环境文件其他setting/option告诉cli当我使用ng build --prod --env=cms 添加我在上面显示的第二个 index.html 文件脚本路径中的路径。

提前致谢

JK


PS: 我不确定这是否是最好的地方。我不觉得这是一个错误,所以不想在 github 存储库中创建问题。人们讨论这些类型的事情还有其他方式吗?

好的,

如果我更仔细地准备文档,我会找到我的答案 看到这个 link to the docs

它指出:

Base tag handling in index.html

When building you can modify base tag () in your index.html with --base-href your-url option.

Sets base tag href to /myUrl/ in your index.html

ng build --base-href /myUrl/ 
ng build --bh /myUrl/