webpacker 6.0.0.beta.6 → 6.0.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/jest.yml +1 -1
- data/.github/workflows/js-lint.yml +1 -1
- data/.github/workflows/ruby.yml +6 -9
- data/CHANGELOG.md +4 -20
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +5 -7
- data/README.md +109 -306
- data/docs/assets.md +135 -0
- data/docs/cloud9.md +310 -0
- data/docs/css.md +303 -0
- data/docs/deployment.md +148 -0
- data/docs/docker.md +68 -0
- data/docs/engines.md +213 -0
- data/docs/env.md +68 -0
- data/docs/es6.md +72 -0
- data/docs/folder-structure.md +66 -0
- data/docs/integrations.md +220 -0
- data/docs/misc.md +23 -0
- data/docs/props.md +187 -0
- data/docs/react.md +183 -0
- data/docs/target.md +22 -0
- data/docs/testing.md +147 -0
- data/docs/troubleshooting.md +3 -5
- data/docs/typescript.md +190 -0
- data/docs/v4-upgrade.md +142 -0
- data/docs/webpack-dev-server.md +94 -0
- data/docs/webpack.md +315 -0
- data/docs/yarn.md +23 -0
- data/lib/install/config/webpacker.yml +2 -4
- data/lib/install/examples/vue3/app.vue +27 -0
- data/lib/install/examples/vue3/hello_vue.js +15 -0
- data/lib/install/javascript/packs/application.css +9 -0
- data/lib/install/{packs/entrypoints → javascript/packs}/application.js +2 -4
- data/lib/install/template.rb +3 -3
- data/lib/webpacker/commands.rb +1 -2
- data/lib/webpacker/compiler.rb +3 -9
- data/lib/webpacker/dev_server_runner.rb +0 -2
- data/lib/webpacker/helper.rb +43 -13
- data/lib/webpacker/manifest.rb +1 -1
- data/lib/webpacker/version.rb +1 -1
- data/lib/webpacker/webpack_runner.rb +0 -1
- data/package.json +1 -1
- data/package/__tests__/development.js +1 -2
- data/package/babel/preset-react.js +62 -0
- data/package/babel/preset.js +13 -24
- data/package/environments/__tests__/base.js +5 -5
- data/package/environments/base.js +19 -19
- data/package/environments/development.js +0 -1
- data/package/environments/production.js +30 -28
- data/package/index.js +2 -7
- data/package/rules/babel.js +1 -1
- data/package/rules/coffee.js +5 -5
- data/package/rules/erb.js +3 -5
- data/package/rules/file.js +3 -5
- data/package/rules/index.js +17 -9
- data/package/rules/less.js +10 -14
- data/package/rules/sass.js +9 -13
- data/package/rules/svg.js +23 -0
- data/package/utils/get_style_rule.js +31 -27
- data/package/utils/helpers.js +0 -25
- data/test/configuration_test.rb +2 -2
- data/test/dev_server_runner_test.rb +2 -10
- data/test/helper_test.rb +39 -33
- data/test/manifest_test.rb +0 -8
- data/test/mounted_app/test/dummy/config/webpacker.yml +2 -2
- data/test/test_app/app/{packs/entrypoints → javascript/packs}/application.js +1 -1
- data/test/test_app/app/{packs/entrypoints → javascript/packs}/multi_entry.css +0 -0
- data/test/test_app/app/{packs/entrypoints → javascript/packs}/multi_entry.js +0 -0
- data/test/test_app/config/webpacker.yml +2 -2
- data/test/test_app/public/packs/manifest.json +0 -7
- metadata +36 -17
- data/6_0_upgrade.md +0 -62
- data/config/README.md +0 -3
- data/config/webpacker.yml +0 -1
- data/package/__tests__/index.js +0 -9
- data/package/rules/raw.js +0 -5
- data/package/rules/stylus.js +0 -26
data/docs/assets.md
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
# Assets
|
2
|
+
|
3
|
+
|
4
|
+
Static assets like images and fonts support is enabled out-of-box
|
5
|
+
and you can link them into your JavaScript app code and have them
|
6
|
+
compiled automatically.
|
7
|
+
|
8
|
+
|
9
|
+
## Import from node modules
|
10
|
+
|
11
|
+
You can also import styles from `node_modules` using the following syntax.
|
12
|
+
Please note that your styles will always be extracted into `[pack_name].css`:
|
13
|
+
|
14
|
+
```sass
|
15
|
+
// app/javascript/styles.sass
|
16
|
+
// ~ to tell webpack that this is not a relative import:
|
17
|
+
|
18
|
+
@import '~@material/animation/mdc-animation'
|
19
|
+
@import '~bootstrap/dist/css/bootstrap'
|
20
|
+
```
|
21
|
+
|
22
|
+
```js
|
23
|
+
// Your main app pack
|
24
|
+
// app/javascript/packs/app.js
|
25
|
+
|
26
|
+
import '../styles'
|
27
|
+
```
|
28
|
+
|
29
|
+
```erb
|
30
|
+
<%# In your views %>
|
31
|
+
|
32
|
+
<%= javascript_pack_tag 'app' %>
|
33
|
+
<%= stylesheet_pack_tag 'app' %>
|
34
|
+
```
|
35
|
+
|
36
|
+
|
37
|
+
## Import from Sprockets using helpers
|
38
|
+
|
39
|
+
It's possible to link to assets that have been precompiled by Sprockets. Add the `.erb` extension to your JavaScript file, then you can use Sprockets' asset helpers:
|
40
|
+
|
41
|
+
```erb
|
42
|
+
<%# app/javascript/my_pack/example.js.erb %>
|
43
|
+
|
44
|
+
<% helpers = ActionController::Base.helpers %>
|
45
|
+
const railsImagePath = "<%= helpers.image_path('rails.png') %>"
|
46
|
+
```
|
47
|
+
|
48
|
+
This is enabled by the `rails-erb-loader` loader rule in `config/webpack/loaders/erb.js`.
|
49
|
+
|
50
|
+
|
51
|
+
## Using babel module resolver
|
52
|
+
|
53
|
+
You can also use [babel-plugin-module-resolver](https://github.com/tleunen/babel-plugin-module-resolver) to reference assets directly from `app/assets/**`
|
54
|
+
|
55
|
+
```bash
|
56
|
+
yarn add babel-plugin-module-resolver
|
57
|
+
```
|
58
|
+
|
59
|
+
Specify the plugin in your `babel.config.js` with the custom root or alias. Here's an example:
|
60
|
+
|
61
|
+
```js
|
62
|
+
{
|
63
|
+
plugins: [
|
64
|
+
[require("babel-plugin-module-resolver").default, {
|
65
|
+
"root": ["./app"],
|
66
|
+
"alias": {
|
67
|
+
"assets": "./assets"
|
68
|
+
}
|
69
|
+
}]
|
70
|
+
]
|
71
|
+
}
|
72
|
+
```
|
73
|
+
|
74
|
+
And then within your javascript app code:
|
75
|
+
|
76
|
+
```js
|
77
|
+
// Note: we don't have to do any ../../ jazz
|
78
|
+
|
79
|
+
import FooImage from 'assets/images/foo-image.png'
|
80
|
+
import 'assets/stylesheets/bar'
|
81
|
+
```
|
82
|
+
|
83
|
+
|
84
|
+
## Link in your Rails views
|
85
|
+
|
86
|
+
You can also link `js|images|styles|fonts` used within your js app in rails views
|
87
|
+
using `asset_pack_path`, `asset_pack_url`, `image_pack_path`, `image_pack_url` and
|
88
|
+
`image_pack_tag` helpers. These helpers are especially useful in cases where you
|
89
|
+
want to create a `<link rel="prefetch">` or `<img />` for an asset.
|
90
|
+
|
91
|
+
```yml
|
92
|
+
app/javascript:
|
93
|
+
- packs
|
94
|
+
- app.js
|
95
|
+
- images
|
96
|
+
- calendar.png
|
97
|
+
```
|
98
|
+
|
99
|
+
```js
|
100
|
+
// app/javascript/packs/app.js (or any of your packs)
|
101
|
+
|
102
|
+
// import all image files in a folder:
|
103
|
+
require.context('../images', true)
|
104
|
+
```
|
105
|
+
|
106
|
+
```erb
|
107
|
+
<%# Rails view, for example app/views/layouts/application.html.erb %>
|
108
|
+
|
109
|
+
<img src="<%= asset_pack_path 'media/images/calendar.png' %>" />
|
110
|
+
<% # => <img src="/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
|
111
|
+
|
112
|
+
<img src="<%= asset_pack_url 'media/images/calendar.png' %>" />
|
113
|
+
<% # => <img src="https://example.com/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
|
114
|
+
|
115
|
+
<img src="<%= image_pack_path 'media/images/calendar.png' %>" />
|
116
|
+
<% # => <img src="/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
|
117
|
+
|
118
|
+
<img src="<%= image_pack_url 'media/images/calendar.png' %>" />
|
119
|
+
<% # => <img src="https://example.com/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
|
120
|
+
|
121
|
+
<%= image_pack_tag 'media/images/calendar.png' %>
|
122
|
+
<% # => <img src="/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
|
123
|
+
|
124
|
+
<%# no path used in image helpers resolves to default 'images' folder: %>
|
125
|
+
<img src="<%= image_pack_path 'calendar.png' %>" />
|
126
|
+
<% # => <img src="/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
|
127
|
+
|
128
|
+
<img src="<%= image_pack_url 'calendar.png' %>" />
|
129
|
+
<% # => <img src="https://example.com/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
|
130
|
+
|
131
|
+
<%= image_pack_tag 'calendar.png' %>
|
132
|
+
<% # => <img src="/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
|
133
|
+
```
|
134
|
+
|
135
|
+
Note you need to add a `media/` prefix (not `/media/`) to any subfolder structure you might have in `app/javascript`. See more examples in the [tests](/test/helper_test.rb#L37).
|
data/docs/cloud9.md
ADDED
@@ -0,0 +1,310 @@
|
|
1
|
+
# Webpack dev server and Rails on Cloud9
|
2
|
+
|
3
|
+
**Please note that this article is particularly relevant when
|
4
|
+
migrating the [`webpacker`] gem from v3.0.1 to v3.0.2, as described in
|
5
|
+
the [below](#binstub-versions).**
|
6
|
+
|
7
|
+
[**`[Go to tl;dr]`**](#tldr)
|
8
|
+
|
9
|
+
## Contents
|
10
|
+
|
11
|
+
- [Context](#context)
|
12
|
+
- [Binstub versions](#binstub-versions)
|
13
|
+
- [Quick solution](#quick-solution)
|
14
|
+
- [Flexible solution](#flexible-solution)
|
15
|
+
- [Sources](#sources)
|
16
|
+
- [Versions](#versions)
|
17
|
+
- [tl;dr](#tldr)
|
18
|
+
|
19
|
+
## Context
|
20
|
+
|
21
|
+
This article describes how to properly configure
|
22
|
+
[`webpack-dev-server`] with [`webpacker`] gem on a [Cloud9] workspace.
|
23
|
+
After a preliminary remark about the proper binstub version of the
|
24
|
+
`./bin/webpack-dev-server` script, this article presents two ways to
|
25
|
+
tackle the task: a simple and [quick solution](#quick-solution), which
|
26
|
+
is sufficient if we work alone on a project, and a slightly more
|
27
|
+
involved but [flexible approach](#flexible-solution), that can be
|
28
|
+
useful when several people might work in the same codebase.
|
29
|
+
|
30
|
+
[Cloud9]: https://c9.io
|
31
|
+
[`webpack-dev-server`]: https://github.com/webpack/webpack-dev-server
|
32
|
+
[`webpacker`]: https://github.com/rails/webpacker
|
33
|
+
|
34
|
+
## Binstub versions
|
35
|
+
|
36
|
+
A lot of the confusion about the [`webpack-dev-server`] options and
|
37
|
+
why they might not be properly taken into account, might be due to an
|
38
|
+
outdated version of the `./bin/webpack-dev-server` script. The script
|
39
|
+
created by the `rails webpacker:install` task of the [`webpacker`] gem
|
40
|
+
v3.0.1 ([source][v3.0.1/lib/install/bin/webpack-dev-server.tt]) is not
|
41
|
+
compatible with how v3.0.2 (sic) of the gem handles the
|
42
|
+
[`webpack-dev-server`] option flags (see full list of
|
43
|
+
[versions](#versions) below), which logically expects the
|
44
|
+
corresponding [binstub version][#833] of the script
|
45
|
+
([source][v3.0.2/exe/webpack-dev-server]). So please make sure that
|
46
|
+
you are using the [correct binstub][v3.0.2/exe/webpack-dev-server]
|
47
|
+
(the same applies to [`./bin/webpack`][v3.0.2/exe/webpack]). To be
|
48
|
+
fair, the [changelog of v3.0.2] properly mentions the change:
|
49
|
+
|
50
|
+
> - Added: Binstubs [#833]
|
51
|
+
> - (...)
|
52
|
+
> - Removed: Inline CLI args for dev server binstub, use env variables
|
53
|
+
instead
|
54
|
+
|
55
|
+
[changelog of v3.0.2]: https://github.com/rails/webpacker/blob/v3.0.2/CHANGELOG.md#302---2017-10-04
|
56
|
+
[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
|
57
|
+
[v3.0.2/exe/webpack-dev-server]: https://github.com/rails/webpacker/blob/v3.0.2/exe/webpack-dev-server
|
58
|
+
[v3.0.2/exe/webpack]: https://github.com/rails/webpacker/blob/v3.0.2/exe/webpack
|
59
|
+
[#833]: https://github.com/rails/webpacker/pull/833/files
|
60
|
+
|
61
|
+
## Quick solution
|
62
|
+
|
63
|
+
If you are working alone, the easiest way to fix the configuration of
|
64
|
+
the [`webpack-dev-server`] is to modify the `development.dev_server`
|
65
|
+
entry of the `config/webpacker.yml` file.
|
66
|
+
|
67
|
+
### `config/webpacker.yml` file
|
68
|
+
|
69
|
+
The `development.dev_server` entry of the `config/webpacker.yml` file
|
70
|
+
has to be changed from the following default values:
|
71
|
+
|
72
|
+
```yaml
|
73
|
+
dev_server:
|
74
|
+
https: false
|
75
|
+
host: localhost
|
76
|
+
port: 3035
|
77
|
+
public: localhost:3035
|
78
|
+
hmr: false
|
79
|
+
# Inline should be set to true if using HMR
|
80
|
+
inline: true
|
81
|
+
overlay: true
|
82
|
+
disable_host_check: true
|
83
|
+
use_local_ip: false
|
84
|
+
```
|
85
|
+
|
86
|
+
into these custom configuration:
|
87
|
+
|
88
|
+
```yaml
|
89
|
+
dev_server:
|
90
|
+
https: true
|
91
|
+
host: localhost
|
92
|
+
port: 8082
|
93
|
+
public: your-workspace-name-yourusername.c9users.io:8082
|
94
|
+
hmr: false
|
95
|
+
inline: false
|
96
|
+
overlay: true
|
97
|
+
disable_host_check: true
|
98
|
+
use_local_ip: false
|
99
|
+
```
|
100
|
+
|
101
|
+
You can obtain the value `your-workspace-name-yourusername.c9users.io`
|
102
|
+
for your [Cloud9] workspace with `echo ${C9_HOSTNAME}`.
|
103
|
+
|
104
|
+
There are four main differences with the approaches found in the
|
105
|
+
mentioned [sources](#sources):
|
106
|
+
|
107
|
+
- Some solutions suggested to set the [`host`][devserver-host] option
|
108
|
+
to `your-workspace-name-yourusername.c9users.io`, which required to
|
109
|
+
add a line to the `/etc/hosts` file by running `echo "0.0.0.0
|
110
|
+
${C9_HOSTNAME}" | sudo tee -a /etc/hosts`. This was only necessary
|
111
|
+
due to restrictions in previous versions of [`webpacker`] and how
|
112
|
+
the value of the [`public`][devserver-public] setting was
|
113
|
+
calculated. Currently it is [no longer necessary][pr-comment-hosts]
|
114
|
+
to modify the `/etc/hosts` file because the [`host`][devserver-host]
|
115
|
+
setting can be kept as `localhost`.
|
116
|
+
|
117
|
+
[pr-comment-hosts]: https://github.com/rails/webpacker/pull/1033#pullrequestreview-78992024
|
118
|
+
|
119
|
+
- Some solutions stressed the need to set the
|
120
|
+
[`https`][devserver-https] option to `false` but this failed with
|
121
|
+
`net::ERR_ABORTED` in the browser console and raised the following
|
122
|
+
exception in the server when the client tried to get the
|
123
|
+
JavaScript sources:
|
124
|
+
|
125
|
+
```
|
126
|
+
#<OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=unknown state>
|
127
|
+
```
|
128
|
+
|
129
|
+
Setting `https: true` removes the issue.
|
130
|
+
|
131
|
+
- By leaving the [`inline`][devserver-inline] option to the default
|
132
|
+
`false` value, the live compilation still works but the browser
|
133
|
+
console constantly reports the following error:
|
134
|
+
|
135
|
+
```
|
136
|
+
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.
|
137
|
+
Origin 'https://your-workspace-name-yourusername.c9users.io' is therefore not allowed access. The response had HTTP status code 503.
|
138
|
+
```
|
139
|
+
|
140
|
+
Setting `inline: false` removes the issue.
|
141
|
+
|
142
|
+
|
143
|
+
- None of the solutions suggested to set the
|
144
|
+
[`public`][devserver-public] option in the `config/webpacker.yml`
|
145
|
+
file and some suggested to pass it to the `webpack-dev-server`
|
146
|
+
command line. By setting it in the configuration file we don't need
|
147
|
+
to care about it in the terminal.
|
148
|
+
|
149
|
+
[devserver-host]: https://webpack.js.org/configuration/dev-server/#devserver-host
|
150
|
+
[devserver-https]: https://webpack.js.org/configuration/dev-server/#devserver-https
|
151
|
+
[devserver-inline]: https://webpack.js.org/configuration/dev-server/#devserver-inline
|
152
|
+
[devserver-public]: https://webpack.js.org/configuration/dev-server/#devserver-public
|
153
|
+
|
154
|
+
With this configuration, running as usual `./bin/webpack-dev-server`
|
155
|
+
in one terminal and `./bin/rails s -b $IP -p $PORT` in another should
|
156
|
+
work.
|
157
|
+
|
158
|
+
## Flexible solution
|
159
|
+
|
160
|
+
The previous solution is useful and fast to implement, but if you are
|
161
|
+
working with other people on the same repo it can be tricky to
|
162
|
+
maintain the proper configuration in the `config/webpacker.yml` file.
|
163
|
+
Moreover, the hostname of your [Cloud9] workspace is hardcoded, so
|
164
|
+
that the configuration is not portable.
|
165
|
+
|
166
|
+
A hint about another way to configure the `webpack-dev-server` can be
|
167
|
+
found in the [README of this repo][`webpacker` documentation]:
|
168
|
+
|
169
|
+
> You can use environment variables as options supported by
|
170
|
+
> webpack-dev-server in the form `WEBPACKER_DEV_SERVER_<OPTION>`.
|
171
|
+
> Please note that these environment variables will always take
|
172
|
+
> precedence over the ones already set in the configuration file.
|
173
|
+
|
174
|
+
Note that when the configuration of the [`webpack-dev-server`] is
|
175
|
+
tweaked with ENV variables, those same values _have to be passed_ to
|
176
|
+
the `rails server` process as well in order to let it use the _same_
|
177
|
+
configuration.
|
178
|
+
|
179
|
+
Taking that into account, a flexible solution can be implemented using
|
180
|
+
[`foreman`] with the following `Procfile.dev`:
|
181
|
+
|
182
|
+
[`foreman`]: https://github.com/ddollar/foreman
|
183
|
+
|
184
|
+
```Procfile
|
185
|
+
web: ./bin/rails server -b ${RAILS_SERVER_BINDING:-localhost} -p ${RAILS_SERVER_PORT:-3000}
|
186
|
+
webpacker: ./bin/webpack-dev-server
|
187
|
+
```
|
188
|
+
|
189
|
+
and this `bin/start` script:
|
190
|
+
|
191
|
+
```bash
|
192
|
+
#!/bin/bash
|
193
|
+
|
194
|
+
# Immediately exit script on first error
|
195
|
+
set -e -o pipefail
|
196
|
+
|
197
|
+
APP_ROOT_FOLDER="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
|
198
|
+
cd "${APP_ROOT_FOLDER}"
|
199
|
+
|
200
|
+
if [ -n "${C9_USER}" ]; then
|
201
|
+
# We are in a Cloud9 machine
|
202
|
+
|
203
|
+
# Make sure that Postgres is running
|
204
|
+
sudo service postgresql status || sudo service postgresql start
|
205
|
+
|
206
|
+
# Adapt the configuration of the webpack-dev-server
|
207
|
+
export APP_DOMAIN="${C9_HOSTNAME}"
|
208
|
+
export RAILS_SERVER_BINDING='0.0.0.0'
|
209
|
+
export RAILS_SERVER_PORT='8080'
|
210
|
+
export WEBPACKER_DEV_SERVER_PORT='8082'
|
211
|
+
export WEBPACKER_DEV_SERVER_HTTPS='true'
|
212
|
+
export WEBPACKER_DEV_SERVER_HOST="localhost"
|
213
|
+
export WEBPACKER_DEV_SERVER_PUBLIC="${C9_HOSTNAME}:${WEBPACKER_DEV_SERVER_PORT}"
|
214
|
+
export WEBPACKER_DEV_SERVER_HMR='false'
|
215
|
+
export WEBPACKER_DEV_SERVER_INLINE='false'
|
216
|
+
export WEBPACKER_DEV_SERVER_OVERLAY='true'
|
217
|
+
export WEBPACKER_DEV_SERVER_DISABLE_HOST_CHECK='true'
|
218
|
+
export WEBPACKER_DEV_SERVER_USE_LOCAL_IP='false'
|
219
|
+
fi
|
220
|
+
|
221
|
+
foreman start -f Procfile.dev
|
222
|
+
```
|
223
|
+
|
224
|
+
With these two scripts in place, the application can always be started
|
225
|
+
by running `bin/start`, in both [Cloud9] and other systems. The
|
226
|
+
trick is that by exporting the `WEBPACKER_DEV_SERVER_*` variables
|
227
|
+
before calling `foreman start`, we make sure that those values are
|
228
|
+
available to both `webpack-dev-server` and `rails server` processes.
|
229
|
+
|
230
|
+
## Sources
|
231
|
+
|
232
|
+
- ["Webpack dev server and Rails on Cloud9"][original-article] (the
|
233
|
+
original source for the present article, inspired by this
|
234
|
+
[comment][original-comment])
|
235
|
+
- ["Making Webpacker run on Cloud 9"] (GitHub issue)
|
236
|
+
- ["Anyone here got webpack-dev-server to work on Cloud 9?"] (GitHub issue)
|
237
|
+
- [`webpacker` documentation]
|
238
|
+
- [`webpacker/dev_server.rb` code]
|
239
|
+
- [`webpack-dev-server` documentation]
|
240
|
+
- ["Using Rails With Webpack in Cloud 9"] (blog article)
|
241
|
+
|
242
|
+
[original-article]: http://rbf.io/en/blog/2017/11/18/webpack-dev-server-and-rails-on-cloud9/
|
243
|
+
[original-comment]: https://github.com/rails/webpacker/issues/176#issuecomment-345532309
|
244
|
+
["Making Webpacker run on Cloud 9"]: https://github.com/rails/webpacker/issues/176
|
245
|
+
["Anyone here got webpack-dev-server to work on Cloud 9?"]: https://github.com/webpack/webpack-dev-server/issues/230
|
246
|
+
[`webpacker` documentation]: https://github.com/rails/webpacker/tree/v3.0.2#development
|
247
|
+
[`webpacker/dev_server.rb` code]: https://github.com/rails/webpacker/blob/v3.0.2/lib/webpacker/dev_server.rb#L55
|
248
|
+
[`webpack-dev-server` documentation]: https://webpack.js.org/configuration/dev-server/
|
249
|
+
["Using Rails With Webpack in Cloud 9"]: http://aalvarez.me/blog/posts/using-rails-with-webpack-in-cloud-9.html
|
250
|
+
|
251
|
+
## Versions
|
252
|
+
|
253
|
+
Since things in this ecosystem move fast, it's important to mention the
|
254
|
+
versions of the world for which this documentation is relevant:
|
255
|
+
|
256
|
+
```shell
|
257
|
+
$ egrep '^ ?(ruby|webpacker|rails) ' Gemfile.lock
|
258
|
+
rails (5.1.4)
|
259
|
+
webpacker (3.0.2)
|
260
|
+
ruby 2.4.2p198
|
261
|
+
|
262
|
+
$ yarn versions
|
263
|
+
yarn versions v1.1.0
|
264
|
+
{ http_parser: '2.7.0',
|
265
|
+
node: '8.5.0',
|
266
|
+
v8: '6.0.287.53',
|
267
|
+
uv: '1.14.1',
|
268
|
+
zlib: '1.2.11',
|
269
|
+
ares: '1.10.1-DEV',
|
270
|
+
modules: '57',
|
271
|
+
nghttp2: '1.25.0',
|
272
|
+
openssl: '1.0.2l',
|
273
|
+
icu: '59.1',
|
274
|
+
unicode: '9.0',
|
275
|
+
cldr: '31.0.1',
|
276
|
+
tz: '2017b' }
|
277
|
+
|
278
|
+
$ cat /etc/os-release | head -6
|
279
|
+
NAME="Ubuntu"
|
280
|
+
VERSION="14.04.5 LTS, Trusty Tahr"
|
281
|
+
ID=ubuntu
|
282
|
+
ID_LIKE=debian
|
283
|
+
PRETTY_NAME="Ubuntu 14.04.5 LTS"
|
284
|
+
VERSION_ID="14.04"
|
285
|
+
```
|
286
|
+
|
287
|
+
Everything was tested using Chrome Version 62.
|
288
|
+
|
289
|
+
## tl;dr
|
290
|
+
|
291
|
+
1. Make sure that you are running the [proper binstub
|
292
|
+
version](#binstub-versions) of `./bin/webpack-dev-server`.
|
293
|
+
|
294
|
+
1. Change the `development.dev_server` entry `config/webpacker.yml` file into:
|
295
|
+
|
296
|
+
```yaml
|
297
|
+
dev_server:
|
298
|
+
https: true
|
299
|
+
host: localhost
|
300
|
+
port: 8082
|
301
|
+
public: your-workspace-name-yourusername.c9users.io:8082
|
302
|
+
hmr: false
|
303
|
+
inline: false
|
304
|
+
overlay: true
|
305
|
+
disable_host_check: true
|
306
|
+
use_local_ip: false
|
307
|
+
```
|
308
|
+
|
309
|
+
1. Now running as usual `./bin/webpack-dev-server` in one terminal and
|
310
|
+
`./bin/rails s -b $IP -p $PORT` in another should work as expected.
|
data/docs/css.md
ADDED
@@ -0,0 +1,303 @@
|
|
1
|
+
# CSS, Sass and SCSS
|
2
|
+
|
3
|
+
Webpacker supports importing CSS, Sass and SCSS files directly into your JavaScript files.
|
4
|
+
|
5
|
+
Importing and loading styles is a two step process:
|
6
|
+
|
7
|
+
1. You need to tell webpack which file(s) it has to compile and know how to load
|
8
|
+
|
9
|
+
When you do `import '../scss/application.scss'`, you're telling webpack to include `application.scss` in the build. This does not mean it's going to be compiled into your javascript, only that webpack now compiles and knows how to load this file. (How that file compilation is handled is depending on how your loaders (`css-loader`, `sass-loader`, `file-loader`, etc.) are configured.)
|
10
|
+
|
11
|
+
2. You need to load those files in your views
|
12
|
+
|
13
|
+
In order to have styles load in production, you need to include `stylesheet_pack_tag` with the same name as the javascript file that imports the styles.
|
14
|
+
|
15
|
+
When you do `<%= stylesheet_pack_tag 'application' %>`, that's a run-time inclusion from Rails, where Rails gets the correct "asset path" to that file from webpack.
|
16
|
+
|
17
|
+
## Import global styles into your JS app
|
18
|
+
|
19
|
+
### Importing CSS as a multi-file pack (Webpacker v5)
|
20
|
+
|
21
|
+
When you add a CSS/SCSS/SASS file to `app/javascript/packs/` directory, make sure to use the same pack name as its complementary JavaScript pack, e.g. `application.js` and `application.css`. By Webpacker convention (as of Webpacker v5), this will bundle `application.js` and `application.scss` as part of the same entry point (also described as [a multi-file entry point in the webpack docs](https://webpack.js.org/concepts/entry-points/#single-entry-shorthand-syntax)). With this approach, you can avoid importing CSS from JS, if desired.
|
22
|
+
|
23
|
+
```
|
24
|
+
app/
|
25
|
+
javascript/
|
26
|
+
packs/
|
27
|
+
application.js
|
28
|
+
application.scss
|
29
|
+
```
|
30
|
+
|
31
|
+
### Importing CSS from CSS
|
32
|
+
|
33
|
+
You can import additional CSS/SCSS/SASS files from within a CSS file:
|
34
|
+
|
35
|
+
```
|
36
|
+
app/
|
37
|
+
javascript/
|
38
|
+
stylesheets/
|
39
|
+
application.scss
|
40
|
+
posts.scss
|
41
|
+
comments.scss
|
42
|
+
```
|
43
|
+
|
44
|
+
```css
|
45
|
+
/* app/javascript/stylesheets/application.scss */
|
46
|
+
|
47
|
+
@import './posts';
|
48
|
+
@import './comments';
|
49
|
+
```
|
50
|
+
|
51
|
+
### Importing CSS provided by an NPM package from SCSS/CSS
|
52
|
+
|
53
|
+
Given your application installs an NPM package that provides CSS, such as `flatpickr`, you can import the CSS file(s) by path from the package directory within `node_modules/`:
|
54
|
+
|
55
|
+
```js
|
56
|
+
/* app/javascript/stylesheets/application.scss */
|
57
|
+
|
58
|
+
@import "flatpickr/dist/flatpickr.css"
|
59
|
+
```
|
60
|
+
|
61
|
+
### Importing CSS from JS
|
62
|
+
|
63
|
+
```sass
|
64
|
+
// app/javascript/hello_react/styles/hello-react.sass
|
65
|
+
|
66
|
+
.hello-react
|
67
|
+
padding: 20px
|
68
|
+
font-size: 12px
|
69
|
+
```
|
70
|
+
|
71
|
+
```js
|
72
|
+
// React component example
|
73
|
+
// app/javascript/packs/hello_react.jsx
|
74
|
+
|
75
|
+
import React from 'react'
|
76
|
+
import helloIcon from '../hello_react/images/icon.png'
|
77
|
+
import '../hello_react/styles/hello-react'
|
78
|
+
|
79
|
+
const Hello = (props) => (
|
80
|
+
<div className='hello-react'>
|
81
|
+
<img src={helloIcon} alt='hello-icon' />
|
82
|
+
<p>Hello {props.name}!</p>
|
83
|
+
</div>
|
84
|
+
)
|
85
|
+
```
|
86
|
+
|
87
|
+
### Importing CSS provided by an NPM package from JS
|
88
|
+
|
89
|
+
Given your application installs an NPM package that provides CSS, such as `flatpickr`, you can import the CSS file(s) by path from the package directory within `node_modules/`. This is an alternative to importing from within a CSS file, as above:
|
90
|
+
|
91
|
+
```js
|
92
|
+
// app/javascript/packs/application.js
|
93
|
+
|
94
|
+
import 'flatpickr/dist/flatpickr.css'
|
95
|
+
```
|
96
|
+
|
97
|
+
## Import scoped styles into your JS app
|
98
|
+
|
99
|
+
Stylesheets that end with `.module.*` are treated as [CSS Modules](https://github.com/css-modules/css-modules).
|
100
|
+
|
101
|
+
```sass
|
102
|
+
// app/javascript/hello_react/styles/hello-react.module.sass
|
103
|
+
|
104
|
+
.helloReact
|
105
|
+
padding: 20px
|
106
|
+
font-size: 12px
|
107
|
+
```
|
108
|
+
|
109
|
+
```js
|
110
|
+
// React component example
|
111
|
+
// app/javascript/packs/hello_react.jsx
|
112
|
+
|
113
|
+
import React from 'react'
|
114
|
+
import helloIcon from '../hello_react/images/icon.png'
|
115
|
+
import styles from '../hello_react/styles/hello-react'
|
116
|
+
|
117
|
+
const Hello = (props) => (
|
118
|
+
<div className={styles.helloReact}>
|
119
|
+
<img src={helloIcon} alt='hello-icon' />
|
120
|
+
<p>Hello {props.name}!</p>
|
121
|
+
</div>
|
122
|
+
)
|
123
|
+
```
|
124
|
+
|
125
|
+
**Note:** Declared class is referenced as object property in JavaScript.
|
126
|
+
|
127
|
+
## Import scoped styles into your TypeScript app
|
128
|
+
|
129
|
+
Using CSS modules with a TypeScript application requires a few differences from a JavaScript app. The CSS / Sass files are the same:
|
130
|
+
|
131
|
+
```sass
|
132
|
+
// app/javascript/hello_react/styles/hello-react.module.sass
|
133
|
+
|
134
|
+
.helloReact
|
135
|
+
padding: 20px
|
136
|
+
font-size: 12px
|
137
|
+
```
|
138
|
+
|
139
|
+
There must also be a type definition file for these styles:
|
140
|
+
|
141
|
+
```typescript
|
142
|
+
export const helloReact: string
|
143
|
+
```
|
144
|
+
|
145
|
+
You can then import the styles like this:
|
146
|
+
|
147
|
+
```typescript
|
148
|
+
// React component example
|
149
|
+
// app/javascripts/packs/hello_react.tsx
|
150
|
+
|
151
|
+
import React from 'react'
|
152
|
+
import helloIcon from '../hello_react/images/icon.png'
|
153
|
+
import * as styles from '../hello_react/styles/hello-react.module.sass'
|
154
|
+
|
155
|
+
const Hello = (props) => (
|
156
|
+
<div className={styles.helloReact}>
|
157
|
+
<img src={helloIcon} alt='hello-icon' />
|
158
|
+
<p>Hello {props.name}!</p>
|
159
|
+
</div>
|
160
|
+
)
|
161
|
+
```
|
162
|
+
|
163
|
+
You can automatically generate type definitions for the styles by installing the `typed-scss-modules` as a development dependency:
|
164
|
+
|
165
|
+
```
|
166
|
+
yarn add typed-scss-modules --dev
|
167
|
+
```
|
168
|
+
|
169
|
+
Then by adding these lines to your `package.json`:
|
170
|
+
|
171
|
+
```
|
172
|
+
"scripts": {
|
173
|
+
"gen-typings": "yarn run tsm app/javascript/**/*.sass",
|
174
|
+
"watch-typings": "yarn run tsm app/javascript/**/*.sass -w"
|
175
|
+
},
|
176
|
+
```
|
177
|
+
|
178
|
+
You can generate the typings for the stylesheet by running the command `yarn gen-typings` when you've finished writing CSS, or run `yarn watch-typings` to have it automatically generate them as you go.
|
179
|
+
|
180
|
+
## Link styles from your Rails views
|
181
|
+
|
182
|
+
Under the hood webpack uses
|
183
|
+
[mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) plugin to extract all the referenced styles within your app and compile it into
|
184
|
+
a separate `[pack_name].css` bundle so that in your view you can use the
|
185
|
+
`stylesheet_pack_tag` helper.
|
186
|
+
|
187
|
+
```erb
|
188
|
+
<%= stylesheet_pack_tag 'hello_react' %>
|
189
|
+
```
|
190
|
+
|
191
|
+
## Add bootstrap
|
192
|
+
|
193
|
+
You can use Yarn to add bootstrap or any other modules available on npm:
|
194
|
+
|
195
|
+
```bash
|
196
|
+
yarn add bootstrap
|
197
|
+
```
|
198
|
+
|
199
|
+
Import Bootstrap and theme (optional) CSS in your app/javascript/packs/application.js file:
|
200
|
+
|
201
|
+
```js
|
202
|
+
import 'bootstrap/dist/css/bootstrap'
|
203
|
+
import 'bootstrap/dist/css/bootstrap-theme'
|
204
|
+
```
|
205
|
+
|
206
|
+
Or in your app/javascript/packs/application.sass file:
|
207
|
+
|
208
|
+
```sass
|
209
|
+
// ~ to tell that this is not a relative import
|
210
|
+
|
211
|
+
@import '~bootstrap/dist/css/bootstrap'
|
212
|
+
@import '~bootstrap/dist/css/bootstrap-theme'
|
213
|
+
```
|
214
|
+
|
215
|
+
## Post-Processing CSS
|
216
|
+
|
217
|
+
Webpacker out-of-the-box provides CSS post-processing using
|
218
|
+
[postcss-loader](https://github.com/postcss/postcss-loader)
|
219
|
+
and the installer sets up a standard `postcss.config.js`
|
220
|
+
file in your app root with standard plugins.
|
221
|
+
|
222
|
+
```js
|
223
|
+
module.exports = {
|
224
|
+
plugins: [
|
225
|
+
require('postcss-import'),
|
226
|
+
require('postcss-flexbugs-fixes'),
|
227
|
+
require('postcss-preset-env')({
|
228
|
+
autoprefixer: {
|
229
|
+
flexbox: 'no-2009'
|
230
|
+
},
|
231
|
+
stage: 3
|
232
|
+
})
|
233
|
+
]
|
234
|
+
}
|
235
|
+
```
|
236
|
+
|
237
|
+
## Using CSS with [vue-loader](https://github.com/vuejs/vue-loader)
|
238
|
+
|
239
|
+
Vue templates require loading the stylesheet in your application in
|
240
|
+
order for CSS to work. This is in addition to loading the JavaScript
|
241
|
+
file for the entry point. Loading the stylesheet will also load the
|
242
|
+
CSS for any nested components.
|
243
|
+
|
244
|
+
```erb
|
245
|
+
<%= stylesheet_pack_tag 'hello_vue' %>
|
246
|
+
<%= javascript_pack_tag 'hello_vue' %>
|
247
|
+
```
|
248
|
+
|
249
|
+
## Resolve url loader
|
250
|
+
|
251
|
+
Since `Sass/libsass` does not provide url rewriting, all linked assets must be relative to the output. Add the missing url rewriting using the resolve-url-loader. Place it directly after the sass-loader in the loader chain.
|
252
|
+
|
253
|
+
```bash
|
254
|
+
yarn add resolve-url-loader
|
255
|
+
```
|
256
|
+
|
257
|
+
```js
|
258
|
+
// webpack/environment.js
|
259
|
+
const { environment } = require('@rails/webpacker')
|
260
|
+
|
261
|
+
// resolve-url-loader must be used before sass-loader
|
262
|
+
environment.loaders.get('sass').use.splice(-1, 0, {
|
263
|
+
loader: 'resolve-url-loader'
|
264
|
+
})
|
265
|
+
|
266
|
+
module.exports = environment
|
267
|
+
```
|
268
|
+
|
269
|
+
## Working with TypeScript
|
270
|
+
|
271
|
+
In order to get CSS to work with typescript you have two options.
|
272
|
+
You can either use `require` to bypass typescript special `import`.
|
273
|
+
|
274
|
+
```ts
|
275
|
+
const styles = require('../hello_react/styles/hello-react')
|
276
|
+
```
|
277
|
+
|
278
|
+
You may also use the package [typings-for-css-modules-loader](https://github.com/Jimdo/typings-for-css-modules-loader) instead of `css-loader` to automatically generate typescript `.d.ts` files in order to help resolve any css/scss styles. To do that:
|
279
|
+
|
280
|
+
```js
|
281
|
+
// app/javascript/packs/hello_react.jsx
|
282
|
+
import * as styles from '../hello_react.styles/hello-react.module.scss'
|
283
|
+
```
|
284
|
+
|
285
|
+
```bash
|
286
|
+
yarn add --dev typings-for-css-modules-loader
|
287
|
+
```
|
288
|
+
|
289
|
+
```js
|
290
|
+
// webpack/environment.js
|
291
|
+
const { environment } = require('@rails/webpacker')
|
292
|
+
|
293
|
+
// replace css-loader with typings-for-css-modules-loader
|
294
|
+
environment.loaders.get('moduleSass').use = environment.loaders
|
295
|
+
.get('moduleSass')
|
296
|
+
.use.map((u) => {
|
297
|
+
if (u.loader == 'css-loader') {
|
298
|
+
return { ...u, loader: 'typings-for-css-modules-loader' }
|
299
|
+
} else {
|
300
|
+
return u
|
301
|
+
}
|
302
|
+
})
|
303
|
+
```
|