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

Модули.Революция

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for DaZzz DaZzz
December 21, 2014

 Модули.Революция

Выступление на ES6 Meetup by Codehipsters. Рассмотрение подходов CJS, AMD и ES6 Modules. Описание плюсов и минусов каждого.

Avatar for DaZzz

DaZzz

December 21, 2014
Tweet

More Decks by DaZzz

Other Decks in Programming

Transcript

  1. var  Say  =  (function()  {      //  Приватные  методы

         var            hi    =  'Hi!',          bye  =  'Bye!'      //  Публичные  методы      return  {          sayHi:  function()  {              console.log(hi);          },          sayBye:  function()  {              console.log(bye);          }      }   }());
  2. //  say.js   exports.sayHi  =  function()  {      console.log('Hi!');

      }   exports.sayBye  =  function()  {      console.log('Bye!')   }
  3. <!-­‐-­‐  index.html  -­‐-­‐>   <script>      var    

         Say      =  require('say'),          sayHi  =  require('say').sayHi;      Say.sayHi();      sayHi();   </script>
  4. (function(/*!  Brunch  !*/)  {      'use  strict';    

     var  globals  =  typeof  window  !==  'undefined'  ?  window  :  global;      var  modules  =  {};      var  cache  =  {};      var  has                      =  function(object,  name)  {  ...  };      var  expand                =  function(root,  name)  {  ...  };      var  dirname              =  function(path)  {  ...  };      var  localRequire    =  function(path)  {  ...  };      var  initModule        =  function(name,  definition)  {  ...  };      var  require  =  function(name,  loaderPath)  {  ...  };      var  define  =  function(bundle,  fn)  {  ...  };      var  list  =  function()  {  ...  };      globals.require  =  require;      globals.require.define  =  define;      globals.require.register  =  define;      globals.require.list  =  list;      globals.require.brunch  =  true;   })();   require.register("say",  function(exports,  require,  module)  {      exports.sayHi  =  function()  {          console.log('Hi!');      }      exports.sayBye  =  function()  {          console.log(‘Bye!');      }   }); Бранче-магия Наши методы
  5. <!-­‐-­‐  index.html  -­‐-­‐>   <script          

     src="bower_components/requirejs/require.js"            data-­‐main="main">   </script>
  6. //  main.js   requirejs.config({          baseUrl:  'lib'

      });   require(['Say'],  function  (Say)  {      Say.sayHi();      Say.sayBye();   });
  7. //  lib/say.js   define('Say',  [],  function()  {      var

     sayPrivate  =  function  ()  {          console.log('Private!');      }      return  {          sayHi:  function  ()  {              console.log('Hi!');          },          sayBye:  function  ()  {              console.log('Bye!');          }      }   });
  8. //  so_dependecies_much_require.js   require([      'underscore',      'jquery',

         'numeral',      'd3',      'components/Table',      'components/Header',      'components/RowGroup',      'components/Row',      'components/Cell',      'components/BarChart',      'components/Toggle',      'components/Dropdown',      'components/YourMomma'   ],  function(_,  $,  numeral,  d3,     Table,  RowGroup,  Row,  Cell,     Header,  Toggle,  Dropdown,  SoFat)  {            //  Код   }
  9. import  'lib/calc'  as  c;   import  {  square  as  squ

     }  from  'lib/calc';   import  {  square,  MY_CONSTANT  }  from  'lib/calc';   import  *  from  'lib/calc'; Импорт
  10. Динамический импорт Promise.all(              

       ['module1',  'module2',  'module3']                  .map(x  =>  System.import(x)))          .then(function  ([module1,  module2,  module3])  {                    }); Мапим строки в импорты
  11. Как все работает? 1. Подгрузка всех импортов заблаговременно. Браузер должен

    знать все пути до начала выполнения скриптов. 2. Асинхронная подгрузка System.import.
  12. <!-­‐-­‐  index.html  -­‐-­‐>   <script>      System.baseURL  =  '/lib/';

         System.import('main').then(function(Main)  {          Main.main();      });   </script>
  13. //  lib/main.js   import  {sayHi,  sayBye}  from  'say'   export

     function  main()  {      sayHi();      sayBye();   }
  14. //  lib/say.js   export  function  sayHi()  {      console.log('Hi!');

      }   export  function  sayBye()  {      console.log('Bye!');   }
  15. Модули ES6 • Асинхронный require() • Не нужны обертки на

    модулями • Использование 6to5 или traceur