如何在meteor.js包中仅包含IE8浏览器的javascript文件?

时间:2014-09-28 15:26:29

标签: meteor

我想只为IE8浏览器添加一个javascript文件。例如:

  <!--[if lt IE 9]>
    <script src="js/ie/html5shiv.js"></script>
  <![endif]-->

我在package.js中使用以下代码作为我的meteor包:

Package.onUse(function (api) {
  api.versionsFrom("METEOR@0.9.2.2");
  api.use('jquery');
  var path = Npm.require('path');
  var asset_path = path.join('js);
  // Surround following code in <!--[if lt IE 9]> and <![endif]--> tags somehow!!!!
  api.addFiles(path.join(asset_path, 'ie', 'html5shiv.js'), 'client');
}

我知道最终的包将包含一个js文件中合并的所有js文件的缩小版本。在这种情况下,我希望代码看起来像:

<!--[if lt IE 9]>
    // Contents of html5shiv.js here!
<![endif]-->

1 个答案:

答案 0 :(得分:1)

这是我提出的解决方案,遗憾的是我无法测试它,因为我目前没有可用的Windows环境。

my-package/client/views/lib/head.html

<head>
    <!--[if lt IE 9]>
        <meta name="lower-than-ie9">
    <![endif]-->
</head>
Meteor Spacebars模板语法中的

headbody标签将被特别处理:您在这些标签之间放置的任何内容(它们可以多次出现)都会附加到最终的HTML文档中。

这就是为什么我们将这个Meteor模板代码添加到我们的包中:如果IE版本低于IE9,它最终将创建一个名为lower-than-ie9的元标记,我们稍后可以测试它的存在或不。

my-package/client/lib/my-package.js

Meteor.startup(function(){
  // search for the meta tag appended by the conditional inclusion comment
  if($("meta[name='lower-than-ie9']").length){
    // if it's there, load the script with jQuery
    $.getScript("/my-package/public/html5shiv.js");
  }
});

您需要将html5shiv.js脚本放在程序包的公共目录中:

my-package/public/html5shiv.js

这是您需要的包的结构:

my-package/package.js

Package.onUse(function(api){
  api.versionsFrom("METEOR@0.9.2.2");
  api.use("jquery","client");
  api.addFiles([
    "client/views/lib/head.html",
    "client/lib/my-package.js"
  ],"client");
  // specify that the script is an asset to prevent it from
  // being minimized and bundled to the client
  api.addFiles("public/html5shiv.js","client",{
    isAsset:true
  });
});