Meteor package, how to add static files
Asked Answered
P

2

18

I'm creating a package and, for the client side, I need to add some static files like fonts and images. After trying some outdated solution I found nothing seemed to work for me. How should I add those files? Create a public folder inside my package? Adding the files with api.addFiles? Is this even possible?

Picrate answered 10/6, 2014 at 14:23 Comment(1)
excellent question, have you raised an issue to get this documented ?Osteoma
A
30

update: meteor 1.2

You should now use api.addAssets to add static files to your package.


original answer

You can add static assets to any package and they will be served by meteor. The easiest way to demonstrate this is with an example. Have a look at the source for hopscotch.

The package contains an img directory with the file sprite-green-0.3.png. If you look at the package.js file you can see that it gets added to the client with:

api.addFiles('img/sprite-green-0.3.png', 'client');

After adding the package to your project, you can access the file directly with this URL:

http://localhost:3000/packages/hopscotch/img/sprite-green-0.3.png

In summary, you can use api.addFiles to add static assets. All assets will be accessable under a path like /packages/[package name]/[path to asset].

Note that you can add {isAsset: true} as a third argument to addFiles for assets which should not be automatically loaded. This post contains an example of its use.

Akee answered 10/6, 2014 at 22:5 Comment(5)
You should use {isAsset: true} for CSS and JS files that you don't want auto-loaded on the client.Darvon
If your package has a name with a ":" in it (as most username-namespaced packages do now) it gets translated to an underscore. An image living in "public/img/sprite.png" in "author:packagename" will be found at "/packages/author_packagename/public/img/sprite.png".Voe
@Voe thank you so much, this is what we were missing. I wish this were better documented, it's funny how this small comment was the missing link.Thalassic
nice, this iis a pretty important feature actually and I'm disapointed at how long it took us to find this answer, thank you so much!Osteoma
is there a way to pass the list of assets from the package.json into the package code itself? You often define which assets to add in the package.json but then need to run through a loop to load them inside runtime code.Elka
A
0

For assets file like favicon.ico or fonts files you can create a public folder.

You can check this answer or the documentation.

Adame answered 18/6, 2018 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.