• This repository has been archived on 08/Feb/2022
  • Stars
    star
    1,483
  • Rank 31,688 (Top 0.7 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created almost 9 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Builds multi-config webpack projects in parallel

npm version Build Status CircleCI Coverage Status Install Size

parallel-webpack - Building multi-configs in parallel

parallel-webpack allows you to run multiple webpack builds in parallel, spreading the work across your processors and thus helping to significantly speed up your build. For us at trivago it has reduced the build from 16 minutes to just 2 minutes - for 32 variants. That performance improvement naturally comes at the expense of utilizing all available CPU cores.

Installation

npm install parallel-webpack --save-dev

You can choose whether to install parallel-webpack globally or locally. At trivago, we keep our build tools locally to the project so that we have full control over its versions.

Basic example

Given a webpack.config.js like this:

var path = require('path');
module.exports = [{
    entry: './pageA.js',
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: 'pageA.bundle.js'
    }
}, {
    entry: './pageB.js',
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: 'pageB.bundle.js'
    }
}];

parallel-webpack will run both specified builds in parallel.

Variants example

Sometimes, just using different configurations like above won't be enough and what you really want or need is the same configuration with some adjustments. parallel-webpack can help you with generating those configuration variants as well.

var createVariants = require('parallel-webpack').createVariants;

// Those options will be mixed into every variant
// and passed to the `createConfig` callback.
var baseOptions = {
    preferredDevTool: process.env.DEVTOOL || 'eval'
};

// This object defines the potential option variants
// the key of the object is used as the option name, its value must be an array
// which contains all potential values of your build.
var variants = {
    minified: [true, false],
    debug: [true, false],
    target: ['commonjs2', 'var', 'umd', 'amd']
};

function createConfig(options) {
    var plugins = [
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.DefinePlugin({
            DEBUG: JSON.stringify(JSON.parse(options.debug))
        })
    ];
    if(options.minified) {
        plugins.push(new webpack.optimize.UglifyJsPlugin({
            sourceMap: false,
            compress: {
                warnings: false
            }
        }));
    }
    return {
        entry: './index.js',
        devtool: options.preferredDevTool,
        output: {
            path: './dist/',
            filename: 'MyLib.' +
                options.target +
                (options.minified ? '.min' : '') +
                (options.debug ? '.debug' : '')
                + '.js',
            libraryTarget: options.target
        },
        plugins: plugins
    };
}

module.exports = createVariants(baseOptions, variants, createConfig);

The above configuration will create 16 variations of the build for you, which parallel-webpack will distribute among your processors for building.

[WEBPACK] Building 16 targets in parallel
[WEBPACK] Started building MyLib.umd.js
[WEBPACK] Started building MyLib.umd.min.js
[WEBPACK] Started building MyLib.umd.debug.js
[WEBPACK] Started building MyLib.umd.min.debug.js

[WEBPACK] Started building MyLib.amd.js
[WEBPACK] Started building MyLib.amd.min.js
[WEBPACK] Started building MyLib.amd.debug.js
[WEBPACK] Started building MyLib.amd.min.debug.js

[WEBPACK] Started building MyLib.commonjs2.js
[WEBPACK] Started building MyLib.commonjs2.min.js
[WEBPACK] Started building MyLib.commonjs2.debug.js
[WEBPACK] Started building MyLib.commonjs2.min.debug.js

[WEBPACK] Started building MyLib.var.js
[WEBPACK] Started building MyLib.var.min.js
[WEBPACK] Started building MyLib.var.debug.js
[WEBPACK] Started building MyLib.var.min.debug.js

Running the watcher

One of the features that made webpack so popular is certainly its watcher which continously rebuilds your application.

When using parallel-webpack, you can easily use the same feature as well by specifying the --watch option on the command line:

parallel-webpack --watch

Specifying retry limits

As a side-effect of using parallel-webpack, an error will no longer lead to you having to restart webpack. Instead, parallel-webpack will keep retrying to build your application until you've fixed the problem.

