limurezzz@gmail.com
var MODULE = (function () {
var my = {},
privateVariable = 1;
function privateMethod() {
// ...
}
my.moduleProperty = 1;
my.moduleMethod = function () {
// ...
};
return my;
}());
define('counterModule', function(){
var count = 0;
return {
add: function(){
return count++;
},
reset: function(){
count = 0;
},
get: function(){
return count;
}
}
});
module id is usually left empty
define('viewModule', ['counterModule'], function(counter){
var views = {};
return {
register: function(viewName, viewData){
counter.add();
views[viewName] = viewData;
},
count: function(){
return counter.get();
}
....
};
});
//loading module
var myModule = require(‘../myModule’);
//declaring module
module.exports = myModule;
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define('myModule', ['pkg/A', 'pkg/B'], function (A, B) {
return factory(A, B);
});
} else if (typeof exports === 'object') {
// Node.js
module.exports = factory(require('pkg/A'), require('pkg/B'));
} else {
// Browser globals
root.myModule = factory(root.A, root.B);
}
}(this, function (B, B) {
var myModule = {};
return myModule;
}));
// default export
export default MyModule;
import MyModule from './MyModule';
/* named exports */
export subModule;
import {subModule} from './MyModule';
Tracuer
// correct
export let one = 1;
// also correct
let two = 2;
export {two};
// also correct
export {one, two};
// also correct
export {one as once, two as twice};
// class export
export class User {
constructor(name) {
this.name = name;
}
};
// function export
export function sayHi() {
alert("Hello!");
};
// or separately from definition
export {User, sayHi}
// no name - error!
export function() { alert("Error"); };
// nums.js
export let one = 1;
export let two = 2;
// module.js
import {one, two} from "./nums";
alert( `${one} and ${two}` ); // 1 and 2
// import one with name item1,
// two – with name item2
import {one as item1, two as item2} from "./nums";
alert( `${item1} and ${item2}` ); // 1 and 2
// import everything from nums.js
import * as numbers from "./nums";
// exported values are part of numbers
alert( `${numbers.one} and ${numbers.two}` );
// 1 and 2
// default class export
export default class User {
constructor(name) {
this.name = name;
}
};
import User from './user';
new User("Andrei");
// if user.js had
export class User { ... }
// then you would use curly brackets:
import {User} from './user';
new User("Andrei");
export const foo = 'bar'
/*
* import {foo} from './foo'
* console.assert(foo === 'bar')
*/
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var foo = exports.foo = 'bar';
/*
* import {foo} from './foo'
* console.assert(foo === 'bar')
*/
NodeJS (2009)
AMD (2011)
Browserify (2014)
2014
July 2013
siteroot/
js/
app.js
require.js
jquery.js
mymodule.js
index.html
<script data-main="/js/app" src="/js/require.js"></script>
<script src="scripts/require.js"></script>
<script>
require.config({
baseUrl: "/another/path",
paths: {
"some": "some/v1.0"
},
waitSeconds: 15
});
require( ["some/module", "my/module", "a.js", "b.js"],
function(someModule, myModule) {
//This function will be called when all the dependencies
//listed above are loaded. Note that this function could
//be called before the page is loaded.
//This callback is optional.
}
);
</script>
node r.js -convert path/to/commonjs/modules/ path/to/output
It is part of the RequireJS project, and works with the RequireJS implementation of AMD.
npm install -g browserify
browserify js/main.js -o js/bundle.js -d
var _ = require('underscore'),
names = ['Bruce Wayne', 'Wally West', 'John Jones', 'Kyle Rayner', 'Arthur Curry', 'Clark Kent'],
otherNames = ['Barry Allen', 'Hal Jordan', 'Kara Kent', 'Diana Prince', 'Ray Palmer', 'Oliver Queen'];
_.each([names, otherNames], function(nameGroup) {
findSuperman(nameGroup);
});
function findSuperman(values) {
_.find(values, function(name) {
if (name === 'Clark Kent') {
console.log('It\'s Superman!');
} else {
console.log('... No superman!');
}
});
}
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){
var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);
if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");
throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};
t[o][0].call(l.exports,function(e){var n=t[o][1][e];
return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}
var i=typeof require=="function"&&require;
for(var o=0;o<r.length;o++)s(r[o]);
return s})({1:[function(require,module,exports){
var _ = require('underscore'),
names = ['Bruce Wayne', 'Wally West', 'John Jones', 'Kyle Rayner', 'Arthur Curry', 'Clark Kent'],
otherNames = ['Barry Allen', 'Hal Jordan', 'Kara Kent', 'Diana Prince', 'Ray Palmer', 'Oliver Queen'];
_.each([names, otherNames], function(nameGroup) {
findSuperman(nameGroup);
});
function findSuperman(values) {
_.find(values, function(name) {
if (name === 'Clark Kent') {
console.log('It\'s Superman!');
} else {
console.log('... No superman!');
}
});
}
"devDependencies": {
"browserify": "latest",
"watchify": "latest"
},
"scripts": {
"build-js": "browserify js/main.js > js/findem.js",
"watch-js": "watchify js/main.js -o js/findem.js"
}
package.json
Because life is too short for CLI commands
({
paths: {
requireLib: '../third-party/almond'
},
include: ['requireLib', 'router/front-page-router', 'router/challenge-page-router'],
baseUrl: '.',
mainConfigFile: 'loader.js',
name: 'loader',
out: 'loader-min.js',
})
'events', 'stream', 'path', 'url', 'assert', 'buffer', 'util',
'querystring', 'http', 'vm', and 'crypto'
when you require() them
<!-- bundles:js -->
<script src="node_modules/systemjs/dist/system.js"></script>
<script src="system.config.js"></script>
<script>System.import('./index')</script>
<!-- endinject -->
System.import('./src/codeFile.js').then(function(m) {
// do something with 'm'
});
normalize path (absolute, relative, and aliased)
check its internal registry to see if that module has already been loaded
perform additional modification of the file’s contents if desired
execute the module, add it to the registry, and then resolve the promise with the module
System.config({
defaultJSExtensions: true,
meta: {
"angular-ui-router": { deps: ['angular'] },
"angular-translate": { deps: ['angular'] },
"angular-touch": { deps: ['angular'] },
"angular-cookies": { deps: ['angular'] },
"angular-animate": { deps: ['angular'] },
"angular-ui-bootstrap": { deps: ['angular'] }
},
paths: {
"angular": "node_modules/angular/index.js",
"angular-ui-router": "node_modules/angular-ui-router/release/angular-ui-router.js",
"angular-translate": "node_modules/angular-translate/dist/angular-translate.js",
"angular-sanitize": "node_modules/angular-sanitize/angular-sanitize.js",
"angular-cookies": "node_modules/angular-cookies/angular-cookies.js",
"angular-ui-bootstrap": "node_modules/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js",
"angular-ui-tree": "node_modules/angular-ui-tree/dist/angular-ui-tree.js",
"angular-tree-control": "node_modules/angular-tree-control/angular-tree-control.js",
"angularModalService": "node_modules/angular-modal-service/dst/angular-modal-service.min.js",
"es6-promise": "node_modules/es6-promise/dist/es6-promise.js",
"fetch": "node_modules/whatwg-fetch/fetch",
"ckeditor": "node_modules/ckeditor/ckeditor.js",
"reflect-metadata": "node_modules/reflect-metadata/Reflect.js",
"querystringify": "node_modules/querystringify/index.js",
"lodash": "node_modules/lodash/lodash.js",
"lodash/*": "node_modules/lodash/*.js"
}
});
Register module
Import module
Check whether it exists
some other useful things
System.register(['./config/main.module',
'./config/main.module.config',
'angular-translate'],
function(exports_1, context_1) {
'use strict';
var __moduleName = context_1 && context_1.id;
var main_module_1;
function bootstrap() {
angular.bootstrap(document,
['pascalprecht.translate', main_module_1.tmModule.name]);
}
exports_1("bootstrap", bootstrap);
return {
setters:[
function (main_module_1_1) {
main_module_1 = main_module_1_1;
},
function (_1) {},
function (_2) {}],
execute: function() {
}
}
});
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"noImplicitAny": false,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noEmitHelpers" : true,
"rootDir" : "./",
"outDir" : "dist"
}
}
.tsconfig
But they say:
"Although we use SystemJS for illustrative purposes here, it's only one option for loading modules. Use the module loader that you prefer. For Webpack and Angular, see Webpack: an Introduction. Or, learn more about SystemJS configuration in general here."
npm install webpack -g
webpack entry.js bundle.js
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
}),
new WebpackNotifierPlugin(),
]
module.exports = {
entry: './main',
output: {
filename: 'bundle.js',
libraryTarget: "amd"
},
module: {
loaders: [
{
test: /\.js$/, loader: "babel-loader",
query: {
presets: ['es2015']
},
}
]
}
};
"var" - Export by setting a variable: var Library = xxx (default)
"this" - Export by setting a property of this: this["Library"] = xxx
"commonjs" - Export by setting a property of exports: exports["Library"] = xxx
"commonjs2" - Export by setting module.exports: module.exports = xxx
"amd" - Export to AMD (optionally named - set the name via the library option)
"umd" - Export to AMD, CommonJS2 or as property in root
resolve: {
extensions: ['', '.js', '.json', '.coffee']
}
module: {
loaders: [
{ test: /\.coffee$/, loader: 'coffee-loader' },
{ test: /\.js$/, loader: 'babel-loader' }
]
}
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader" },
]
require("./stylesheet.css");
preLoaders: [
{
test: /\.(es6|js)$/,
exclude: /node_modules/,
loaders: ['eslint']
}
]