【问题标题】:npm publish component with css and font带有 css 和字体的 npm 发布组件
【发布时间】:2017-02-03 04:38:49
【问题描述】:

我想在 NPM 上发布一个 Angular 2 组件。该组件使用引用某些字体的 css。至于发布.ts 文件,我可以做到这一点。但我对 css 和字体一无所知。

我有:

despotic-turtle.component.ts
despotic-turtle.component.css
fonts/font.ttf .svg ...

我的 css 中有这个:

@font-face {
  font-family: 'despotic-turtle';
  src:  url('despotic-turtle/fonts/font.eot?ggipqk');
  /* ....*/
}

我不能让css和字体保持原样,那该怎么办?

【问题讨论】:

    标签: node.js angular fonts npm webpack


    【解决方案1】:

    cssfontshtml 将包含在您的 npm publish 中。

    你会遇到的问题是,除非你在@Component中添加moduleId,否则找不到引用的css:

    @Component({
        moduleId: module.id
    })
    

    请注意,您必须使用 commonjs 进行编译才能使其正常工作。

    另外,您不应该将 .ts 文件发布到 npm,您应该发布的唯一文件是 index.jsyour-component.js + your-component.css + your-component.html.d.ts 文件和 font 文件.

    请记住,npm 仅用于发布转译文件,当您导入库时,您不应该编译它(检查 @angular 库,他们的库中没有 ts)。

    【讨论】:

    • 是的,您必须编译这些文件,但您不必将它们添加到发布中(将 *.ts 添加到您的 .npmignore 文件中)。 100% 肯定找不到您的字体,解决此问题的唯一方法是使用 cdn 或您自己托管文件以使用 url 添加它。
    • 问题是 moduleId 已添加到 Component 装饰器以解决此问题,但它不适用于 css 文件(您需要字体的地方)。一种方法是使用url("/node_modules/......") 导入字体,但我猜你已经知道并且不想这样做,因为嗯,它很难看!不幸的是,除了cdn存储之外,我认为您的问题没有解决方案。每个组件库都使用 cdn,这就是原因。
    猜你喜欢
    • 2020-03-19
    • 2016-11-04
    • 2016-09-26
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    相关资源
    最近更新 更多