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

stripecon_webpack.pdf

greg_808
September 24, 2018
180

 stripecon_webpack.pdf

Presentation i did at StripeConEu 2018

greg_808

September 24, 2018
Tweet

Transcript

  1. GREG WEINDL
    FULLSTACK DEVELOPER PIXELPOEMS
    [email protected]
    @greg_808


    BOOST YOUR FRONTEND WORKFLOW
    WITH WEBPACK

    View Slide

  2. WHOAMI?
    ‣ Full Stack Web Developer
    ‣ Pixelpoems Vienna
    ‣ 3 years experience as professional Web Dev
    ‣ Cats and Music Lover

    View Slide

  3. TOPICS OF THIS TALK
    ▸ Why do we need a Module Bundler/TaskRunner
    ▸ What is Webpack?
    ▸ Gulp vs. Webpack
    ▸ Webpack Basics
    ▸ Why I think Webpack is the way to go

    View Slide

  4. MODULE BUNDLER
    TASKRUNNER

    View Slide

  5. ▸ Automate recurring tasks
    ▸ Autoprefixer
    ▸ Minify CSS
    ▸ Uglify JS
    ▸ Pre and Post -processors e.g. Babel & SASS

    View Slide

  6. ▸ Remove unused CSS, JS
    ▸ Linting e.g. ESLint
    ▸ Image Optimisation
    ▸ Generate Icons

    View Slide

  7. REASONS WHY

    View Slide

  8. USER EXPERIENCE
    We want the page to load as fast as possible.
    ‣ Minification and concatenation of CSS
    ‣ Uglification and concatenation of JS
    ‣ Image Optimisation

    View Slide

  9. We want to use the latest features in a productive way.
    ▸ Enhance CSS with SASS
    ▸ Use the latest JS features with Babe
    Keep your job exiting. Be as productive as possible
    ‣ Automate tasks that can be automated (Autoprefixer,
    …)
    DEVELOPER EXPERIENCE

    View Slide

  10. WHAT IS WEBPACK

    View Slide

  11. At its core, webpack is a static module bundler for
    modern JavaScript applications. When webpack processes
    your application, it internally builds a dependency
    graph which maps every module your project needs and
    generates one or more bundles.
    https://webpack.js.org/concepts/

    View Slide

  12. ASSETS/MODULE BUNDLER
    https://webpack.js.org/

    View Slide

  13. WHAT IS GULP?

    View Slide

  14. Gulp solves the problem of repetition. Many of the
    tasks that web developers find themselves doing over
    and over on a daily basis can be simplified by
    becoming automated. Gulp is a javascript task runner
    that lets you automate tasks
    http://brandonclapp.com/what-is-gulp-js-and-why-use-it/

    View Slide

  15. GULP FILE EXAMPLE

    View Slide

  16. COMPARISON
    Initial release:
    GitHub stars:
    Weekly
    downloads:
    WEBPACK
    Mar 2012
    43.914
    4.309.345

    Gulp
    Jul 2013
    30.273
    884.969

    View Slide

  17. IT DEPENDS ON PERSONAL TASTE
    gulpfile.js
    ▸ get rid of bootstrap
    ▸ get rid of jQuery
    ▸ get rid of gulp
    ▸ get prepared for the
    shiny „new“ JavaScript
    frameworks

    webpack.dev.js
    ▸ write own grid and
    styling system use CSS-
    Grid
    ▸ write Vanilla JS, start using
    ES6-features more often
    ▸ start using web pack
    ▸ start using VUE.JS

    View Slide

  18. WHAT DOES WEBPACK
    DIFFERENTLY

    View Slide

  19. Webpack uses a dependency graph.
    When you bundle a project using webpack, it traverses
    the imports, constructing a dependency graph of the
    project and then generates output based on the
    configuration. Additionally, it's possible to define split
    points to create separate bundles within the project
    code itself.
    https://survivejs.com/webpack/what-is-webpack/

    View Slide

  20. Hot Module Replacement
    You are likely familiar with tools, such
    as LiveReload or BrowserSync already. These tools refresh
    the browser automatically as you make changes. Hot
    Module Replacement (HMR) takes things one step further.
    In the case of React, it allows the application to maintain
    its state without forcing a refresh.
    https://survivejs.com/webpack/what-is-webpack/

    View Slide

  21. WHY IS THAT GREAT?
    ▸ Since webpack is based on JavaScript code modules, it
    knows how to concatenate your assets.
    ▸ JavaScript code modules are a concept introduced
    in Gecko 1.9 (browser engine) and can be used for
    sharing code between different privileged scopes.
    ▸ This is great if you have a lot of JS files. You don’t have
    to remember the correct order to load your JS files
    webpack will sort them out and creates a single file to
    load in your index.html/page.ss.

    View Slide

  22. WHAT IS A WEBPACK MODULE
    In contrast to Node.js modules, webpack modules can
    express their dependencies in a variety of ways. A few
    examples are:
    ‣ An ES2015 import statement
    ‣ A CommonJS require() statement
    ‣ An AMD define and require statement
    ‣ An @import statement inside of a css/sass/less file.
    ‣ An image url in a stylesheet (url(...)) or html () file.
    https://webpack.js.org/concepts/modules/

    View Slide

  23. WEBPACK BASICS

    View Slide

  24. ENTRY:
    An entry point indicates which module webpack should use
    to begin building out its internal dependency graph. webpack
    will figure out which other modules and libraries that entry
    point depends on (directly and indirectly).
    entry: {
    app: ['./src/js/entry.js', './src/css/main.scss'],
    myAwesomeComponent: ['./src/components/myAwesomeComponent.js']
    },

    View Slide

  25. The output property tells webpack where to emit
    the bundles it creates and how to name these files. It
    defaults to ./dist/main.js for the main output file and to
    the ./dist folder for any other generated file.
    output: {
    filename: javascript/[name].[contenthash].js
    path: path.resolve(__dirname, „./dist“),
    },
    OUTPUT:

    View Slide

  26. LOADERS:
    Out of the box, webpack only
    understands JavaScript
    files. Loaders allow webpack to
    process other types of files and
    convert them into valid modules that
    can be consumed by your application
    and added to the dependency graph.
    {
    test: /\.(png|svg|jpe?g|gif)$/,
    loader: 'file-loader',
    options: {
    name: 'images/[name].[ext]',
    }
    },

    View Slide

  27. PLUGINS:
    While loaders are used to transform
    certain types of modules, plugins can
    be leveraged to perform a wider
    range of tasks like bundle
    optimization, asset management and
    injection of environment variables.
    new PurifyCSSPlugin({
    paths: (Files),
    purifyOptions: {
    minify: true,
    info: true,
    rejected: true,
    whitelist: ['*js*']
    }
    }),

    View Slide

  28. WEBPACK´S EXECUTION PROCESS
    https://survivejs.com/webpack/what-is-webpack/

    View Slide

  29. SHOW & EXPLAIN
    WEBPACK CONFIG

    View Slide

  30. WHAT DID WE LEARN SO FAR

    View Slide

  31. ▸ Webpack is a module bundler, but you can also use it
    running tasks as well.
    ▸ Webpack relies on a dependency graph underneath.
    Webpack traverses through the source to construct the
    graph, and it uses this information and configuration to
    generate bundles.
    ▸ Webpack relies on loaders and plugins. Loaders operate
    on a module level, while plugins rely on hooks provided by
    webpack and have the best access to its execution process.

    View Slide

  32. ▸ Webpack’s configuration describes how to transform
    assets of the graphs and what kind of output it should
    generate. Part of this information can be included in the
    source itself if features like code splitting are used.
    ▸ Hot Module Replacement (HMR) helped to popularize
    webpack. It's a feature that can enhance the development
    experience by updating code in the browser without needing
    a full page refresh.
    ▸ Webpack can generate hashes for filenames allowing you to
    invalidate past bundles as their contents change.

    View Slide

  33. DO YOU REALLY NEED TO
    SWITCH TO WEBPACK?

    View Slide

  34. ▸ If you develop JavaScript heavy applications? => Definitely
    ▸ If front end bundlers and optimisation tools are new to
    you? => Go for it
    ▸ If you want to stay up to date with all the crazy JS stuff?
    => Go for it
    ▸ If you don’t us much JS in your projects and have a
    working Gulp Grunt etc. Workflow. => Stay with what you
    got!

    View Slide

  35. DO YOU REALLY NEED TO
    SWITCH TO WEBPACK?

    View Slide

  36. ▸ Lets be honest: I got caught by the hype.
    ▸ I was in the mood for something new.
    ▸ SilverStripe uses webpack.
    ▸ VUE, React and Angular use webpack.
    ▸ As it is already version 4 I consider it to be stable
    ▸ I guess it will stay around for a while

    View Slide

  37. WHY NOT LARAVEL MIX OR POI
    ETC.

    View Slide

  38. ▸ I think that wrappers for tools are great as long as you
    know how to use the technology under the hood.
    ▸ The chance is very high that the wrapper is missing a
    feature you need. So you have to understand what is
    really going on. It may even be more in the way than
    been helpful.
    ▸ So better start with understanding the tool before you
    choose to go with a helper.

    View Slide

  39. ▸ My approach to web development:
    ▸ GET THE BASICS RIGHT AND BUILD UP FROM
    THERE

    View Slide

  40. WHAT I HAVE LEARNED ALONG
    THE WAY

    View Slide

  41. ▸ Webpack is not as hard as every one says. You can do it.
    ▸ Web development isn’t about the web developer and
    using the most shiniest tools! It is all about user
    experience and conversions.
    ▸ Always stay with the KISS principle:
    ▸ KEEP IT SIMPLE STUPID

    View Slide

  42. LINKS
    ▸ https://webpack.js.org/
    ▸ https://survivejs.com
    ▸ https://github.com/Greg808/webpackdemoStripeCon2018

    View Slide