Posted  by 

Webpack Dev Server

Webpack Dev Server Rating: 9,7/10 189 votes

In this guide, we'll dive into some of the best practices and utilities for building a production site or application.

This walkthrough stems from Tree Shaking and Development. Please ensure you are familiar with the concepts/setup introduced in those guides before continuing on.

Setup

Webpack-dev-server is configured by default to support live-reload of files as you edit your assets while the server is running. See the documentation for more use cases and options. Browser Support. While webpack-dev-server transpiles the client (browser) scripts to an ES5 state. I like to have a single command to run as for development servers, ideally both rails server and./bin/webpack-dev-server would be launched in parallel. It seems the easiest and modern way to do so nowadays is to use a Procfile.dev and overmind.

The goals of development and production builds differ greatly. In development, we want strong source mapping and a localhost server with live reloading or hot module replacement. In production, our goals shift to a focus on minified bundles, lighter weight source maps, and optimized assets to improve load time. With this logical separation at hand, we typically recommend writing separate webpack configurations for each environment.

Webpack Dev Server

While we will separate the production and development specific bits out, note that we'll still maintain a 'common' configuration to keep things DRY. In order to merge these configurations together, we'll use a utility called webpack-merge. With the 'common' configuration in place, we won't have to duplicate code within the environment-specific configurations.

Let's start by installing webpack-merge and splitting out the bits we've already worked on in previous guides:

project

webpack.common.js

webpack.dev.js

webpack.prod.js

In webpack.common.js, we now have setup our entry and output configuration and we've included any plugins that are required for both environments. In webpack.dev.js, we've set mode to development. Also, we've added the recommended devtool for that environment (strong source mapping), as well as our simple devServer configuration. Finally, in webpack.prod.js,mode is set to production which loads TerserPlugin, which was first introduced by the tree shaking guide.

Note the use of merge() calls in the environment-specific configurations to include our common configuration in webpack.dev.js and webpack.prod.js. The webpack-merge tool offers a variety of advanced features for merging but for our use case we won't need any of that.

NPM Scripts

Now, let's modify our npm scripts to use the new configuration files. For the start script, which runs webpack-dev-server, we will use webpack.dev.js, and for the build script, which runs webpack to create a production build, we will use webpack.prod.js:

package.json

Feel free to run those scripts and see how the output changes as we continue adding to our production configuration.

Specify the Mode

Many libraries will key off the process.env.NODE_ENV variable to determine what should be included in the library. For example, when process.env.NODE_ENV is not set to 'production' some libraries may add additional logging and testing to make debugging easier. However, with process.env.NODE_ENV set to 'production' they might drop or add significant portions of code to optimize how things run for your actual users. Since webpack v4, specifying mode automatically configures DefinePlugin for you:

English subtitles ← Habana Blues 1 Follower 1861 Lines I have participated in the prodcution of thes documentary as a producer. Use the following code to embed this video. See our usage guide for more details on embedding. Paste this in your document somewhere (closest to the closing body tag is preferable). Subtitles Habana Blues - subtitles english. Habana blues, 1CD (eng). Uploaded 2007-01-28, downloaded 2541x. Habana Blues tells the story of Ruy and Tito, two young Cubans who share a similar dream: tobecome music stars. Their families and the same circle of friends keep them grounded and motivated. But their lives will be transforme by an international offer to record an album and perform abroad, causing dilemmas and affecting relations with their. Habana blues english subtitles.

webpack.prod.js

Webpack Dev Server Proxy

Technically, NODE_ENV is a system environment variable that Node.js exposes into running scripts. It is used by convention to determine dev-vs-prod behavior by server tools, build scripts, and client-side libraries. Contrary to expectations, process.env.NODE_ENV is not set to 'production'within the build script webpack.config.js, see #2537. Thus, conditionals like process.env.NODE_ENV 'production' ? '[name].[contenthash].bundle.js' : '[name].bundle.js' within webpack configurations do not work as expected.

