【问题标题】:Meteor package css relative resource pathMeteor包css相对资源路径
【发布时间】:2015-03-21 02:45:30
【问题描述】:

如何从同一包中的 CSS 文件中引用 Meteor 包中的图像文件,以便在捆绑后可以访问该图像。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    使用包相对路径引用您的图像,即:

    /packages/my-package/css/my_css.css:

    .my-class{
        background-image:url('/packages/my-package/img/my_image.png');
    }
    

    明确要求 Meteor 通过包系统 API 将其捆绑到客户端:

    /packages/my-package/package.js :

    Package.on_use(function(api){
        var clientFiles=[
            // css
            "css/my_css.css",
            // img
            "img/my_image.png"
        ];
        api.add_files(clientFiles,"client");
    });
    

    这样你的包将是真正通用的:用户只需要“mrt add”它来自动将你的图像提供给客户端,而不会弄乱为特定于应用程序的静态文件保留的 /public。

    例如,考虑一个 bootstrap3-glyphicons 包:

    包/
    -> bootstrap3-glyphicons/
    ----> bootstrap-glyphicons/ (来自 Twitter Bootstrap 的第 3 方文件)
    -------> CSS/
    ----------> bootstrap-glyphicons.css
    -------> 字体/
    ----------> glyphiconshalflings-regular.eor
    ----------> ...
    -------> bootstrap_override.css (我们的覆盖使其以 Meteor 方式工作)
    -------> package.js
    --------> smart.json

    package.js:

    Package.on_use(function(api){
        api.use(["bootstrap3"]);//!
        //
        var clientFiles=[
            // css
            "bootstrap-glyphicons/css/bootstrap-glyphicons.css",
            // glyphicon fonts
            "bootstrap-glyphicons/fonts/glyphiconshalflings-regular.eot",
            ...
        ];
        api.add_files(clientFiles,"client");
        // this css file makes the paths to the icon absolute (/packages/bootstrap3-glyphicons)
        // it needs to be included AFTER the standard bootstrap css in order to take precedence.
        api.add_files("bootstrap_override.css","client");
    });
    

    bootstrap_override.css:

    @font-face{
        font-family:'Glyphicons Halflings';
        src:url('/packages/bootstrap3-glyphicons/bootstrap-glyphicons/fonts/glyphiconshalflings-regular.eot');
        src:url('/packages/bootstrap3-glyphicons/bootstrap-glyphicons/fonts/glyphiconshalflings-regular.eot?#iefix') format('embedded-opentype'), ...
    }
    

    【讨论】:

    • 这就是我现在使用的。我不喜欢 css 中的 /packages/my-package/...。 css 是第 3 方,我想避免对其进行更改。
    • 您不必直接在 3rd 方文件中进行更改,您可以覆盖 css 规则:请参阅我更新的示例。
    • 请注意,如果包在作者之后包含:,则会在公共资产路径中翻译为_
    猜你喜欢
    • 2016-09-10
    • 2010-12-08
    • 2011-12-15
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多