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

Модули

 Модули

Какие есть модули, краткий обзор

Теги: модули, CommonJS, AMD, ES6 Modules, WebPack, динамическая подгрузка

Avatar for Eugene Zhlobo

Eugene Zhlobo

October 05, 2016
Tweet

More Decks by Eugene Zhlobo

Other Decks in Programming

Transcript

  1. • Оборачиваем в самовызываемые анонимные функции для инкапсуляции • Объединяем

    в один зачем вы читаете этот текст файл для оптимизации • Следим за порядком все равно проговариваю вслух зависимостей
  2. const $ = require('jquery'); exports.myComponent = function() { // Code

    of my component }; Любите браузеры?
  3. module getName { export var value = ‘Eugene’ } module

    greeting { import value from getName export `Hello ${value}` }
  4. module getName { export var value = ‘Eugene’ } module

    greeting { import value from getName export `Hello ${value}` }
  5. module getName { export var value = ‘Eugene’ } module

    greeting { import value from getName export `Hello ${value}` }
  6. // utils.js export const inc = number => number +

    1 // module.js import { inc } from ‘./utils’ export default inc(1) // main.js import two from ‘./module’ two === 2 // true
  7. // utils.js export const inc = number => number +

    1 // module.js import { inc } from ‘./utils’ export default inc(1) // main.js import two from ‘./module’ two === 2 // true
  8. // utils.js export const inc = number => number +

    1 // module.js import { inc } from ‘./utils’ export default inc(1) // main.js import two from ‘./module’ two === 2 // true
  9. // utils.js export const inc = number => number +

    1 // module.js import { inc } from ‘./utils’ export default inc(1) // main.js import two from ‘./module’ two === 2 // true
  10. // jquery.js module.exports = jQuery // utils.js const inc =

    number => number + 1 module.exports = { inc }
  11. // page.js const page = (require) => { const $

    = require(‘./jquery’) const utils = require(‘./utils’) utils.inc(1) // 2 } require.ensure([‘./jquery’, ‘./utils’], page) CommonJS
  12. AMD // page.js const page = ($, utils) => {

    utils.inc(1) // 2 } require([‘./jquery’, ‘./utils’], page)
  13. Webpack 1.x.x (coming in 2.0.0!) does not natively support or

    understand ES6 modules. However, you can get around that by using a transpiler, like Babel, to turn the ES6 import syntax into CommonJs or AMD modules. This approach is effective but has one important caveat for dynamic loading. The module syntax addition (import x from 'foo') is intentionally designed to be statically analyzable, which means that you cannot do dynamic imports. // INVALID!!!!!!!!! ['lodash', 'backbone'].forEach(name => import name ) Luckily, there is a JavaScript API “loader” specification being written to handle the dynamic use case: System.load (or System.import ). This API will be the native equivalent to the above require variations. However, most transpilers do not support converting System.load calls to require.ensure so you have to do that directly if you want to make use of dynamic code splitting. ES6 Modules