gpt4 book ai didi

angular - 在 ng-packagr 构建中包含 json 文件

转载 作者:搜寻专家 更新时间:2023-10-30 21:36:40 26 4
gpt4 key购买 nike

我想将 json 文件导入到我的 Angular 5 应用程序中,然后构建一个库。我可以通过包含这段代码来完成第一部分

declare module "*.json" {
const value: any;
export default value;
}

typings.d.ts,但是当我尝试构建它时失败并出现错误

Cannot find module '../../assets/locale-en.json'.

有什么办法可以解决吗?

最佳答案

最后,我只是编写了将 json 文件转换为 ts 文件的脚本,并将其添加到 package.json 中用于 packagr 构建的脚本命令>.

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]);
}

关于angular - 在 ng-packagr 构建中包含 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50369248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com