Browserify
Webpack
Rollup
(function(){
// todo
}())
(function($){
// jQuery plugin
}(jQuery))
// testModule.js
function testModule() {
this.hello = function() {
return 'hello!';
}
this.goodbye = function() {
return 'goodbye!';
}
}
module.exports = testModule;
// consumer.js
const test = require('testModule');
const testModuleInstance = new test();
testModuleInstance.hello(); // 'hello!'
testModuleInstance.goodbye(); // 'goodbye!'
// consumer.js
define(['myModule'], function(myModule) {
myModule.hello();
myModule.goodbye();
});
/* Alternative syntax */
define(function (require) {
const myModule = require('myModule');
myModule.hello();
myModule.goodbye();
});
// myModule.js
define([], function() {
return {
hello: function() {
return 'hello';
},
goodbye: function() {
return 'goodbye';
}
};
});
In many cases it uses AMD as a base, with special-casing added to handle CommonJS compatibility
capable of working on both client and server
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define([], factory);
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory();
} else {
// Browser globals (Note: root is window)
root.returnExports = factory();
}
}(this, function () {
// Methods
function private(){};
function hello(){};
function goodbye(){};
// Exposed public methods
return {
hello: hello,
goodbye: goodbye
}
}));
// testModule.js
export default function testModule() {
function hello() {
return 'hello!';
}
function goodbye() {
return 'goodbye!';
}
}
// consumer.js
import testModule from 'testModule';
// or
import { hello } from 'testModule';
import React, { Component } from 'react';
class App extends Component {
render() {
return (
<div>Hello World!</div>
);
}
}
export default App;
Simply put, it is the process of grouping modules (and their dependencies) into a single file (or group of files) in the correct order.
// testModule.js
export default function testModule() {
function hello() {
return 'hello!';
}
function goodbye() {
return 'goodbye!';
}
}
// consumer.js
import { hello } from 'testModule';
// testModule.js
export default function testModule() {
function hello() {
return 'hello!';
}
}
Tree shaking