Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RequireJS

 RequireJS

Talk given at NDC,Oslo - June 2013

Sebastiano Armeli

June 13, 2013
Tweet

More Decks by Sebastiano Armeli

Other Decks in Programming

Transcript

  1. var module = (function(){ // private variables, methods var title

    = “”; function f1() {} return { // public/privileged methods getTitle: function(){ return title; } } }()) ; MODULE PATTERN Thursday, June 13, 13
  2. define(function () { var title = “”; function f1() {}

    return { getTitle: function() { return title; } } }); RJS MODULE PATTERN Thursday, June 13, 13
  3. index.html js / -- main.js -- helpers.js -- app /

    -- views / -- view1.js -- vendor / -- require.js view1.js ------------ define([‘helpers’], function(helpers){ return { init: function(){} } }); define(function(){ // code here }); helpers.js ------------ Thursday, June 13, 13
  4. index.html js / -- main.js -- helpers.js -- app /

    -- views / -- view1.js -- vendor / -- require.js view1.js ------------ define([‘helpers’], function(helpers){ return { init: function(){} } }); define(function(){ // code here }); helpers.js ------------ Thursday, June 13, 13
  5. index.html ------------ <script src=”js/vendor/require.js” data-main=”js/main.js” main.js ------------ require([‘view1’],function(view1){ view1.init(); });

    index.html js / -- main.js -- helpers.js -- app / -- views / -- view1.js -- vendor / -- require.js Thursday, June 13, 13
  6. main.js ------------ require.config({ baseUrl: ‘./js’, paths: { ‘view1’: ‘app/views/view1’ }

    }); require([‘view1’],function(view1){ view1.init(); }); index.html js / -- main.js -- helpers.js -- app / -- views / -- view1.js -- vendor / -- require.js Thursday, June 13, 13
  7. var node = document.createElement('script'); node.async = true; node.setAttribute('data-requirecontext', context.contextName); node.setAttribute('data-requiremodule',

    moduleName); node.src = url; var head = document.getElementsByTagName('head')[0]; head.appendChild(node); Script Loader Thursday, June 13, 13
  8. exports.render = function() {}; var module = require(‘view1’); NO de!ne()

    require() synchronous Server-side approach Thursday, June 13, 13
  9. Simplified CommonJS Wrapper define(function(require, exports, module){ // Module required before

    the callback runs var helpers = require(‘helpers’); exports.render = function() { helpers.doSomething(); } }); Thursday, June 13, 13
  10. if ( typeof define === "function" && define.amd ) {

    define( "jquery", [], function () { return jQuery; }); } Thursday, June 13, 13
  11. index.html js / -- main.js -- helpers.js -- app /

    -- views / -- view1.js -- vendor / -- require.js -- backbone.js -- underscore.js -- jquery.js main.js ------------ require.config({ baseUrl: ‘js/vendor’, shim: { ‘underscore’:{ exports: ‘_’ }, ‘backbone’: { deps: [‘jquery’, ‘underscore’], exports: ‘Backbone’ } } }); require([‘backbone’],function(Backbone){ Backbone.history.start(); }); Thursday, June 13, 13
  12. index.html js / -- main.js -- helpers.js -- app /

    -- views / -- view1.js -- vendor / -- require.js -- backbone.js -- underscore.js -- jquery.js main.js ------------ require.config({ baseUrl: ‘js/vendor’, shim: { ‘underscore’:{ exports: ‘_’ }, ‘backbone’: { deps: [‘jquery’, ‘underscore’], exports: ‘Backbone’ } } }); require([‘backbone’],function(Backbone){ Backbone.history.start(); }); Thursday, June 13, 13
  13. LOADER PLUGINS • i18n!, async!, domReady! • text!, css!, json!,

    cs!, hbs! [plugin Module ID]![resource ID] Thursday, June 13, 13
  14. main.js ------------ require.config({ baseUrl: ‘./js’ }); require([‘text!partials/file.txt’], function(txt) { //

    txt goes here }); index.html js / -- main.js -- vendor / -- require.js -- text.js -- partials / -- !le.txt Thursday, June 13, 13
  15. main.js ------------ require.config({ baseUrl: ‘./js’ }); require([‘css!../css/style.css’], function() { //

    After css is loaded }); index.html js / -- main.js -- vendor / -- require.js -- css.js css / -- style.css Thursday, June 13, 13
  16. build.js ------------ ({ appDir:'../', mainConfigFile: '../js/main.js', dir: "../build", modules: [

    { name: "../main" } ] }) index.html js / -- main.js -- helpers.js -- app / -- views / -- view1.js -- vendor / -- require.js tools / -- build.js Thursday, June 13, 13
  17. build/js/main.js ---------------- index.html build / -- index.html -- build.txt --

    js / -- main.js -- helpers.js -- app / -- views / -- view1.js -- vendor / -- require.js -- tools / -- build.js js/vendor/../main.js ---------------- js/helpers.js js/vendor/view1.js js/vendor/../main.js build/build.txt ---------------- Thursday, June 13, 13
  18. ({ appDir:'../', mainConfigFile: '../js/main.js', dir: "../build", preserveLicenseComments: false, removeCombined: true,

    optimize: "uglify2", modules: [ { name: "../main", excludeShallow: [ "view1" ] } ] }) Thursday, June 13, 13
  19. runner.html ------------ runner.html spec / -- view1Spec.js -- main.js js

    / -- vendor / -- require.js <script src=”js/vendor/require.js” data-main=”spec/main.js” require.config({ baseUrl: ‘js/vendor’, paths: { ‘view1’: ..., ‘spec’: ‘../../spec’ } }); main.js ------------ Thursday, June 13, 13
  20. runner.html spec / -- view1Spec.js -- main.js js / --

    vendor / -- require.js -- domReady.js require.config({ ... }); require( [“domReady!”,“spec/view1Spec”], function(document){ jasmine.getEnv().addReporter( new jasmine.HtmlReporter() ); jasmine.getEnv().execute(); }); main.js ------------ Thursday, June 13, 13
  21. module.exports = function(grunt) { var config = require(“build”); grunt.initConfig({ requirejs:

    { compile: { options: config } } grunt.loadNpmTasks('grunt-contrib-requirejs'); } Gruntfile.js ------------ Thursday, June 13, 13
  22. ES6 - Modules Module de!nition module [module ID] export [variable

    | function] Module dependency import { [var | fn] } from [module ID] Thursday, June 13, 13
  23. ES6 - Modules module “helpers” { } module “view1” {

    import helpers from “helpers”; exports function init() {...}; } Thursday, June 13, 13
  24. Recap • Async Script loader • Future proof • No

    globals • Optimization • Modularity Thursday, June 13, 13