If you're using a library like react, you should actually see a significant drop in bundle size after adding DefinePlugin. Also, note that any of our local /src code can key off of this as well, so the following check would be valid:

src/index.js

Minification

webpack v4+ will minify your code by default in production mode.

Note that while the TerserPlugin is a great place to start for minification and being used by default, there are other options out there:

If you decide to try another minification plugin, just make sure your new choice also drops dead code as described in the tree shaking guide and provide it as the optimization.minimizer.

Source Mapping

We encourage you to have source maps enabled in production, as they are useful for debugging as well as running benchmark tests. That said, you should choose one with a fairly quick build speed that's recommended for production use (see devtool). For this guide, we'll use the source-map option in the production as opposed to the inline-source-map we used in the development:

webpack.prod.js

Avoid inline-*** and eval-*** use in production as they can increase bundle size and reduce the overall performance.

Minimize CSS

It is crucial to minimize your CSS for production. Please see the Minimizing for Production section.

CLI Alternatives

Some of what has been described above can also be achieved by using the command line. For example, the --optimize-minimize flag will include the TerserPlugin behind the scenes. The --define process.env.NODE_ENV='production' will do the same for the DefinePlugin instance described above. And, webpack -p will automatically invoke both those flags and thus the plugins to be included.

While these shorthand methods are nice, we usually recommend just using the configuration as it's better to understand exactly what is being done for you in both cases. The configuration also gives you more control on fine-tuning other options within both plugins.

Use webpack with a development server that provideslive reloading. This should be used for development only.

It uses webpack-dev-middleware under the hood, which providesfast in-memory access to the webpack assets.

Table of Contents

  • Usage

Getting Started

First things first, install the module:

Note: While you can install and run webpack-dev-server globally, we recommendinstalling it locally. webpack-dev-server will always use a local installationover a global one.

Usage

Webpack Dev Server Express

There are two main, recommended methods of using the module:

With the CLI

Elevator controller manual. The easiest way to use it is with the CLI. In the directory where yourwebpack.config.js is, run:

Note: Many CLI options are available with webpack-dev-server. Explore this link.

With NPM Scripts

NPM package.json scripts are a convenient and useful means to run locally installedbinaries without having to be concerned about their full paths. Simply define ascript as such:

And run the following in your terminal/console:

NPM will automagically reference the binary in node_modules for you, andexecute the file or command.

The Result

Either method will start a server instance and begin listening for connectionsfrom localhost on port 8080.

webpack-dev-server is configured by default to support live-reload of files asyou edit your assets while the server is running.

See the documentation for more use cases and options.

Browser Support

While webpack-dev-server transpiles the client (browser) scripts to an ES5state, the project only officially supports the last two versions of majorbrowsers. We simply don't have the resources to support every whackybrowser out there.

If you find a bug with an obscure / old browser, we would actively welcome aPull Request to resolve the bug.

Support

We do our best to keep Issues in the repository focused on bugs, features, andneeded modifications to the code for the module. Because of that, we ask userswith general support, 'how-to', or 'why isn't this working' questions to try oneof the other support channels that are available.

Your first-stop-shop for support for webpack-dev-server should by the excellentdocumentation for the module. If you see an opportunity for improvementof those docs, please head over to the webpack.js.org repo and open apull request.

From there, we encourage users to visit the webpack Gitter chat andtalk to the fine folks there. If your quest for answers comes up dry in chat,head over to StackOverflow and do a quick search or open a newquestion. Remember; It's always much easier to answer questions that include yourwebpack.config.js and relevant files!

If you're twitter-savvy you can tweet #webpack with your questionand someone should be able to reach out and lend a hand.

If you have discovered a 🐛, have a feature suggestion, or would like to seea modification, please feel free to create an issue on Github. Note: The issuetemplate isn't optional, so please be sure not to remove it, and please fill itout completely.

Contributing

We welcome your contributions! Please have a read of CONTRIBUTING.md for more information on how to get involved.

Attribution

This project is heavily inspired by peerigon/nof5.

License