WarsawJS Slides: Webpack - what is it and how to make it work

We talk about JavaScript. Each month in Warsaw, Poland.

Speaker

Andriy Mykulyak

"Webpack - what is it and how to make it work"

2016-05-11

LinkedIn Gmail

Who am I ?

IT Expert in the Departament of Electronic Banking Applications

PKO BP, Warsaw

Full stack developer. Likes challenges. Wants to finish in Ironman.

"Do. Or do not. There is no try" (c) Yoda

What is Webpack ?

Build systems are software tools designed to automate the process of program compilation.

What can you use it for ?

Advantages

Disadvantages

The IDEA

Everything is a JavaScript module.

If something is not a JavaScript module, then make it one !

Anatomy of a build

Meet our sample application

  1. Two pages - login.html, main.html
  2. Common code in a separate JS file - common.js
  3. We're writing in ES6
  4. A separate stylesheet file - screen.css

login.js, main.js

        
            import App from './common';
            require('./screen.scss');

            class LoginApp extends App { ... }
        
    
        
            import App from './common';
            require('./screen.scss');

            class MainApp extends App { ... }
        
    

common.js

        
            import $ from 'some-external-lib';

            class App {
            ...
            }
        
    

webpack.config.js

        
            {
            entry: { login: './login', main: './main' },
            output: { path: path.join(__dirname, 'dist/js'), filename: '[name].js' },
            module: {
                loaders: [
                    { test: /\.js$/, loaders: [ 'babel-loader' ], query: { stage: 0 } },
                    { test: /\.scss$/, loader: 'style!css!sass?outputStyle=expanded' }
                ]
            }
            }
        
    

Anatomy of a build configuration

Webpack build is driven by configuration file. Its main sections:

Anatomy of a build configuration - development

Multiple configurations

Useful when multiple distributions should be built.

Multiple configurations during development

API for modules

Loaders

Loaders transform resources of our application, eventually transforming them into JavaScript.

Loaders - execution order

  1. preloaders from configuration
  2. loaders from configuration file
  3. loaders in the require() call
  4. postLoaders from configuration

Default rules can be overwritten in require() using !, !! or -! prefixes

Existing loaders - I

Existing loaders - II

Loaders - the simplest synchronous loader

        
            module.exports = function (resourceText) {
                // this - loader context
                // this.resolve(context, request, callback) - like require() for loaders
                // this.addDependency(file) - adds given file as a dependency
                return resourceText;
            };
        
    

Loaders - an asynchronous loader

        
            module.exports = function (resourceText) {
                var done = this.async();
                someAsyncOperation().then(function (data) {
                    done(null, data);
                }, function (err) {
                    done(err);
                });
            };
        
    

Plugins

Plugins extend functionality of the Webpack engine. They can do practically everything with the resources, and their dependency graph.

Useful plugins

Useful Links

Thank you

See you next month at WarsawJS