简体   繁体   English

流星CSS和JavaScript文件加载最佳做法

[英]Meteor css and javascript files loading best practice

TL,DR: How to load css and javascript files independent of Meteor assumptions about alphabetical order (which is far from how it works in practice.) TL,DR: 如何独立于流星关于字母顺序的假设加载CSS和javascript文件(与实际工作方式相去甚远)。

Stackoverflow tells me this question might be subjective but I hope not. Stackoverflow告诉我这个问题可能是主观的,但我希望不是。

Meteor loads files based on alphabetical order (and other rules.) 流星根据字母顺序(和其他规则)加载文件。
So to force it to load the CSS and JS files in the order I wanted, I had to start the fiels with numbers that indicate the load order. 因此,要强制它按我想要的顺序加载CSS和JS文件,我必须以表明加载顺序的数字开始该文件。 If I have jquery.js and bootstrap.js , Meteor will load bootstrap.js before jquery.js . 如果我有jquery.jsbootstrap.js ,Meteor将在jquery.js之前加载bootstrap.js But bootstrap depends on jquery so jquery must be loaded first. 但是引导程序取决于jquery,因此必须先加载jquery。

In order to solve this, the options are: 为了解决这个问题,选项是:
1. Put the files in the public directory and manually load them. 1.将文件放在public目录中,然后手动加载它们。 But this didn't work as Meteor appears to be sending the files with text/html MIME type. 但这不起作用,因为Meteor似乎正在发送具有text/html MIME类型的文件。
2. Create a Meteor package and specify the load order from there. 2.创建一个Meteor包,并从那里指定加载顺序。 I find this like hitting a fly with a hammer just for loading CSS and Javascript. 我发现这就像用锤子砸苍蝇只是为了加载CSS和Javascript。
3. Put a number before every file. 3.在每个文件前加一个数字。 In the previous example, to load jquery before bootstrap, rename the fiels to 1.jquery.js and 2.bootstrap.js This works and is tedious but at least I get to load the files the way I want them to. 在前面的示例中,要在引导程序之前加载jquery,请将文件名重命名为1.jquery.js2.bootstrap.js2.bootstrap.js有效又乏味,但至少我可以按照所需的方式加载文件。

I am new to Meteor so I am wondering if there are recommended best practices concerning this. 我是Meteor的新手,所以我想知道是否有关于此的推荐最佳实践。 I was thinking of using AMD for javascript but that's limited to javascript. 我当时在考虑将AMD用于javascript,但仅限于javascript。

Its an interesting question and this is probably one of the pitfalls of making a Meteor app. 这是一个有趣的问题,这可能是制作Meteor应用程序的陷阱之一。

You've mentioned all of the usable solutions such as creating an explicit package or renaming the files. 您已经提到了所有可用的解决方案,例如创建显式包或重命名文件。

The best way I would think is to use the atmosphere packages. 我认为最好的方法是使用大气包装。 For example if you add bootstrap, jquery is a dependency of it so it will always load first. 例如,如果添加引导程序,则jquery是它的依赖项,因此它将始终首先加载。 Most js libraries that involve load order are typically on atmosphere. 大多数涉及加载顺序的js库通常都处于大气中。

The other best way if there's no atmosphere package, though i'm not sure I would say is tedious is to put a number in front of the js file to indicate load order. 如果没有大气包,另一种最好的方法是,虽然我不确定我会说乏味,但是在js文件前放置一个数字以指示加载顺序。

One thing is when you use the /public folder the files map to / , so you can load the js file yourself manually in the order you would want (in the root html file using /public. Meteor returns the text/html MIME type as its version of a 404 file not found error. This method is a bit troublesome though because the files are seperated in production and can cause trouble if one or the other dont load. 一件事是,当您使用/public文件夹时,文件映射到/ ,因此您可以按照想要的顺序手动加载js文件(在使用/ public的html根文件中。Meteor返回text / html MIME类型为它的404文件版本未找到错误。此方法有点麻烦,因为文件在生产中是分开存放的,如果一个或另一个不加载,可能会造成麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM