如何在angular-cli中将配置传递到indexTransform中?

时间:2019-08-08 19:35:36

标签: angular angular-cli angular-cli-v8

我想在angular 8(angular-cli)项目中使用indexTransform功能,但是targetOptions的配置属性仍然为空。

我在这里尝试了示例项目:https://github.com/just-jeb/angular-builders/tree/master/packages/custom-webpack/examples/full-cycle-app

但是在示例项目中,配置也为空。

module.exports = (targetOptions, indexHtml) => {
    targetOptions.configuration // configuration is empty
}

我在哪里可以定义应传递给回调函数的配置?

1 个答案:

答案 0 :(得分:0)

配置反映了在ng build中传递给--configuration = configurationName 选项的值,因此在开发模式下始终为空。

如果使用--prod标志(即--configuration = production的别名)进行构建,则可以看到配置正确填充

您可以在下面找到一个用法示例。这段代码在head元素中添加了具有api服务位置的meta标签元素。

const allSettings = {
    development: {
        apiUrl: "http://localhost:8000"
    },
    production: {
        apiUrl: "https://myapiserver/"
    }
}

module.exports = (targetOptions, indexHtml) => {
    const i = indexHtml.indexOf('</head>');

    // if empty assumes 'development'
    const configuration = targetOptions.configuration || "development";

    // load settings (by configuration)    
    const settings = allSettings[configuration];

    // build meta tag
    let customHead = "";
    customHead += `<meta name="apiUrl" content="${setting.apiUrl}">`;

    // add customHead before head end tag
    return `${indexHtml.slice(0,i)}
            ${customHead}
            ${indexHtml.slice(i)}`
}