SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
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?
HOWABOUTTHIS?
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

Weitere ähnliche Inhalte

Ähnlich wie Modular JavaScript with CommonJS Compiler

Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
intelliyole
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
Ran Mizrahi
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
Ben Lin
 

Ähnlich wie Modular JavaScript with CommonJS Compiler (20)

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

Mehr von Dmitry Sheiko

Mehr von Dmitry Sheiko (7)

The Flavor of TypeScript
The Flavor of TypeScriptThe Flavor of TypeScript
The Flavor of TypeScript
 
Writing Scalable and Maintainable CSS
Writing Scalable and Maintainable CSSWriting Scalable and Maintainable CSS
Writing Scalable and Maintainable CSS
 
Tooling JavaScript to ensure consistency in coding style
Tooling JavaScript to ensure consistency in coding styleTooling JavaScript to ensure consistency in coding style
Tooling JavaScript to ensure consistency in coding style
 
JavaScript MV* Framework - Making the Right Choice
JavaScript MV* Framework - Making the Right ChoiceJavaScript MV* Framework - Making the Right Choice
JavaScript MV* Framework - Making the Right Choice
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
A Quick Start - Version Control with Git
A Quick Start - Version Control with GitA Quick Start - Version Control with Git
A Quick Start - Version Control with Git
 
Bringing classical OOP into JavaScript
Bringing classical OOP into JavaScriptBringing classical OOP into JavaScript
Bringing classical OOP into JavaScript
 

Kürzlich hochgeladen

哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
ydyuyu
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Monica Sydney
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
ydyuyu
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
ydyuyu
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
F
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Monica Sydney
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
JOHNBEBONYAP1
 

Kürzlich hochgeladen (20)

哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime BalliaBallia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
 
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
 
Mira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call GirlsMira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
 

Modular JavaScript with CommonJS Compiler