Top Banner
WITH Dmitry Sheiko Modular JavaScript CommonJS Compiler
43

Modular JavaScript with CommonJS Compiler

Jan 27, 2015

Download

Internet

Dmitry Sheiko

Introduction into a developing approach where you can keep your JavaScript modular without any performance loss
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Modular JavaScript with CommonJS Compiler

WITH

Dmitry Sheiko

Modular JavaScript

CommonJS Compiler

Page 2: Modular JavaScript with CommonJS Compiler

separates the functionality of a program into independent modules

MODULAR PROGRAMMING

Page 3: Modular JavaScript with CommonJS Compiler

encapsulates everything required to implement a single aspect of the desired

functionality

MODULE

Page 4: Modular JavaScript with CommonJS Compiler

Therefore, a complex problem can be broken into simpler tasks

Page 5: Modular JavaScript with CommonJS Compiler

The entire system becomes easier to

debugupdatemodify

Page 6: Modular JavaScript with CommonJS Compiler

What about JavaScript?

Page 7: Modular JavaScript with CommonJS Compiler

MODULE PATTERNvar bar = (function(){ // Functionality return exportObj; }()),

foo = (function( bar ){ // Functionality }( bar ));

Page 8: Modular JavaScript with CommonJS Compiler

And what the structure does it give for your codebase?

Page 9: Modular JavaScript with CommonJS Compiler
Page 10: Modular JavaScript with CommonJS Compiler

HOW ABOUT THIS?

Page 11: Modular JavaScript with CommonJS Compiler
Page 12: Modular JavaScript with CommonJS Compiler

AMD

• Designed to accommodate asynchronous loading• Lazy-load scripts • Can load more than just JavaScript files• Config settings to simplify path resolution and

dependency listing

Page 13: Modular JavaScript with CommonJS Compiler

AMD IMPROVES PERFORMANCE OF WEB APPLICATION by bypassing module loading along with the rest of the page content

Page 14: Modular JavaScript with CommonJS Compiler

AMD HARMS PERFORMANCE OF WEB APPLICATION by producing numerous HTTP requests

Page 15: Modular JavaScript with CommonJS Compiler

COMMONJS MODULES/1.1

• Designed for server-side JavaScript and for native desktop applications

• Simple and clean module definition syntax

Page 16: Modular JavaScript with CommonJS Compiler

CJS MODULES + COMMONJS COMPILER

• Designed for server-side JavaScript and for native desktop applications

• Simple and clean module definition syntax• Can load more than just JavaScript files• Config settings to simplify path resolution and

dependency listing

Page 17: Modular JavaScript with CommonJS Compiler

COMMONJS COMPILER is the keyhttp://dsheiko.github.io/cjsc

Page 18: Modular JavaScript with CommonJS Compiler

Let’s get started!

Page 19: Modular JavaScript with CommonJS Compiler

INSTALLING COMMONJS COMPILER$sudo npm i cjsc -g

Page 20: Modular JavaScript with CommonJS Compiler

EXAMPLE 1

`foo.js`:console.log( "foo.js: Hello World" );

`bar.js`:require( "./foo" );console.log( "bar.js: Hello World" );

Page 21: Modular JavaScript with CommonJS Compiler

EXAMPLE 1

Compiling `bar.js`:$cjsc bar.js build.js

Output of `build.js`:foo.js: Hello Worldbar.js: Hello World

Page 22: Modular JavaScript with CommonJS Compiler

WHAT HAVE WE JUST DONE?

We loaded one module in another. Both are executed in the compiled code

Page 23: Modular JavaScript with CommonJS Compiler

EXAMPLE 2

`foo.js`:var privateState = “lorem“;module.exports = { name: "foo.js" };

`bar.js`:console.log( require( "./foo" ) );console.log(“privateState:" + typeof privateState );

Page 24: Modular JavaScript with CommonJS Compiler

EXAMPLE 2

Output of `build.js`:{ name: "foo.js" }privateState: undefined

Page 25: Modular JavaScript with CommonJS Compiler

WHAT HAVE WE JUST DONE?

We accessed an exported object and made certain that private state isn't available outside the module.

Page 26: Modular JavaScript with CommonJS Compiler

EXAMPLE 3

`foo.js`:console.log( "foo.js: constructing" );module.exports = { name: "foo.js" };

`bar.js`:console.log( require( "./foo" ) );console.log( require( "./foo" ) );

Page 27: Modular JavaScript with CommonJS Compiler

EXAMPLE 3

Output of `build.js`:foo.js: constructing{ name: "foo.js" }{ name: "foo.js" }

Page 28: Modular JavaScript with CommonJS Compiler

WHAT HAVE WE JUST DONE?

We checked that loading a module URL multiple times results in a single cached instance.

Page 29: Modular JavaScript with CommonJS Compiler

EXAMPLE 4

`foo.tpl`:Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet

`bar.js`:var tpl = require( "./foo.tpl" );console.log( "foo:" + tpl );

Page 30: Modular JavaScript with CommonJS Compiler

EXAMPLE 4

Output of `build.js`:foo: Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet

Page 31: Modular JavaScript with CommonJS Compiler

WHAT HAVE WE JUST DONE?

We found out that while resolving module dependencies CommonJS Compiler exports any content of non-JavaScript or JSON syntax as a string.

Page 32: Modular JavaScript with CommonJS Compiler

EXAMPLE 5`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 ) );

Page 33: Modular JavaScript with CommonJS Compiler

EXAMPLE 5

Output of `build.js`:

Joe spends 6

Page 34: Modular JavaScript with CommonJS Compiler

WHAT HAVE WE JUST DONE?

We leveraged loading of plain text resource to obtain a template for further use with a template engine (mustache.js).

Page 35: Modular JavaScript with CommonJS Compiler

DEBUGGING COMPILED CODEGenerating source map:$cjsc bar.js build.js --source-map=build.js.map

JavaScript console refers to original sources:

Page 36: Modular JavaScript with CommonJS Compiler

RUN-TIME CONFIGURATIONJSON configuration syntax:{ "<dependency-name>": { "path": "<dependency-path>", "globalProperty": "<global-property>", exports: [ "<variable>", "<variable>" ], require: [ "<dependency-name>", "<dependency-name>" ] }}

Page 37: Modular JavaScript with CommonJS Compiler

RUN-TIME CONFIGURATION EXAMPLE{ "jQuery": { "globalProperty": "jQuery" }, "plugin": { "path": "./config/vendors/jquery.plugin.js", "require": "jQuery", "exports": "jQuery" }}

Page 38: Modular JavaScript with CommonJS Compiler

ENABLING CONFIGURATION

$cjsc foo.js build.js --config=config.json

Page 39: Modular JavaScript with CommonJS Compiler

BUILD AUTOMATION WITH GRUNTGruntfile.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" }},

Page 40: Modular JavaScript with CommonJS Compiler

BUILD AUTOMATION WITH GRUNTGruntfile.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" }}}

Page 41: Modular JavaScript with CommonJS Compiler

BUILD AUTOMATION WITH GRUNT

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.

Page 42: Modular JavaScript with CommonJS Compiler

THANK YOU!

COMMONJS COMPILERhttp://dsheiko.github.io/cjsc COMMONJS COMPILER GRUNT TASKhttps://github.com/dsheiko/grunt-contrib-cjsc

Page 43: Modular JavaScript with CommonJS Compiler

DMITRY SHEIKO

@sheikohttps://github.com/dsheiko

dsheiko.com