如何作为ES6模块访问bower包?

Tom*_*ose 10 ember.js bower ecmascript-6 ember-app-kit

我正在尝试迁移一个ember应用程序来使用ember app-kit.该代码需要accounting.js库.在pre-app-kit版本中,文件是通过脚本标签加载的index.html

<script src="http://cdnjs.cloudflare.com/ajax/libs/accounting.js/0.3.2/accounting.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

并通过全局命名空间在视图中访问

App.MoneyField= Em.TextField.extend({
  init: function() {
    this._super();
    var value = accounting.formatMoney(this.get("money") / 100, '');
    this.set('value', value);
  };
  // other functions omitted
});
Run Code Online (Sandbox Code Playgroud)

在app-kit版本中,我将其accounting.js作为一个凉亭依赖项包含在内.在bower.json:

{
  "name": "ember-app-kit",
  "dependencies": {
    "handlebars": "~1.1.2",
    "jquery": "~1.9.1",
    "qunit": "~1.12.0",
    "ember": "~1.4.0-beta.2",
    "ember-data": "~1.0.0-beta.6",
    "ember-resolver": "git://github.com/stefanpenner/ember-jj-abrams-resolver.git#master",
    "ic-ajax": "~0.3.0",
    "ember-testing-httpRespond": "~0.1.1",
    "accounting":"~0.3.2"
  },
  "resolutions": {
    "ember": "~1.4.0-beta.2"
  }
 }
Run Code Online (Sandbox Code Playgroud)

当我尝试构建应用程序时,它会给出错误

W117: 'accounting' is not defined.
Run Code Online (Sandbox Code Playgroud)

我明白为什么会这样,并知道我需要某种import accounting from ...陈述.

如何将通过bower安装的软件包导入为ES6模块?

bgu*_*uiz 1

我知道几个月前有人问过这个问题,但从那时起,Ember App Kit 已经被ember-cli继承,这提供了一种非常直接的方法来访问 Bower 或 npm 依赖项。

关于作为 ES6 模块访问:

  • 非 AMD 资产无法作为 ES6 模块访问,您只需通过它们导出的全局变量来访问它们。
    • 例如moment
  • 另一方面,AMD 资产可以通过 ES6import语法 访问
    • 例如import { raw as icAjaxRaw } from 'ic-ajax';

还值得一提的是,ember-cli 现在支持附加系统,这可以使导入这些东西就像将它们添加到package.json项目中一样简单。一些更流行的库已经为其提供了 ember-cli 插件。这篇文章描述了如何编写自己的文章。