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

Similar documents
Getting Fat. Tips and tricks for when you're building a single-page app

55249: Developing with the SharePoint Framework Duration: 05 days

ReactJS and Webpack for Rails

Web Development for Dinosaurs An Introduction to Modern Web Development

npm install [<name> [<name>...]] [--save --save-dev --save-optional]

Webpack. What is Webpack? APPENDIX A. n n n

"Charting the Course... MOC A: Developing with the SharePoint Framework. Course Summary

welcome to BOILERCAMP HOW TO WEB DEV

High Performance Single Page Application with Vue.js

CodeHub. Curran Kelleher 8/18/2012

Modern frontend workflows in Liferay Portal and Liferay DXP. Iván Zaera Avellon, Liferay Chema Balsas, Liferay Pier Paolo Ramon, SMC

webpack bundle inner structure and optimization Alexey Ivanov, Evil Martians

Node.js. Node.js Overview. CS144: Web Applications

Frontend UI Training. Whats App :

ES6 Modules...& Polymer

Arjen de Blok. Senior Technical Consultant bij ICT Groep ( sinds 1995 Programmeren sinds 1990 Technologiën. Links

MOdern Java(Script) Server Stack

Stencil: The Time for Vanilla Web Components has Arrived

Introduction to Using NPM scripts as a Build Tool. 1. 1

MEAN February. techdt.la

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

Webpack 2 The last bundler you would need in Vijay Dharap, Principal Architect, Infosys

JavaScript Rd2. -Kyle Simpson, You Don t Know JS

Lecture 8. ReactJS 1 / 24

Chapter 1 - Development Setup of Angular

Pro JavaScript. Development. Coding, Capabilities, and Tooling. Den Odell. Apress"

Hi I m Jamund and I love ES6 and I m giving this talk.

Brunch Documentation. Release Brunch team

Modular JavaScript. JC Franco. Blake Stearman

Enterprise Web Development

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

Node.js 8 the Right Way

Web Applications. Software Engineering 2017 Alessio Gambi - Saarland University

a Very Short Introduction to AngularJS

Advanced React JS + Redux Development

django-baton Documentation

LEARN WITH INTRODUCTION TO TYPESCRIPT

Evolution of the "Web

Build Native-like Experiences in HTML5

20480C: Programming in HTML5 with JavaScript and CSS3. Course Code: 20480C; Duration: 5 days; Instructor-led. JavaScript code.

PWA s are the future!

Lab 1 - Introduction to Angular

Moodle JavaScript. Using AMD with RequireJS. Daniel Thee Roperto.

Virto SharePoint Forms Designer for Office 365. Installation and User Guide

Full Stack boot camp

Web Performance in

APACHE SLING & FRIENDS TECH MEETUP SEPTEMBER Integrating a Modern Frontend Setup with AEM Natalia Venditto, Netcentric

CS193X: Web Programming Fundamentals

Backend Development. SWE 432, Fall Web Application Development

Jquery Ajax Json Php Mysql Data Entry Example

Review. Fundamentals of Website Development. Web Extensions Server side & Where is your JOB? The Department of Computer Science 11/30/2015

VS005 - Cordova vs NativeScript

Angular 2 Programming

SAMPLE CHAPTER. Using Electron and NW.js. Paul B. Jensen. FOREWORD BY Cheng Zhao MANNING

Unifer Documentation. Release V1.0. Matthew S

Jenkins 2 UX Improvements. Keith Zantow Software Engineer, CloudBees, Inc.

Topic 16: Validation. CITS3403 Agile Web Development. Express, Angular and Node, Chapter 11

JavaScript CS 4640 Programming Languages for Web Applications

Part 1: jquery & History of DOM Scripting

jquery Tutorial for Beginners: Nothing But the Goods

Finally JavaScript Is Easy, with Oracle JET! Geertjan Wielenga Product Manager Oracle Developer Tools

MEAN Stack. 1. Introduction. 2. Foundation a. The Node.js framework b. Installing Node.js c. Using Node.js to execute scripts

AngularJS Fundamentals

JavaScript: the Big Picture

Don't Call Us, We'll Call You:

Tools. SWE 432, Fall Design and Implementation of Software for the Web

Comprehensive AngularJS Programming (5 Days)

Building Backbone Plugins

Node.js. Mendel Rosenblum. CS142 Lecture Notes - Node.js

TypeScript coding JavaScript without the pain

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

JavaScript Fundamentals_

Index. Bower, 133, 352 bower.json file, 376 Bundling files, 157

django-baton Documentation

Callbacks & Promises

DECOUPLING PATTERNS, SERVICES AND CREATING AN ENTERPRISE LEVEL EDITORIAL EXPERIENCE

A JavaScript Framework for Presentations and Animations on Computer Science

Application Development

An Introduction to TypeScript. Personal Info

The Essence of Node JavaScript on the Server Asynchronous Programming Module-driven Development Small Core, Vibrant Ecosystem The Frontend Backend

ethnio tm IMPLEMENTATION GUIDE ETHNIO, INC W SUNSET BLVD LOS ANGELES, CA TEL (888) VERSION NO. 3 CREATED JUL 14, 2017

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Web Application Development

Dreamweaver MX The Basics

An Automatic Internationalization and Localization Mechanism for Web Applications

Index. Elad Elrom 2016 E. Elrom, Pro MEAN Stack Development, DOI /

Advance Mobile& Web Application development using Angular and Native Script

Backend Development. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Introduction to Sencha Ext JS

Module Patterns. Understanding and using the Node module system. Pedro Teixeira. This book is for sale at

Virto SharePoint Forms Designer for Office 365. Installation and User Guide

JS Event Loop, Promises, Async Await etc. Slava Kim

Templates and Databinding. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Advanced Angular & Angular 6 Preview. Bibhas Bhattacharya

Sample Copy. Not For Distribution.

Index. Bare-bones connect application, 125 Binary JSON (BSON), 171. Callback functions asynchronous code ifelse, 199 loops,

Welcome. Quick Introductions

DrupalCon Barcelona Preston So September 22, 2015

IMPLEMENTATION AND EVALUATION OF AN ANDROID ACCESSOR-BASED IOT MIDDLEWARE. Vimal Moorthy Krishnamoorthy, B.E

JavaScript CS 4640 Programming Languages for Web Applications

Transcription:

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 often sphagetti code OK only for very simple pages

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

CommonJS Modules Otherwise plain JS files that have an exports object Use require('module_name') to load other modules Synchronous function (proposal exists to add async) module_name is a path (.js extension optional) to search for in any of the configured path dirs # config.js var fs = require('fs'); exports.readconfig = function(callback) { return fs.readfile('my-config.json', 'utf8', callback); }; # app.js var config = require('./config'); config.readconfig(function(err, data) { });

AMD Asynchronous Module Definitions (AMD) define wrapper potentially asynchronously loaded dependencies # config.js define(['fs'], function(fs) { return { readconfig: function(callback) { return fs.readfile('my-config.json', 'utf8', callback); }; }; }); # app.js define(['config'], function(config) { config.readconfig(function(err, data) { }); });

CommonJS Style with AMD RequireJS allows the use of CommonJS requires Optimizer parses source and recognizes deps # config.js define(function(require) { var fs = require('fs'); return { readconfig: function(callback) { return fs.readfile('my-config.json', 'utf8', callback); }; }; }); # app.js define(['config'], function(config) { config.readconfig(function(err, data) { }); });

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

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

Examples

Advanced Features Pragmas (similar to #ifdefs in C preprocessor) Bundle text files (e.g. templates) Load CommonJS modules Multiversion support

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

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 frontend scripts but Bower is still very widely used

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)

Alternatives: Webpack r.js optimizer replacement Supports both AMD and CommonJS Bundle everything CSS (small) images templates

The Future of JS Modules ECMAScript 6 (ES6) features native module support Details still tentative # config.js import readfile from 'fs'; export function readconfig(callback) { return readfile('my-config.json', 'utf8', callback); } # app.js import readconfig from 'config'; readconfig(function(err, data) { });