如何在metro.js包中包含仅用于IE8浏览器的javascript文件



我只想包含一个用于IE8浏览器的javascript文件。例如:

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

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

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]-->

这是我提出的解决方案,不幸的是,我无法测试它,因为我目前没有可用的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
  });
});

最新更新