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

Similar to Modular JavaScript with CommonJS Compiler (20)

PDF
JavaScript Modules Done Right
Mariusz Nowak
 
PPTX
uRequire@greecejs: An introduction to http://uRequire.org
Agelos Pikoulas
 
KEY
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
Mikko Ohtamaa
 
PDF
New and upcoming features in the Node.js module loaders
Igalia
 
PDF
JavaScript Dependencies, Modules & Browserify
Johan Nilsson
 
PPTX
JS & NodeJS - An Introduction
Nirvanic Labs
 
PDF
Unbundling the JavaScript module bundler - Luciano Mammino - Codemotion Rome ...
Codemotion
 
PDF
Unbundling the JavaScript module bundler - Codemotion Rome 2018
Luciano Mammino
 
PDF
Asynchronous Module Definition (AMD)
xMartin12
 
PDF
Advanced Node.JS Meetup
LINAGORA
 
PDF
Create a module bundler from scratch
Sing Ming Chen
 
PPTX
Es build presentation
PT Citra Niaga Teknologi
 
KEY
Modules and EmbedJS
Jens Arps
 
KEY
JavaScript Growing Up
David Padbury
 
PDF
ITB2019 CommandBox vs Node.js - Nolan Erck
Ortus Solutions, Corp
 
PPTX
Node js Powerpoint Presentation by PDEU Gandhinagar
tirthuce22
 
PDF
PLOG - Modern Javascripting with Plone
Rok Garbas
 
PDF
NodeJs Modules1.pdf
Bareen Shaikh
 
PPTX
node.js.pptx
rani marri
 
PDF
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
GITS Indonesia
 
JavaScript Modules Done Right
Mariusz Nowak
 
uRequire@greecejs: An introduction to http://uRequire.org
Agelos Pikoulas
 
Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJ...
Mikko Ohtamaa
 
New and upcoming features in the Node.js module loaders
Igalia
 
JavaScript Dependencies, Modules & Browserify
Johan Nilsson
 
JS & NodeJS - An Introduction
Nirvanic Labs
 
Unbundling the JavaScript module bundler - Luciano Mammino - Codemotion Rome ...
Codemotion
 
Unbundling the JavaScript module bundler - Codemotion Rome 2018
Luciano Mammino
 
Asynchronous Module Definition (AMD)
xMartin12
 
Advanced Node.JS Meetup
LINAGORA
 
Create a module bundler from scratch
Sing Ming Chen
 
Es build presentation
PT Citra Niaga Teknologi
 
Modules and EmbedJS
Jens Arps
 
JavaScript Growing Up
David Padbury
 
ITB2019 CommandBox vs Node.js - Nolan Erck
Ortus Solutions, Corp
 
Node js Powerpoint Presentation by PDEU Gandhinagar
tirthuce22
 
PLOG - Modern Javascripting with Plone
Rok Garbas
 
NodeJs Modules1.pdf
Bareen Shaikh
 
node.js.pptx
rani marri
 
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
GITS Indonesia
 

More from Dmitry Sheiko (7)

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

Recently uploaded (20)

PDF
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
PPTX
原版一样(LHU毕业证书)英国利物浦希望大学毕业证办理方法
Taqyea
 
PDF
AI security AI security AI security AI security
elite44
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PPTX
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
PPTX
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
PDF
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
PDF
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
PDF
Empowering Local Language Email with IDN & EAI – Powered by XgenPlus
XgenPlus Technologies
 
PDF
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
PPTX
原版一样(毕业证书)法国蒙彼利埃大学毕业证文凭复刻
Taqyea
 
PPTX
Meloniusk_Communication_Template_best.pptx
howesix147
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PPTX
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
PDF
google promotion services in Delhi, India
Digital Web Future
 
PPTX
CHAPTER 1 - PART 3 FOR GRADE 11 STUDENTS
FSBTLEDNathanVince
 
PDF
The Hidden Benefits of Outsourcing IT Hardware Procurement for Small Businesses
Carley Cramer
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
PDF
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
PPTX
西班牙巴利阿里群岛大学电子版毕业证{UIBLetterUIB文凭证书}文凭复刻
Taqyea
 
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
原版一样(LHU毕业证书)英国利物浦希望大学毕业证办理方法
Taqyea
 
AI security AI security AI security AI security
elite44
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
Empowering Local Language Email with IDN & EAI – Powered by XgenPlus
XgenPlus Technologies
 
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
原版一样(毕业证书)法国蒙彼利埃大学毕业证文凭复刻
Taqyea
 
Meloniusk_Communication_Template_best.pptx
howesix147
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
google promotion services in Delhi, India
Digital Web Future
 
CHAPTER 1 - PART 3 FOR GRADE 11 STUDENTS
FSBTLEDNathanVince
 
The Hidden Benefits of Outsourcing IT Hardware Procurement for Small Businesses
Carley Cramer
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
西班牙巴利阿里群岛大学电子版毕业证{UIBLetterUIB文凭证书}文凭复刻
Taqyea
 
Ad

Modular JavaScript with CommonJS Compiler