luxembourgjs / js-write-once-run-anywhere

lightning talk

Home Page:http://krampstudio.com/js-write-once-run-anywhere/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Java[Script] : Write once run anywhere

  • The Java lang arguments

The promise of node.js

  • But some early attempts like Jaxer, Mozilla Rhino, etc.
  • The same code on browsers, servers, microcontrollers, etc.

How to write a library that can be integrated in any environments ?

Which version of the languages

Old school

NO MODULES

  • export to global context

Declaration :

var aja = {
 awesome : function(){
    console.log('awesome');
 }
};
window.aja = window.aja || aja;

Usage :

window.aja.awesome();

Common JS

  • runs on node.js, io.js, browserify

Declaration :

var aja = {
 awesome : function(){
    console.log('awesome');
 }
};
module.exports = aja;

Usage :

var aja = require('aja');
aja.awesome();

AMD

  • require.js, almond, etc.

Declaration :

define([], function(){
  var aja = {
   awesome : function(){
      console.log('awesome');
   }
  };
  return aja;
});

Usage :

require(['aja'], function(aja){
  aja.awesome();
});

ES6 imports

  • standard

Declaration :

var aja = {
 awesome : function(){
    console.log('awesome');
 }
};
export default aja;

Usage :

import aja from 'aja';
aja.awesome();

All together

(function(){
'use strict';
  var aja = {
   awesome : function(){
      console.log('awesome');
   }
  };

  //AMD
  if (typeof define === 'function' && define.amd) {
    define([], function(){  //don't name it
      return aja;
    });

  //CommonJs
  } else if (typeof exports === 'object') {
    module.exports = aja;

  //Old school
  } else {
  window.aja = window.aja || aja;
  }

 });

Or even better at https://github.com/umdjs/umd

But where are ES6 modules?

Imposible to feature detect (at least for now), see http://stackoverflow.com/questions/27922232/how-to-feature-detect-es6-modules

Going further

Building a generator that bundle the code for each system:

  • a generic one (using UMD)
  • AMD
  • CJS
  • Old School
  • ES6

I am not going to maintain 5 versions of my source code So, automate it dude

Creation of a Grunt task : grunt-exportify

About

lightning talk

http://krampstudio.com/js-write-once-run-anywhere/


Languages

Language:HTML 52.1%Language:CSS 47.9%