Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure webpack configuration #82

Merged
merged 3 commits into from
Nov 14, 2018
Merged

Conversation

flut1
Copy link
Collaborator

@flut1 flut1 commented Oct 17, 2018

important: this PR should only change the structure of the configuration, the contents of the config should stay the same for now.

Motivation

I've noticed before that our webpack configuration can be somewhat hard to navigate. I wanted to start working on #57, but I'm afraid that making changes in our current webpack config setup will further complicate things. That's why I would like to introduce this change first. Here are some of the problems I am trying to solve:

  • When looking for a specific config, it is unclear beforehand if you need to look in webpack.base.conf, webpack.dev.conf or webpack.prod.conf.
  • There is inconsistency on how we did development vs. production config. Sometimes config objects are split between webpack.dev.conf and webpack.prod.conf, sometimes they are in webpack.base.conf and switched using an isDevelopment flag.
  • Having the same piece config separated into multiple files (dev and prod) makes it easier to introduce unintentional differences between the two and introduce prod-only bugs.
  • Config often referred to helper functions in another file (for example, webpackHelpers). This made it difficult to debug configuration issues because you had to keep clicking back and forth between different modules.

Changes

File structure

The file structure has changed to make it clear where to look when looking for a specific piece of config:

  • webpack.conf.dev.js and webpack.conf.prod.js now only serve as entry files. They do not contain configuration
  • webpack.conf.base.js only contains top-level configuration properties (mode and devtool)
  • all other configuration has been split into the top-level objects of a webpack config file to webpack.partial.conf.<prop>.js
  • filenames have been changed to start with webpack.conf instead of ending in .conf. This will make sure that all the webpack.partial.conf files are next to each other when sorted alphabetically.

Use of helper functions

All helper functions (mostly for loaders) have been moved to be inline at the location where they are used. This will make sure all the configuration related to a single property is next to each other (as opposed to split among different modules). For example:

{
  test: /\.svg$/,
  oneOf: (() => {
    const svgoLoaderConfig = {
      loader: 'svgo-loader',
      options: {
        plugins: [
          { removeStyleElement: true },
          { removeComments: true },
          { removeDesc: true },
          { removeUselessDefs: true },
          { removeTitle: true },
          { removeMetadata: true },
          { removeComments: true },
          { cleanupIDs: { remove: true, prefix: '' } },
          { convertColors: { shorthex: false } },
        ],
      },
    };

    return [
      {
        resourceQuery: /inline/,
        use: [{ loader: 'svg-inline-loader' }, svgoLoaderConfig],
      },
      {
        use: [{ loader: 'url-loader' }, svgoLoaderConfig],
      },
    ];
  })(),
},

An iife ((() => { ... })()) is used here instead of a getSvgoLoaderConfig() at the top of the file. This is to ensure that all the config related to .svg is together.

Small things

  • A small number of conf options have been removed that are already automatically set by webpack when using the 'mode' property.
  • Comment blocks have been added in several places to create a visual separation in long configuration files (see webpack.partial.conf.module.js)

This commit is only supposed to restructure the way we write
webpack configuration. There should be no change in the actual
contents of the configuration.

The file structure has changed to make it clear where to
look when looking for a specific piece of config:
 - webpack.conf.dev.js and webpack.conf.prod.js now only serve
 as entry files. They do not contain configuration
 - webpack.conf.base.js only contains top-level configuration
 properties
 - all other configuration has been split into the top-level
 objects of a webpack config file to webpack.partial.conf.<prop>.js

The filenames have been changed to start with webpack.conf instead
of ending in .conf. This will make sure that all the
webpack.partial.conf files are next to each other when sorted
alphabetically.

All helper functions (mostly for loaders) have been moved to be
inline at the location where they are used. This will make sure
all the configuration related to a single property is next to
each other (as opposed to split among different modules)

A small number of conf options have been removed that are already
automatically set by webpack when using the 'mode' property.

Comment blocks have been added in several places to create a
visual separation in long configuration files.
Copy link

@ThaNarie ThaNarie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like the change :)

build-tools/config/webpack/webpack.conf.base.js Outdated Show resolved Hide resolved
build-tools/config/webpack/webpack.conf.base.js Outdated Show resolved Hide resolved
@jesse-mm
Copy link
Collaborator

jesse-mm commented Oct 17, 2018

Nice work and I like the approach of separation for the configuration options of webpack. 👍

Floris Bernard added 2 commits November 7, 2018 16:48
Instead of passing an isDevelopment flag to webpack.conf.base.js,
we now pass a buildType string which (by default) can be either
'development' or 'production'. This leaves room for more complex
build setups where more types of builds are desirable. The partial
files will now also receive the `buildType` string. Because the
buildType in most cases is either development or production,
the `isDevelopment` boolean is also passed to the partials for
convenience.

The `dev` and `build` objects of `config.js` have been slightly
refactored and renamed for clarity.
 - because `env` exists in both of these, it is moved one level
 up. The distinction between build types is made inside of the
 `env` object instead
 - `dev` has been renamed to `devServer`, to indicate that these
 options are specifically meant for the webpack development
 server (as opposed to development builds, which can theoratically
 also be deployed).
 - `build` has been renamed to `dist` (technically a dev server is
 also a "build", so that term is a bit too generic)
This plugin needs to be injected from webpack.conf.dev, because
the port number needs to be passed to the success message.
Added a comment to clarify this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants