# Webpack dev server and Rails on Cloud9 **Please note that this article is particularly relevant when migrating the [`webpacker`] gem from v3.0.1 to v3.0.2, as described in the [below](#binstub-versions).** [**`[Go to tl;dr]`**](#tldr) ## Contents - [Context](#context) - [Binstub versions](#binstub-versions) - [Quick solution](#quick-solution) - [Flexible solution](#flexible-solution) - [Sources](#sources) - [Versions](#versions) - [tl;dr](#tldr) ## Context This article describes how to properly configure [`webpack-dev-server`] with [`webpacker`] gem on a [Cloud9] workspace. After a preliminary remark about the proper binstub version of the `./bin/webpack-dev-server` script, this article presents two ways to tackle the task: a simple and [quick solution](#quick-solution), which is sufficient if we work alone on a project, and a slightly more involved but [flexible approach](#flexible-solution), that can be useful when several people might work in the same codebase. [Cloud9]: https://c9.io [`webpack-dev-server`]: https://github.com/webpack/webpack-dev-server [`webpacker`]: https://github.com/rails/webpacker ## Binstub versions A lot of the confusion about the [`webpack-dev-server`] options and why they might not be properly taken into account, might be due to an outdated version of the `./bin/webpack-dev-server` script. The script created by the `rails webpacker:install` task of the [`webpacker`] gem v3.0.1 ([source][v3.0.1/lib/install/bin/webpack-dev-server.tt]) is not compatible with how v3.0.2 (sic) of the gem handles the [`webpack-dev-server`] option flags (see full list of [versions](#versions) below), which logically expects the corresponding [binstub version][#833] of the script ([source][v3.0.2/exe/webpack-dev-server]). So please make sure that you are using the [correct binstub][v3.0.2/exe/webpack-dev-server] (the same applies to [`./bin/webpack`][v3.0.2/exe/webpack]). To be fair, the [changelog of v3.0.2] properly mentions the change: > - Added: Binstubs [#833] > - (...) > - Removed: Inline CLI args for dev server binstub, use env variables instead [changelog of v3.0.2]: https://github.com/rails/webpacker/blob/v3.0.2/CHANGELOG.md#302---2017-10-04 [v3.0.1/lib/install/bin/webpack-dev-server.tt]: https://github.com/rails/webpacker/blob/v3.0.1/lib/install/bin/webpack-dev-server.tt [v3.0.2/exe/webpack-dev-server]: https://github.com/rails/webpacker/blob/v3.0.2/exe/webpack-dev-server [v3.0.2/exe/webpack]: https://github.com/rails/webpacker/blob/v3.0.2/exe/webpack [#833]: https://github.com/rails/webpacker/pull/833/files ## Quick solution If you are working alone, the easiest way to fix the configuration of the [`webpack-dev-server`] is to modify the `development.dev_server` entry of the `config/webpacker.yml` file. ### `config/webpacker.yml` file The `development.dev_server` entry of the `config/webpacker.yml` file has to be changed from the following default values: ```yaml dev_server: https: false host: localhost port: 3035 public: localhost:3035 hmr: false # Inline should be set to true if using HMR inline: true overlay: true disable_host_check: true use_local_ip: false ``` into these custom configuration: ```yaml dev_server: https: true host: localhost port: 8082 public: your-workspace-name-yourusername.c9users.io:8082 hmr: false inline: false overlay: true disable_host_check: true use_local_ip: false ``` You can obtain the value `your-workspace-name-yourusername.c9users.io` for your [Cloud9] workspace with `echo ${C9_HOSTNAME}`. There are four main differences with the approaches found in the mentioned [sources](#sources): - Some solutions suggested to set the [`host`][devserver-host] option to `your-workspace-name-yourusername.c9users.io`, which required to add a line to the `/etc/hosts` file by running `echo "0.0.0.0 ${C9_HOSTNAME}" | sudo tee -a /etc/hosts`. This was only necessary due to restrictions in previous versions of [`webpacker`] and how the value of the [`public`][devserver-public] setting was calculated. Currently it is [no longer necessary][pr-comment-hosts] to modify the `/etc/hosts` file because the [`host`][devserver-host] setting can be kept as `localhost`. [pr-comment-hosts]: https://github.com/rails/webpacker/pull/1033#pullrequestreview-78992024 - Some solutions stressed the need to set the [`https`][devserver-https] option to `false` but this failed with `net::ERR_ABORTED` in the browser console and raised the following exception in the server when the client tried to get the JavaScript sources: ``` # ``` Setting `https: true` removes the issue. - By leaving the [`inline`][devserver-inline] option to the default `false` value, the live compilation still works but the browser console constantly reports the following error: ``` Failed to load https://your-workspace-name-yourusername.c9users.io:8082/sockjs-node/info?t=1511016561187: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://your-workspace-name-yourusername.c9users.io' is therefore not allowed access. The response had HTTP status code 503. ``` Setting `inline: false` removes the issue. - None of the solutions suggested to set the [`public`][devserver-public] option in the `config/webpacker.yml` file and some suggested to pass it to the `webpack-dev-server` command line. By setting it in the configuration file we don't need to care about it in the terminal. [devserver-host]: https://webpack.js.org/configuration/dev-server/#devserver-host [devserver-https]: https://webpack.js.org/configuration/dev-server/#devserver-https [devserver-inline]: https://webpack.js.org/configuration/dev-server/#devserver-inline [devserver-public]: https://webpack.js.org/configuration/dev-server/#devserver-public With this configuration, running as usual `./bin/webpack-dev-server` in one terminal and `./bin/rails s -b $IP -p $PORT` in another should work. ## Flexible solution The previous solution is useful and fast to implement, but if you are working with other people on the same repo it can be tricky to maintain the proper configuration in the `config/webpacker.yml` file. Moreover, the hostname of your [Cloud9] workspace is hardcoded, so that the configuration is not portable. A hint about another way to configure the `webpack-dev-server` can be found in the [README of this repo][`webpacker` documentation]: > You can use environment variables as options supported by > webpack-dev-server in the form `WEBPACKER_DEV_SERVER_