Code Monkey home page Code Monkey logo

es6-module-loader's Introduction

ES6 Module Loader

An ES6 Module Loader polyfill based on http://wiki.ecmascript.org/doku.php?id=harmony:module_loaders by Luke Hoban, Addy Osmani and Guy Bedford.

Not yet suitable for production use while the specification is still subject to change.

Download

Getting Started

Check-out the demo sample to see the project in action.

Use the System (pre-configured Loader):

System.baseURL = '/lib';
System.import('js/test1', function (test1) {
  console.log('test1.js loaded', test1);
});

where, test1 can contain module syntax:

test1.js:

export function tester() {
  console.log('hello!');
}

Load multiple modules:

System.import(['js/test1', 'js/test2'], function(test1, test2) {
  console.log('test1.js loaded', test1);
  console.log('test2.js loaded', test2);
}, function(err) {
  console.log('loading error');
});

Load a plain JavaScript file from a URL:

System.load('js/libs/jquery-1.7.1.js', function() {
  var $ = System.global.jQuery;
  console.log('jQuery loaded', $);
  $('body').css({'background':'blue'});
});

Define a new module Loader instance:

var loader = new Loader(Loader, {
  global: window,
  strict: false,
  resolve: function (normalized, options) {
    return '/' + normalized + '.js';
  },
  fetch: function (url, fulfill, reject, options) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          fulfill(xhr.responseText);
        } else {
          reject(xhr.statusText);
        }
      }
    };
    xhr.open("GET", url, true);
    xhr.send(null);
  },
  translate: function (source, options) {
    return source;
  }
});

Define an ES6 module programatically (useful in optimized / production environments):

var module = new Module({ test: 'hello' });
System.set('my-module', module);
console.log(System.get('my-module'));

Notes and roadmap

Syntax Parsing

The Esprima ES6 Harmony parser is being used to do parsing, loaded only when necessary.

The following module statements are currently supported:

import 'jquery';                        // import a module
import $ from 'jquery';                 // import the default export of a module
import { $ } from 'jquery';             // import a named export of a module
import { $ as jQuery } from 'jquery';   // import a named export to a different name

export var x = 42;                      // export a named variable
export function foo() {};               // export a named function

export default var x = 42;              // export the default export
export default function foo() {};       // export the default export as a function
export default = function foo() {};     // export the default export by assignment

export { encrypt };                     // export an existing variable
export { decrypt as dec };              // export a variable as a new name
export { encrypt as en } from 'crypto'; // export an export from another module
export * from 'crypto';                 // export all exports from another module

module 'crypto' { ... }                 // define a module

Specification Notes

The polyfill is implemented exactly to the specification, except where areas are currently under debate.

The only feature which is not possible to fully polyfill is the intrinsics functionality and sandboxing of the loader. Custom builtins and full global encapsulation is still provided.

To follow the current the specification changes, see https://github.com/ModuleLoader/es6-module-loader/issues?labels=specification&page=1&state=open.

Projects using us

  • JSPM Loader is a RequireJS-style loader using our polyfill to load ES6, AMD, CommonJS and global modules

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Also, please don't edit files in the "dist" subdirectory as they are generated via grunt. You'll find source code in the "lib" subdirectory!

Release History

(Nothing yet)

License

Copyright (c) 2012 Luke Hoban, Addy Osmani, Guy Bedford
Licensed under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.