Meteor package css relative resource path

前端 未结 1 1973
野的像风
野的像风 2021-01-14 04:18

How can I refer to an image file in a Meteor package from a CSS file in the same package, so that the image would be accessible after bundling.

相关标签:
1条回答
  • 2021-01-14 04:42

    Reference your image using package relative path, ie :

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

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

    Explicitly ask Meteor to bundle it on the client via the package system 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");
    });
    

    This way your package will be truly generic : users will just have to "mrt add" it to automatically serve your image to the client without messing with /public which is reserved for application-specific static files.

    As an example, consider a bootstrap3-glyphicons package :

    packages/
    -> bootstrap3-glyphicons/
    ----> bootstrap-glyphicons/ (3rd party files from Twitter Bootstrap)
    -------> css/
    ----------> bootstrap-glyphicons.css
    -------> fonts/
    ----------> glyphiconshalflings-regular.eor
    ----------> ...
    -------> bootstrap_override.css (our overriding to make it work the Meteor way)
    -------> 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'), ...
    }
    
    0 讨论(0)
提交回复
热议问题