While that is highly useful for development it can be a nightmare for CI builds. Thus, when building with parallel-webpack in a CI context, you should consider to use the --max-retries (or -m option) to force parallel-webpack to give up on your build after a certain amount of retries:

parallel-webpack --max-retries=3

Specifying the configuration file

When you need to use a configuration file that is not webpack.config.js, you can specify its name using the --config parameter:

parallel-webpack --config=myapp.webpack.config.js

Switch off statistics (improves performance)

While the statistics generated by Webpack are very usually very useful, they also take time to generate and print and create a lot of visual overload if you don't actually need them.

Since version 1.3.0, generating them can be turned off:

parallel-webpack --no-stats

Limiting parallelism

Under certain circumstances you might not want parallel-webpack to use all of your available CPUs for building your assets. In those cases, you can specify the parallel, or p for short, option to tell parallel-webpack how many CPUs it may use.

parallel-webpack -p=2

Configurable configuration

Sometimes, you might want to access command line arguments within your webpack.config.js in order to create a more specific configuration.

parallel-webpack will forward every parameter specified after -- to the configuration as is:

parallel-webpack -- --app=trivago

Within webpack.config.js:

console.log(process.argv);
// => [ 'node', 'parallel-webpack', '--app=trivago' ]

parallel-webpack adds the first two values to process.argv to ensure that there are no differences between various ways of invoking the webpack.config.js.

Node.js API

Just like webpack, you can also use parallel-webpack as an API from node.js (You can specify any other option used in worker-farm):

var run = require('parallel-webpack').run,
    configPath = require.resolve('./webpack.config.js');

run(configPath, {
    watch: false,
    maxRetries: 1,
    stats: true, // defaults to false
    maxConcurrentWorkers: 2 // use 2 workers
});

You can pass a notify callback as well.

var run = require('parallel-webpack').run,
    configPath = require.resolve('./webpack.config.js'),
    options = {/*...*/};

function notify() {
// do things
}

run(configPath, options, notify);

NOTE: In watch mode notify callback provided with Node.js API will run only once when all of the builds are finished.

createVariants


createVariants(baseConfig: Object, variants: Object, configCallback: Function): Object[]

Alters the given baseConfig with all possible variants and maps the result into a valid webpack configuration using the given configCallback.

createVariants(variants: Object, configCallback: Function): Object[]

Creates all possible variations as specified in the variants object and maps the result into a valid webpack configuration using the given configCallback.

createVariants(baseConfig: Object, variants: Object): Object[]

Alters the given baseConfig with all possible variants and returns it.

createVariants(variants: Object): Object[]

Creates all possible variations from the given variants and returns them as a flat array.

More Repositories

1

prettier-plugin-sort-imports

A prettier plugin to sort imports in typescript and javascript files by the provided RegEx order.
TypeScript
3,284
star
2

gollum

An n:m message multiplexer written in Go
Go
939
star
3

Heimdallr.swift

Easy to use OAuth 2 library for iOS, written in Swift.
Swift
639
star
4

cluecumber

Clear and concise reporting for the Cucumber BDD JSON format.
Java
270
star
5

Heimdall.droid

Easy to use OAuth 2 library for Android by trivago.
Kotlin
258
star
6

melody

Melody is a library for building JavaScript web applications.
JavaScript
215
star
7

Dobby

Swift helpers for mocking and stubbing
Swift
165
star
8

prettier-plugin-twig-melody

Code formatting plugin for Prettier which can handle Twig/Melody templates
JavaScript
155
star
9

cucable-plugin

Maven plugin that simplifies running Cucumber scenarios in parallel.
Java
115
star
10

triava

The triava project contains several of trivago's core libraries for Java-based projects: caching, collections, annotations, concurrency libraries and more.
Java
75
star
11

scalad

Horizontal Job Autoscaler for Nomad
Go
69
star
12

hamara

Export datasource from the existing Grafana DB into a YAML provisioning file
Go
57
star
13

