Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a bower package as an ES6 module?

I'm trying to migrate an ember app to use the ember app-kit. The code requires the accounting.js library. In the pre-app-kit version the file was loaded via a script tag in index.html

<script src="http://cdnjs.cloudflare.com/ajax/libs/accounting.js/0.3.2/accounting.min.js"></script>

and accessed in the views through the global namespace

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

In the app-kit version, I've included accounting.js as a bower dependency. In 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"
  }
 }

When I try to build the app, it gives the error

W117: 'accounting' is not defined.

I understand why this is and know I need some sort of import accounting from ... statement.

How do I import a package installed via bower as an ES6 module?

like image 734
Tom Close Avatar asked Jan 29 '14 18:01

Tom Close


People also ask

What are modules in ES6?

ES6 comes to your rescue with the concept of Modules. A module organizes a related set of JavaScript code. A module can contain variables and functions. A module is nothing more than a chunk of JavaScript code written in a file. By default, variables and functions of a module are not available for use.

What is the use of export variable in ES6?

Variables and functions within a module should be exported so that they can be accessed from within other files. Modules in ES6 work only in strict mode. This means variables or functions declared in a module will not be accessible globally. The export keyword can be used to export components in a module.

What is ES6 and why should I Care?

ES6 is the first time that JavaScript has built-in modules ( source ). TL;DR — If you want to see a practical example where we create a dashboard using ES6 modules from an architectural design layout, jump to section 4.

Is it possible to register new Bower packages?

As Bower is deprecated, registering new Bower packages is not supported anymore. Neverthless you can install any GitHub repository as Bower package by putting full name in bower.json:


1 Answers

I know that this was asked a few months ago, but since then, Ember App Kit has been succeeded by ember-cli, and this provides a very straight forward means to access either bower or npm dependencies.

  • Non-AMD asset
  • AMD asset

With regards to being accessed as ES6 modules:

  • Non-AMD assets cannot be accessed as an ES6 module, you simply access them through the global variable that they export.
    • e.g. moment
  • AMD assets, on the other hand, can be accessed through the ES6 import syntax
    • e.g. import { raw as icAjaxRaw } from 'ic-ajax';

Worth also mentioning, that ember-cli supports an add-on system now, which can make importing these things as simple as adding them to the package.json of your project. Some of the more popular libraries already have ember-cli addons for them. This post describes how you can write your own.

like image 56
bguiz Avatar answered Oct 11 '22 15:10

bguiz