SlideShare a Scribd company logo
WITH
Dmitry Sheiko
ModularJavaScript
CommonJSCompiler
separates the functionality of a program
into independent modules
MODULARPROGRAMMING
encapsulates everything required to
implement a single aspect of the desired
functionality
MODULE
Therefore,acomplexproblemcanbe
brokenintosimplertasks
Theentiresystem
becomeseasierto
debug
update
modify
WhataboutJavaScript?
MODULEPATTERN
var
bar = (function(){
// Functionality
return exportObj;
}()),
foo = (function( bar ){
// Functionality
}( bar ));
Andwhatthestructuredoesitgive
foryourcodebase?
Modular JavaScript with CommonJS Compiler
HOWABOUTTHIS?
Modular JavaScript with CommonJS Compiler
AMD
•
Designedtoaccommodateasynchronousloading
•
Lazy-loadscripts
•
CanloadmorethanjustJavaScriptfiles
•
Configsettingstosimplifypathresolutionand
dependencylisting
AMDIMPROVESPERFORMANCEOF
WEBAPPLICATIONby bypassing
module loading along with the rest of
the page content
AMDHARMSPERFORMANCEOFWEB
APPLICATIONby producing numerous
HTTP requests
COMMONJSMODULES/1.1
•
Designedforserver-sideJavaScriptandfor
nativedesktopapplications
•
Simpleandcleanmoduledefinitionsyntax
CJSMODULES+COMMONJSCOMPILER
•
Designedforserver-sideJavaScriptandfor
nativedesktopapplications
•
Simpleandcleanmoduledefinitionsyntax
•
CanloadmorethanjustJavaScriptfiles
•
Configsettingstosimplifypathresolutionand
dependencylisting
COMMONJSCOMPILERis the key
http://dsheiko.github.io/cjsc
Let’s get started!
INSTALLINGCOMMONJSCOMPILER
$sudo npm i cjsc -g
EXAMPLE1
`foo.js`:
console.log( "foo.js: Hello World" );
`bar.js`:
require( "./foo" );
console.log( "bar.js: Hello World" );
EXAMPLE1
Compiling`bar.js`:
$cjsc bar.js build.js
Outputof `build.js`:
foo.js: Hello World
bar.js: Hello World
WHATHAVEWEJUSTDONE?
We loaded one module in another. Both are
executed in the compiled code
EXAMPLE2
`foo.js`:
var privateState = “lorem“;
module.exports = { name: "foo.js" };
`bar.js`:
console.log( require( "./foo" ) );
console.log(“privateState:" + typeof privateState );
EXAMPLE2
Outputof `build.js`:
{ name: "foo.js" }
privateState: undefined
WHATHAVEWEJUSTDONE?
We accessed an exported object and made
certain that private state isn't available outside
the module.
EXAMPLE3
`foo.js`:
console.log( "foo.js: constructing" );
module.exports = { name: "foo.js" };
`bar.js`:
console.log( require( "./foo" ) );
console.log( require( "./foo" ) );
EXAMPLE3
Outputof `build.js`:
foo.js: constructing
{ name: "foo.js" }
{ name: "foo.js" }
WHATHAVEWEJUSTDONE?
We checked that loading a module URL multiple
times results in a single cached instance.
EXAMPLE4
`foo.tpl`:
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet
`bar.js`:
var tpl = require( "./foo.tpl" );
console.log( "foo:" + tpl );
EXAMPLE4
Outputof `build.js`:
foo: Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet
WHATHAVEWEJUSTDONE?
We found out that while resolving module
dependencies CommonJS Compiler exports any
content of non-JavaScript or JSON syntax as a
string.
EXAMPLE5
`foo.tpl`:
{{title}} spends {{calc}}
`bar.js`:
var mustache = require( "./mustache" ),
tpl = require( "./foo.tpl" ),
view = { title: "Joe", calc: function () { return 2 + 4;}};
console.log( mustache.render( tpl, view ) );
EXAMPLE5
Outputof `build.js`:
Joe spends 6
WHATHAVEWEJUSTDONE?
We leveraged loading of plain text resource to
obtain a template for further use with a
template engine (mustache.js).
DEBUGGINGCOMPILEDCODE
Generatingsourcemap:
$cjsc bar.js build.js --source-map=build.js.map
JavaScriptconsolereferstooriginalsources:
RUN-TIMECONFIGURATION
JSONconfigurationsyntax:
{
"<dependency-name>": {
"path": "<dependency-path>",
"globalProperty": "<global-property>",
exports: [ "<variable>", "<variable>" ],
require: [ "<dependency-name>", "<dependency-name>" ]
}
}
RUN-TIMECONFIGURATIONEXAMPLE
{
"jQuery": {
"globalProperty": "jQuery"
},
"plugin": {
"path": "./config/vendors/jquery.plugin.js",
"require": "jQuery",
"exports": "jQuery"
}
}
ENABLINGCONFIGURATION
$cjsc foo.js build.js --config=config.json
BUILDAUTOMATIONWITHGRUNT
Gruntfile.js:
grunt.loadNpmTasks( "grunt-contrib-cjsc" );
grunt.initConfig({
cjsc:{
debug: {
options: {
sourceMap: "./wwwroot/build/js/*.map",
config: {
"backbone": {
"path": "./wwwroot/vendors/backbone/backbone"
}}},
files: {
"./wwwroot/build/js/app.js": "./wwwroot/js/app.js"
}},
BUILDAUTOMATIONWITHGRUNT
Gruntfile.js:
build: {
options: {
minify: true,
banner: "/* License */",
config: {
"backbone": {
"path": "path": "./wwwroot/vendors/backbone/backbone"
}}},
files: {
"./wwwroot/build/js/app.js": "./wwwroot/js/app.js"
}}}
BUILDAUTOMATIONWITHGRUNT
It gives us two options: cjsc:debug and cjsc:build. The first
one we run during development; it provides source maps
for debugging and doesn't compress output. The second
option we use when preparing production build.
THANKYOU!
COMMONJSCOMPILER
http://dsheiko.github.io/cjsc
COMMONJSCOMPILERGRUNTTASK
https://github.com/dsheiko/grunt-contrib-cjsc
DMITRYSHEIKO
@sheiko
https://github.com/dsheiko
dsheiko.com

More Related Content

PDF
GraphQL in Symfony
PDF
Python Load Testing - Pygotham 2012
PDF
Web sphere administration
PPTX
A few good JavaScript development tools
PPT
Performance and Scalability Testing with Python and Multi-Mechanize
PDF
AngularJS - TechTalk 3/2/2014
PDF
cbmanual
PDF
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
GraphQL in Symfony
Python Load Testing - Pygotham 2012
Web sphere administration
A few good JavaScript development tools
Performance and Scalability Testing with Python and Multi-Mechanize
AngularJS - TechTalk 3/2/2014
cbmanual
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009

Similar to Modular JavaScript with CommonJS Compiler (20)

PPTX
Angular JS in 2017
PDF
IOC + Javascript
PPT
Smoothing Your Java with DSLs
PDF
WebNet Conference 2012 - Designing complex applications using html5 and knock...
PDF
Warsaw Frontend Meetup #1 - Webpack
PDF
Advanced Node.JS Meetup
PDF
Webpack: your final module bundler
KEY
Modules and EmbedJS
PDF
Frontend JS workflow - Gulp 4 and the like
PPSX
RequireJS
PPTX
Single Page Applications on JavaScript and ASP.NET MVC4
KEY
Exciting JavaScript - Part II
ODP
Drupal Best Practices
PDF
HTML5 for the Silverlight Guy
PDF
Intro to node.js - Ran Mizrahi (27/8/2014)
PDF
Intro to node.js - Ran Mizrahi (28/8/14)
PPTX
Javascript first-class citizenery
KEY
How and why i roll my own node.js framework
PDF
Spring Data MongoDB 介紹
PDF
JavaFX Enterprise (JavaOne 2014)
Angular JS in 2017
IOC + Javascript
Smoothing Your Java with DSLs
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Warsaw Frontend Meetup #1 - Webpack
Advanced Node.JS Meetup
Webpack: your final module bundler
Modules and EmbedJS
Frontend JS workflow - Gulp 4 and the like
RequireJS
Single Page Applications on JavaScript and ASP.NET MVC4
Exciting JavaScript - Part II
Drupal Best Practices
HTML5 for the Silverlight Guy
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (28/8/14)
Javascript first-class citizenery
How and why i roll my own node.js framework
Spring Data MongoDB 介紹
JavaFX Enterprise (JavaOne 2014)
Ad

More from Dmitry Sheiko (7)

PPTX
The Flavor of TypeScript
PPTX
Writing Scalable and Maintainable CSS
PDF
Tooling JavaScript to ensure consistency in coding style
PDF
JavaScript MV* Framework - Making the Right Choice
PDF
TypeScript Introduction
PDF
A Quick Start - Version Control with Git
PPTX
Bringing classical OOP into JavaScript
The Flavor of TypeScript
Writing Scalable and Maintainable CSS
Tooling JavaScript to ensure consistency in coding style
JavaScript MV* Framework - Making the Right Choice
TypeScript Introduction
A Quick Start - Version Control with Git
Bringing classical OOP into JavaScript
Ad

Recently uploaded (20)

PDF
Introduction to the IoT system, how the IoT system works
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
Funds Management Learning Material for Beg
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
Digital Literacy And Online Safety on internet
PDF
Exploring VPS Hosting Trends for SMBs in 2025
PPTX
Mathew Digital SEO Checklist Guidlines 2025
PPTX
E -tech empowerment technologies PowerPoint
PPT
Ethics in Information System - Management Information System
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
Introuction about WHO-FIC in ICD-10.pptx
Introduction to the IoT system, how the IoT system works
WebRTC in SignalWire - troubleshooting media negotiation
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Slides PPTX World Game (s) Eco Economic Epochs.pptx
INTERNET------BASICS-------UPDATED PPT PRESENTATION
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
introduction about ICD -10 & ICD-11 ppt.pptx
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Funds Management Learning Material for Beg
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Module 1 - Cyber Law and Ethics 101.pptx
Digital Literacy And Online Safety on internet
Exploring VPS Hosting Trends for SMBs in 2025
Mathew Digital SEO Checklist Guidlines 2025
E -tech empowerment technologies PowerPoint
Ethics in Information System - Management Information System
Job_Card_System_Styled_lorem_ipsum_.pptx
Introuction about WHO-FIC in ICD-10.pptx

Modular JavaScript with CommonJS Compiler