我的当前设置
我创建并发布了一个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"
]
}
}
index.js
,将其更改为normal: 'dist/public/fonts/Roboto-Regular.ttf'
? - Nikhil Nanjappatsconfig.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.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,我会尽力帮助。 - Andriyinput
必须是../node_modules/mypackage/public
而 不是./node_modules/mypackage/public
。如果你能做出这个更改,我会接受你的答案。谢谢。此外我的全部代码可以在这里找到。 - Nikhil Nanjappa