在ng-packagr版本中包含json文件 [英] Include json file in ng-packagr build
本文介绍了在ng-packagr版本中包含json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将json文件导入到我的Angular 5应用中,然后构建一个lib.我可以通过包含以下代码来完成第一部分
I want to import json file into my Angular 5 app and then build a lib. I can do the first part by including this code
declare module "*.json" {
const value: any;
export default value;
}
到 typings.d.ts
,但是当我尝试构建它时,它会失败并显示错误
to typings.d.ts
, but when I try to build it it fails with error
Cannot find module '../../assets/locale-en.json'.
有什么办法可以解决?
推荐答案
最后,我只是编写了将 json
文件转换为 ts
文件并将其添加到脚本的脚本用于在 package.json
中构建packagr的命令.
In the end I just wrote script to convert json
file to ts
file and added it to script command for packagr build in package.json
.
package.json
scripts: {
"packagr": "node ./scripts/update-component-lib-locale-json.js && ng-packagr -p ng-package.json && rm -rf lib/node_modules && rm lib.tgz"
}
脚本文件
#!/usr/bin/env node
var fs = require('fs');
var Mustache = require('mustache');
var locales = [
'en',
];
/**
* Updates the component library's locale JSON with the shared locale JSON
* of the parent project.
*
* NOTE: This has been implemented as a workaround for not being able to
* dynamically load the shared locale JSON file in the component library's
* `locale-<locale>.ts` file. This seems to be a limitation of `ng-packagr` and
* could be resolved in future releases, or by someone smarter than me ;)
*
* @param {string} locale - A two-character locale string (e.g. 'en').
*/
var updateComponentLibLocaleJSON = function (locale) {
locale = locale.toLowerCase().trim();
// Location of the component locale definition file for this locale (required by `ng-packagr`).
var componentLibLocaleJSONPath = './src/components/locale-' + locale + '.ts';
// Location of the generic locale definition file Mustache template.
var componentLocaleJSONTemplatePath = './src/components/locale.ts.mustache';
// Location of the shared locale JSON file for this locale.
var sharedLocaleJSONPath = './src/assets/locale-' + locale + '.json';
// Read the shared JSON for this locale, render the Mustache template with
// that JSON and save the output to the component lib's locale JSON file for
// this locale.
var sharedLocaleJSON = fs.readFileSync(sharedLocaleJSONPath).toString();
var componentLocaleJSONTemplate = fs.readFileSync(componentLocaleJSONTemplatePath).toString();
var updatedComponentLocaleJSON =
Mustache.render(componentLocaleJSONTemplate, { localeJSON: sharedLocaleJSON });
fs.writeFileSync(componentLibLocaleJSONPath, updatedComponentLocaleJSON);
};
for (var i = 0; i < locales.length; i++) {
updateComponentLibLocaleJSON(locales[i]);
}
这篇关于在ng-packagr版本中包含json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文