首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ng-packagr构建中包括json文件。

在ng-packagr构建中包括json文件。
EN

Stack Overflow用户
提问于 2018-05-16 11:05:52
回答 2查看 1K关注 0票数 2

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

代码语言:javascript
复制
declare module "*.json" {
    const value: any;
    export default value;
}

,但是当我试图构建它时,它会出现错误。

代码语言:javascript
复制
Cannot find module '../../assets/locale-en.json'.

有办法解决吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-21 06:28:41

最后,我编写了脚本将json文件转换为ts文件,并将其添加到package.json中packagr的script命令中。

package.json

代码语言:javascript
复制
scripts: {
 "packagr": "node ./scripts/update-component-lib-locale-json.js && ng-packagr -p ng-package.json && rm -rf lib/node_modules && rm lib.tgz"
}

dcript文件

代码语言:javascript
复制
#!/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]);
}
票数 2
EN

Stack Overflow用户

发布于 2018-08-24 19:38:21

试着替换

代码语言:javascript
复制
import * as locale from '../../assets/locale-en.json';

使用

代码语言:javascript
复制
const locale = require('../../assets/locale-en.json');

在你的tsconfig.lib.json里

代码语言:javascript
复制
{
  "compilerOptions": {
    ...
   "declaration": false,
    ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50369248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档