Angular项目Firebase部署遇No i18n config报错,目录配置求助
问题详情
执行ng build --configuration production构建Angular项目后,运行firebase deploy --only hosting时出现报错:
Thank you for trying our early preview of Angular support on Firebase Hosting.
During the preview, support is best-effort and breaking changes can be expected. Proceed with caution.Documentation: https://firebase.google.com/docs/hosting/frameworks/angular
File a bug: https://github.com/firebase/firebase-tools/issues/new?template=bug_report.md
Submit a feature request: https://github.com/firebase/firebase-tools/issues/new?template=feature_request.mdWe'd love to learn from you. Express your interest in helping us shape the future of Firebase Hosting: https://goo.gle/41enW5X
Error: No i18n config on project.
已安装Transloco 5.0.7,根据Firebase文档要求,需在firebase.json中配置i18n根目录:
"i18n": { "root": "/localized-files" },
且该目录需位于public目录下(即public/localized-files/)。但当前项目的i18n文件存放在源码目录src/app/assets/i18n/,尝试了多种路径配置:
"i18n": { "root": "src/app/assets/i18n/" },
"i18n": { "root": "app/assets/i18n/" },
"i18n": { "root": "/assets/i18n/" },
"i18n": { "root": "assets/i18n/" },
均出现相同类型报错:
hosting: Couldn't find specified i18n root directory /assets/i18n in public directory .
疑问:firebase.json中是否有配置项可以指定Angular的index.html位于app目录而非public目录?
解决方案
核心逻辑
Firebase Hosting部署的是Angular构建后的产物,而非源码文件,所以i18n配置路径必须基于构建产物的根目录(即firebase.json中hosting.public指定的目录),而非源码目录src。
具体操作
确认i18n文件已被复制到构建产物
Angular默认会将src/assets下的文件同步到构建产物目录,执行ng build --configuration production后,src/app/assets/i18n会被复制到构建产物的assets/i18n目录下。配置Firebase的根目录与i18n路径
在firebase.json的hosting节点中,通过public字段指定Angular的构建产物目录,同时配置i18n路径为构建产物中的相对路径:- 若将Angular构建输出目录改为
public(在angular.json的architect.build.options.outputPath中设置):"hosting": { "public": "public", "i18n": { "root": "assets/i18n" } // 其他Hosting配置... } - 若使用Angular默认构建输出目录(
dist/[你的项目名]):"hosting": { "public": "dist/your-project-name", "i18n": { "root": "assets/i18n" } // 其他Hosting配置... }
- 若将Angular构建输出目录改为
验证并重新部署
构建完成后,检查构建产物目录下是否存在assets/i18n文件夹,确认文件已同步完成,再重新运行firebase deploy --only hosting即可。
内容的提问来源于stack exchange,提问作者Thomas David Kehoe