tgo

trivago go utilities
Go
57
star
14

jade

A simple but yet powerful library to expose your entities through JSON API.
PHP
55
star
15

nomad-pot-driver

Nomad task driver for launching freebsd jails.
Go
54
star
16

Protector

A circuit breaker for InfluxDB
Python
48
star
17

fastutil-concurrent-wrapper

Set of concurrent wrappers around fastutil primitive maps
Java
37
star
18

babel-plugin-cloudinary

Compile cloudinary URLs at build time.
JavaScript
24
star
19

pbf-loader

Webpack loader for .proto files to be used within mapbox/pbf
JavaScript
21
star
20

rumi

trivago continuous integration executor
PHP
20
star
21

influxdbviewer

JavaScript
18
star
22

preact-hooks-testing-library

preact port of https://github.com/testing-library/react-hooks-testing-library
TypeScript
18
star
23

project-ironman

CSS skeleton / structure we used at trivago for our large scale refactoring project called Project Ironman
CSS
17
star
24

logstash-codec-protobuf

Protobuf codec for parsing data into logstash
17
star
25

samsa

A high level NodeJS stream processing library
TypeScript
17
star
26

jcha

Java Class Histogram Analyser
Java
17
star
27

hive-lambda-sting

A small library of hive UDFS using Macros to process and manipulate complex types
Java
15
star
28

reportoire

Kotlin
15
star
29

ReactiveHeimdall

Reactive wrapper around Heimdall.swift
Swift
14
star
30

chapi

chronos & marathon console client - Manage your jobs like a git repository
PHP
14
star
31

rebase

A small python library to make your data layer more robust.
Python
13
star
32

Mail-Pigeon

Mail Pigeon is a mail delivery plattform, which was created in the trivago developer days (something like the Google 20% rule) to replace some of the PHP based mailing scripts that were present that days. trivago has a lot of customers and therefore the focus laid on the fast and relyable sending process of a future mail tool. We decided to build a plattform that just can send mails in a performant manner, later on we built mail pigeon on top of it.
Java
13
star
33

boerewors

Release framework based on Python
Python
11
star
34

rtl-scss-kit

A right-to-left mixin set to create right-to-left and left-to-right css out of one scss-source
CSS
8
star
35

svn-daemon

A daemon to control a svn checkout via a web interface, written in go.
Go
8
star
36

exporter-edgecast

A Prometheus exporter for the Edgecast CDN
Go
7
star
37

triversity

trivago's university collaboration tool
Vue
7
star
38

libvips-mozjpeg-amazonlinux-buildscript

Modified libvips build script which helps to build libvips with mozjpeg support on AWS 'AmazonLinux' EC2 instances.
Shell
7
star
39

junior-java-software-engineer-casestudy

Case study for Junior Software Engineer - Java Backend Services
6
star
40

exporter-chinacache

A Prometheus exporter for the Chinacache CDN
Go
5
star
41

express-booking-selfconnect-api

trivago Express Booking - Self Connect API
PHP
5
star
42

recsys-challenge-2019-benchmarks

Run benchmark algorithms on the data sets of the RecSys Challenge 2019. https://recsys.trivago.cloud/
Python
4
star
43

create-melody-app

Create Melody apps quickly from the command line.
JavaScript
3
star
44

oauth-sdk-js

A tiny javascript library that allows to log in/register with trivago
JavaScript
2
star
45

melody-web

Public web site of the Melody JavaScript framework
HTML
2
star
46

.github

@trivago organization
2
star
47

intellij-idea-plugin

Internal IntelliJ plugin by trivago. Quick and dirty implementation.
Kotlin
1
star
48

melody-template

Melody Starter Example
JavaScript
1
star
49

mod_whatkilledus

Copy of sources from https://emptyhammock.com/projects/httpd/diag/
C
1
star
50

TriMamba

Collection of tools aimed to gather event information from different sources into a database
JavaScript
1
star