On Github kirilknysh / js-modules-talk
by Kiril Knysh
utils.jsApp.UTILS = (function () {
var utils = {};
var STATE = { NAME_PREFIX: 'Sir ', SAL_PREFIX: '€' };
utils.getName = function (actor) { return STATE.NAME_PREFIX + $.trim(actor.name); } utils.getSalary = function (actor) { return STATE.SAL_PREFIX + actor.salary; }
return utils;
})();
utils/name.js(function (utils, $) {
var STATE = { PREFIX: 'Sir ' };
utils.getName = function (actor) { return STATE.PREFIX + $.trim(actor.name); }
})(App.UTILS = App.UTILS || {}, window.jQuery);
utils/salary.js(function (utils, $) {
var STATE = { PREFIX: '€' };
utils.getSalary = function (actor) { return STATE.PREFIX + actor.salary; }
})(App.UTILS = App.UTILS || {}, window.jQuery);
utils.jsvar $ = require('jQuery'); var STATE = { NAME_PREFIX: 'Sir ', SAL_PREFIX: '€' };
function getName(actor) { return STATE.NAME_PREFIX + $.trim(actor.name); } function getSalary(actor) { return STATE.SAL_PREFIX + actor.salary; }
module.exports.getName = getName; module.exports.getSalary = getSalary;
app.jsvar UTILS = require('utils');
UTILS.getName(actor);
utils.jsdefine(['jquery'] , function ($) {
var STATE = { NAME_PREFIX: 'Sir ', SAL_PREFIX: '€' };
return { getName: function (actor) { return STATE.NAME_PREFIX + $.trim(actor.name); }, getSalary: function (actor) { return STATE.SAL_PREFIX + actor.salary; } };
});
app.jsrequire(['utils'], function (UTILS) {
UTILS.getName(actor);
});
index.html <body> ... <script data-main="main" src="bower_components/requirejs/require.js"></script> </body>
main.jsrequirejs.config({ paths: { jquery: 'bower_components/jquery/dist/jquery' } }); requirejs(['scripts/app'], function(app) { app.run(); });
build.js({ baseUrl: ".", name: "main", out: "./build/main.js", paths: { jquery: "bower_components/jquery/dist/jquery.min" } })
utils.jsimport $ from 'jquery';
const STATE = { NAME_PREFIX: 'Sir ', SAL_PREFIX: '€' };
export function getName(actor) { return STATE.NAME_PREFIX + $.trim(actor.name); } export function getSalary(actor) { return STATE.SAL_PREFIX + actor.salary; }
app.jsimport { getName, getSalary } from 'utils';
getName(actor);
webpack.config.jsvar path = require('path'); module.exports = { entry: './scripts/app.js', output: { path: __dirname, filename: 'app.bundle.js' }, module: { loaders: [
{ test: path.join(__dirname, 'scripts'), loader: 'babel-loader' }
] } };
utils.jsexport const SEC_IN_MINUTE = 60;
export function getName(actor) { return actor.name || ''; }
export class Actor(name, salary) { this.name = name; this.salary = salary; }
app.jsimport { SEC_IN_MINUTE, getName } from 'utils';
import * as UTILS from 'utils'; const actor = new UTILS.Actor('Will Smith', 2000000);
super-sort.jsexport default function (array, predicate) { //super sort algorithm here } //no semicolon
Actor.jsexport default class (name, salary) { this.name = name; this.salary = salary; } //no semicolon
app.jsimport superSort from 'super-sort'; import SuperActor from 'Actor';
Actor.jsexport default class (name, salary) { this.name = name; this.salary = salary; }
export function getName(actor) { return actor.name || ''; }
app.jsimport Actor, { getName as getActorName } from 'Actor';
inline.jsexport const SEC_IN_MINUTE = 60; export function getName(actor) { return actor.name || ''; }
clause.jsconst SEC_IN_MINUTE = 60; function getName(actor) { return actor.name || ''; } export { SEC_IN_MINUTE, getName as getActorName };
utils.jsexport function getSalary(actor) { return actor.salary || -1; } export default function(actor) { return actor.name || ''; }
app.jsimport getName from 'utils'; import * as actorUtils from 'utils'; import { getSalary as getActorSalary } from 'utils';
utils.jsexport function getSalary(actor) { return actor.salary || -1; } export default function(actor) { return actor.name || ''; }
app.jsimport getName from 'utils'; import getName, * as actorUtils from 'utils'; import getName, { getSalary as getActorSalary } from 'utils';
utils.jsexport function getSalary(actor) { return actor.salary || -1; } export default function(actor) { return actor.name || ''; }
app.jsimport 'utils';
Actor.jsexport default class (name, salary) { this.name = name; this.salary = salary; }
app.jsconst mFreeman = new Actor('Morgan Freeman', 3000000);
import Actor from 'Actor';
visit-utils.jsexport let visits = 0; export function visitorDetected() { visits++; }
app.jsimport { visits, visitorDetected } from 'visit-utils'; console.log(visits); //0
visitorDetected();
console.log(visits); //1
System.import('some_module') .then(some_module => { // Use some_module }) .catch(error => { ··· });
System.module(source, options?)
System.set(name, module)
System.define(name, source, options?)