RequireJS Javascript Modules for the Browser By Ben Keith Quoin, - - PowerPoint PPT Presentation

requirejs javascript modules for the browser by ben keith
SMART_READER_LITE
LIVE PREVIEW

RequireJS Javascript Modules for the Browser By Ben Keith Quoin, - - PowerPoint PPT Presentation

RequireJS Javascript Modules for the Browser By Ben Keith Quoin, Inc. Traditional Browser JS One global namespace Often inline JS code embedded directly in HTML Many <script> tags with hidden ordering dependencies Very


slide-1
SLIDE 1

RequireJS – Javascript Modules for the Browser By Ben Keith Quoin, Inc.

slide-2
SLIDE 2

Traditional Browser JS

  • One global namespace
  • Often inline JS code embedded directly in

HTML

  • Many <script> tags with hidden ordering

dependencies

  • Very often sphagetti code
  • OK only for very simple pages
slide-3
SLIDE 3

Improvements

  • Script loaders/bundlers (e.g. head.js, LABjs, Rails

assets)

– Eliminate a lot of manual <script> tags – Make dependencies explicit – But still require external determination of dependencies

  • Single app object in global namespace

– Split up related code into JS classes – Separated script files (not modules)

  • Better but doesn't scale well to large apps
slide-4
SLIDE 4

CommonJS Modules

  • Otherwise plain JS files that have an e

x p

  • r

t s

  • bject
  • Use r

e q u i r e ( ' m

  • d

u l e _ n a m e ' ) to load other modules

Synchronous function (proposal exists to add async)

m

  • d

u l e _ n a m e is a path (.js extension optional) to search for in any of the configured path dirs

# c

  • n

f i g . j s v a r f s = r e q u i r e ( ' f s ' ) ; e x p

  • r

t s . r e a d C

  • n

f i g = f u n c t i

  • n

( c a l l b a c k ) { r e t u r n f s . r e a d F i l e ( ' m y

  • c
  • n

f i g . j s

  • n

' , ' u t f 8 ' , c a l l b a c k ) ; } ; # a p p . j s v a r c

  • n

f i g = r e q u i r e ( ' . / c

  • n

f i g ' ) ; c

  • n

f i g . r e a d C

  • n

f i g ( f u n c t i

  • n

( e r r , d a t a ) { … } ) ;

slide-5
SLIDE 5

AMD

  • Asynchronous Module Definitions (AMD)

– d

e f i n e wrapper

– potentially asynchronously loaded dependencies

# c

  • n

f i g . j s d e f i n e ( [ ' f s ' ] , f u n c t i

  • n

( f s ) { r e t u r n { r e a d C

  • n

f i g : f u n c t i

  • n

( c a l l b a c k ) { r e t u r n f s . r e a d F i l e ( ' m y

  • c
  • n

f i g . j s

  • n

' , ' u t f 8 ' , c a l l b a c k ) ; } ; } ; } ) ; # a p p . j s d e f i n e ( [ ' c

  • n

f i g ' ] , f u n c t i

  • n

( c

  • n

f i g ) { c

  • n

f i g . r e a d C

  • n

f i g ( f u n c t i

  • n

( e r r , d a t a ) { … } ) ; } ) ;

slide-6
SLIDE 6

CommonJS Style with AMD

  • RequireJS allows the use of CommonJS

requires

– Optimizer parses source and recognizes deps

# c

  • n

f i g . j s d e f i n e ( f u n c t i

  • n

( r e q u i r e ) { v a r f s = r e q u i r e ( ' f s ' ) ; r e t u r n { r e a d C

  • n

f i g : f u n c t i

  • n

( c a l l b a c k ) { r e t u r n f s . r e a d F i l e ( ' m y

  • c
  • n

f i g . j s

  • n

' , ' u t f 8 ' , c a l l b a c k ) ; } ; } ; } ) ; # a p p . j s d e f i n e ( [ ' c

  • n

f i g ' ] , f u n c t i

  • n

( c

  • n

f i g ) { c

  • n

f i g . r e a d C

  • n

f i g ( f u n c t i

  • n

( e r r , d a t a ) { … } ) ; } ) ;

slide-7
SLIDE 7

RequireJS

  • Most widely-used AMD implementation
  • Script that runs in browser to load, link and

execute modules

  • Can load modules async in the browser

– Useful for dev – Or incremental loading of large applications

slide-8
SLIDE 8

Optimizer (r.js)

  • Bundles multiple modules into one file

– multiple bundles (ex. one file for slowly changing

dependencies and one for application code)

  • Recommended approach

– Much greater performance for large projects

  • Reduces requests on page load

– Error handling for anonymous async script loading is flaky on

IE

  • Very powerful and flexible

– Configuration can get confusing at times

slide-9
SLIDE 9

Examples

slide-10
SLIDE 10

Advanced Features

  • Pragmas (similar to #

i f d e f s in C preprocessor)

  • Bundle text files (e.g. templates)
  • Load CommonJS modules
  • Multiversion support
slide-11
SLIDE 11

Almond

  • Minimalistic AMD implementation for the

browser

  • Very small (1k minified+gzip)

– vs. fully RequireJS script (6.2k minified+gzip)

  • Cannot load remote scripts

– Everything must be pre-built and loaded in script

tags

slide-12
SLIDE 12

Bower

  • Downloads JS dependencies
  • Uses a declaritive config file (bower.json)

– Tell it what scripts you want by name – Tell it where to download them to

  • Similar to NPM but targeted towards browser scripts
  • NPM is increasingly used as a manager for front-

end scripts

– but Bower is still very widely used

slide-13
SLIDE 13

Alternatives: Browserify

  • First popular implementation of CommonJS in the browser

– Fairly new, but gaining momentum

  • Useful for sharing code between NodeJS and browser

– Potentially useful if your backend is in Node

  • e.g. sharing validation code between front and back end
  • Always requires a backend build to generate a usable script
  • Uses NPM to download scripts

– But still has same issue as RequireJS with non-AMD/CommonJS

scripts

  • Plugin available to do lazy loading (not very pretty)
slide-14
SLIDE 14

Alternatives: Webpack

  • r.js optimizer replacement
  • Supports both AMD and CommonJS
  • Bundle everything

– CSS – (small) images – templates

slide-15
SLIDE 15

The Future of JS Modules

  • ECMAScript 6 (ES6) features native module

support

  • Details still tentative

# c

  • n

f i g . j s i m p

  • r

t r e a d F i l e f r

  • m

' f s ' ; e x p

  • r

t f u n c t i

  • n

r e a d C

  • n

f i g ( c a l l b a c k ) { r e t u r n r e a d F i l e ( ' m y

  • c
  • n

f i g . j s

  • n

' , ' u t f 8 ' , c a l l b a c k ) ; } # a p p . j s i m p

  • r

t r e a d C

  • n

f i g f r

  • m

' c

  • n

f i g ' ; r e a d C

  • n

f i g ( f u n c t i

  • n

( e r r , d a t a ) { … } ) ;