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

The Era of Module Bundlers - JSConf Belgium 2017

The Era of Module Bundlers - JSConf Belgium 2017

This talk will walk everyone through the concept and practical usage of JavaScript module bundling and optimisation. With the emergence of advanced bundling tools like Webpack, the JavaScript ecosystem has been enriched with the right set of processes and tools required for creating production-ready builds, which is what this talk aims at covering.
This talk will also cover essential differences between task runners and bundlers with focus on advanced optimisation techniques like Tree shaking and Code splitting.

Arun Michael Dsouza

June 29, 2017
Tweet

More Decks by Arun Michael Dsouza

Other Decks in Technology

Transcript

  1. THE ERA OF MODULE BUNDLERS
    Arun Michael Dsouza
    Software Engineer, AdPushup Inc.
    github.com/arunmichaeldsouza
    twitter.com/amdsouza92
    JSConf Belgium 2017

    View full-size slide

  2. Arun Michael Dsouza
    New Delhi, India
    github.com/arunmichaeldsouza
    twitter.com/amdsouza92

    View full-size slide

  3. Ad Revenue Optimisation Platform
    adpushup.com
    Adblock Monetisation Platform
    adrecover.com

    View full-size slide

  4. THE ERA OF MODULE BUNDLERS
    http://bit.ly/Era-of-module-bundlers-demos

    View full-size slide

  5. Task Runners Module Bundlers

    View full-size slide

  6. JAVASCRIPT MODULES

    View full-size slide

  7. MAINTAINABILITY
    REUSABILITY
    EASE OF TESTING

    View full-size slide

  8. MODULE SYSTEMS IN JAVASCRIPT

    View full-size slide

  9. Immediately Invoked Function Expression

    View full-size slide

  10. http://bit.ly/block-scoping-es6

    View full-size slide

  11. Object Interface
    http://bit.ly/design-patterns-javascript

    View full-size slide

  12. AMD
    COMMON JS

    View full-size slide

  13. COMMON JS
    math.js

    View full-size slide

  14. COMMON JS
    app.js

    View full-size slide

  15. MODULE LOADERS
    Module
    Loader
    AMD
    Common JS

    View full-size slide

  16. REQUIRE JS
    AMD Module Loader
    http://bit.ly/require-js
    http://bit.ly/system-js
    SYSTEM JS
    Common JS / AMD Module Loader
    *demo

    View full-size slide

  17. http://bit.ly/require-js-examples

    View full-size slide

  18. MODULE BUNDLING
    Module
    Bundler
    Bundle
    Modules
    .css
    .js
    .js
    .png
    .scss

    View full-size slide

  19. HOW DO MODULE BUNDLERS WORK ?
    index.js
    data.js
    math.js
    api.js model.js
    db.js
    sql.js
    unit.js
    constants.js
    jsm.js
    Dependency Graph

    View full-size slide

  20. BROWSERIFY
    NPM modules on the browser side! Yay!

    View full-size slide

  21. INSTALLATION
    > npm install -g browserify
    USAGE
    > browserify app.js -o bundle.js
    *demo

    View full-size slide

  22. TRANSFORMS
    Babelify
    ES6 ES5
    http://bit.ly/browserify-transforms

    View full-size slide

  23. USING TRANSFORMS
    > browserify app.js -o bundle.js -t [ babelify --presets
    [ es2015 ] ]

    View full-size slide

  24. TASK RUNNERS

    View full-size slide

  25. BABELIFY TRANSFORM
    > npm install grunt grunt-browserify babelify babel-
    preset-es2015 —save-dev
    *demo

    View full-size slide

  26. INSTALLATION
    > npm install -g webpack
    USAGE
    > webpack app.js bundle.js

    View full-size slide

  27. WEBPACK CONFIG FILE
    *demo

    View full-size slide

  28. LOADERS
    Transform code from one form to another that
    is part of the bundle
    PLUGINS
    Act on the bundle or bundle chunks for
    customisation and optimisation
    *demo

    View full-size slide

  29. BUNDLE OPTIMISATION WITH WEBPACK

    View full-size slide

  30. BUNDLE ANALYSIS

    View full-size slide

  31. WEBPACK BUNDLE ANALYZERS
    http://bit.ly/webpack-bundle-analyzer
    http://bit.ly/webpack-chart
    http://bit.ly/webpack-visualizer
    NPM Module
    Web-based Analysers

    View full-size slide

  32. WEBPACK BUNDLE ANALYZER
    plugins: [
    new BundleAnalyzerPlugin( )
    ]
    *demo

    View full-size slide

  33. WEB-BASED BUNDLE ANALYZERS
    > webpack --profile --json > stats.json

    View full-size slide

  34. PRODUCTION READY BUNDLE

    View full-size slide

  35. UGLIFICATION
    *demo

    View full-size slide

  36. NODE ENVIRONMENT VARIABLE
    *demo

    View full-size slide

  37. WEBPACK PRODUCTION FLAG
    > webpack -p

    View full-size slide

  38. BUILD-TIME GZIPPING

    View full-size slide

  39. DYNAMIC GZIPPING

    View full-size slide

  40. BUILD-TIME GZIPPING
    > npm install compression-webpack-plugin --save-dev
    http://bit.ly/webpack-compression-plugin
    *demo

    View full-size slide

  41. TREE SHAKING
    Dead Code Elimination
    Works with ES2015 Modules

    View full-size slide

  42. babel-plugin-transform-es2015-modules-commonjs

    View full-size slide

  43. ES2015 MODULES (STATIC STRUCTURE)
    Can be tree-shaken
    COMMON JS & OTHERS (DYNAMIC STRUCTURE)
    Cannot be tree-shaken
    *demo

    View full-size slide

  44. CODE SPLITTING
    bundle.js
    styles.css page1.js page2.js

    View full-size slide

  45. CSS CODE SPLITTING
    App code with styles
    Separate CSS bundle
    Styles applied using tag<br/>css-loader<br/>style-loader<br/>Bundle with processed CSS<br/>ExtractTextWebpackPlugin<br/>

    View full-size slide

  46. INSTALLATION
    > npm install —save-dev extract-text-webpack-plugin
    USAGE
    *demo

    View full-size slide

  47. CODE SPLITTING LIBRARIES
    bundle.js
    JQuery
    Lodash
    App Code
    vendor.js
    JQuery Lodash
    bundle.js
    App Code

    View full-size slide

  48. COMMON CHUNKS PLUGIN
    http://bit.ly/common-chunks-plugin
    *demo

    View full-size slide

  49. IMPLICIT COMMON VENDOR CHUNK

    View full-size slide

  50. AUTOMATIC SPLITTING
    *demo

    View full-size slide

  51. ES2015 MODULES
    import
    export

    View full-size slide