NPM包:如何引用包内的资源文件?

7

我的当前设置

我创建并发布了一个npm包(使用typescript编写),其中内部引用了位于public/fonts文件夹中的一些字体文件。

然后,在Angular应用程序中安装此包。

安装包后,在node_modules中的文件结构如下:

 node_modules/
   mypackage/
     dist/
       index.d.ts
       index.js
     public/
       fonts/
     LICENSE
     README.md
     package.json
     tsconfig.json

我正在将index.js中的字体文件引用为:
'Roboto': {
   normal: 'public/fonts/Roboto-Regular.ttf',
   bold: 'public/fonts/Roboto-Bold.ttf',
   italics: 'public/fonts/Roboto-Italic.ttf',
   bolditalics: 'public/fonts/Roboto-BoldItalic.ttf'
}

以上在开发包时没有出现错误

当前问题

但是现在,在我安装、导入并使用该包时,它会抛出以下错误:

Error: ENOENT: no such file or directory, open 'public/fonts/Roboto-Bold.ttf'

观察

如果我将公共文件夹放在使用该包的应用程序的根目录下,它将按预期工作。这意味着该包正在查找应用程序根目录中的public/fonts/,而不是包内部的应用程序根目录

有什么办法可以引用包内的那些文件,并确保在开发包时不会出错吗?

此外,我还在我的包的tsconfig.json中遇到了以下错误:

[ts] No inputs were found in ...../mypackage/tsconfig.json. Specified 'include' paths were [**/*] and 'exclude' paths were [./dist].

对于上述问题,在包的根目录中添加一个空的.ts文件可以解决它,但是否有其他替代方法?

编辑(添加我的包的tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "noImplicitAny": false,
    "lib": [
      "es2015"
    ]
  }
}
2个回答

5

在你的read me文件中,你可以引导用户按照Angular CLI中stories asset configuration所解释的方式向angular json文件添加资产配置:

你可以使用这个扩展配置从项目外部复制资产。例如,你可以从一个node包中复制资产:

"assets": [
  { 
    "glob": "**/*", 
    "input": "./node_modules/some-package/images", 
    "output": "/some-package/" 
  }
]

所以,在您的情况下应该是这样的:
"assets": [
  { 
    "glob": "**/*", 
    "input": "../node_modules/mypackage/public", 
    "output": "/public/" 
  }
]

node_modules的路径应该相对于你的应用程序根目录。

从NG6开始,你可以使用Angular CLI命令中的ng add来安装npm模块并在angular json文件中进行必要的更新。你需要在你的模块中实现ng add的原理,你可以找到像Angular materialprimeng schematics等的示例...


谢谢。这是否意味着我需要编辑包中的相对路径 index.js,将其更改为 normal: 'dist/public/fonts/Roboto-Regular.ttf' - Nikhil Nanjappa
如果您能帮我解决与包的 tsconfig.json 相关的另一个问题,我将不胜感激。该文件在 ...../mypackage/tsconfig.json 中报错:[ts] No inputs were found in ...../mypackage/tsconfig.json. Specified 'include' paths were [**/*] and 'exclude' paths were [./dist]。我已经更新了问题,展示了当前的包 tsconfig.json - Nikhil Nanjappa
针对上述问题,在包的根目录添加一个空白的 .ts 文件可以解决它,但是否有其他替代方法? - Nikhil Nanjappa
原始问题是关于在安装和导入包后获取包资源的问题。我提出的唯一必要步骤是修改你的Angular应用程序的.angular-cli.json文件。这解决了你的问题吗?关于其他问题,你可以从另一个Angular库中学习,例如ngx-leaflet(https://github.com/Asymmetrik/ngx-leaflet),或者探索ng6库原理图(https://github.com/angular/angular-cli/tree/master/packages/schematics/angular/library/files/__projectRoot__)。如果你仍然需要帮助,请将你的整个代码提交到GIT,我会尽力帮助。 - Andriy
是的,虽然只有一点小变化,但这解决了我的问题。input 必须是 ../node_modules/mypackage/public不是 ./node_modules/mypackage/public 。如果你能做出这个更改,我会接受你的答案。谢谢。此外我的全部代码可以在这里找到。 - Nikhil Nanjappa

0
如果你遇到了错误信息 '[ts] No inputs were found in ...../mypackage/tsconfig.json. Specified 'include' paths were [**/*] and 'exclude' paths were [./dist]',请检查你的项目文件结构。我曾经也遇到过这个问题,后来发现是我不小心把我想要用作输出目录(在我的ts.config中指定)的dist文件夹嵌套到了src文件夹中。我只需要确保文件在项目文件结构中放置正确,问题就得到了解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接