我想在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
}
我在哪里可以定义应传递给回调函数的配置?
答案 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)}`
}