webpacker 5.4.1 → 6.0.0.rc.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -1
  3. data/.github/workflows/jest.yml +5 -2
  4. data/.github/workflows/js-lint.yml +5 -2
  5. data/.github/workflows/rubocop.yml +1 -1
  6. data/.github/workflows/ruby.yml +19 -4
  7. data/.gitignore +2 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +4 -3
  10. data/CHANGELOG.md +24 -6
  11. data/CONTRIBUTING.md +1 -1
  12. data/Gemfile.lock +82 -82
  13. data/README.md +377 -172
  14. data/config/README.md +3 -0
  15. data/config/webpacker.yml +1 -0
  16. data/docs/deployment.md +9 -11
  17. data/docs/developing_webpacker.md +29 -0
  18. data/docs/troubleshooting.md +57 -25
  19. data/docs/v6_upgrade.md +75 -0
  20. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  21. data/lib/install/bin/yarn +18 -0
  22. data/lib/install/config/webpack/base.js +3 -0
  23. data/lib/install/config/webpack/development.js +2 -2
  24. data/lib/install/config/webpack/production.js +2 -2
  25. data/lib/install/config/webpack/test.js +2 -2
  26. data/lib/install/config/webpacker.yml +20 -50
  27. data/lib/install/package.json +17 -0
  28. data/lib/install/{javascript/packs → packs/entrypoints}/application.js +4 -3
  29. data/lib/install/template.rb +54 -28
  30. data/lib/tasks/webpacker/binstubs.rake +6 -4
  31. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  32. data/lib/tasks/webpacker/check_node.rake +3 -0
  33. data/lib/tasks/webpacker/check_yarn.rake +3 -0
  34. data/lib/tasks/webpacker/clobber.rake +1 -1
  35. data/lib/tasks/webpacker/compile.rake +4 -2
  36. data/lib/tasks/webpacker/info.rake +12 -10
  37. data/lib/tasks/webpacker/install.rake +6 -4
  38. data/lib/tasks/webpacker/verify_config.rake +14 -0
  39. data/lib/tasks/webpacker/verify_install.rake +1 -10
  40. data/lib/tasks/webpacker/yarn_install.rake +9 -7
  41. data/lib/tasks/webpacker.rake +2 -11
  42. data/lib/tasks/yarn.rake +36 -0
  43. data/lib/webpacker/commands.rb +21 -16
  44. data/lib/webpacker/compiler.rb +16 -9
  45. data/lib/webpacker/configuration.rb +8 -32
  46. data/lib/webpacker/dev_server.rb +6 -0
  47. data/lib/webpacker/dev_server_runner.rb +28 -4
  48. data/lib/webpacker/helper.rb +47 -81
  49. data/lib/webpacker/instance.rb +4 -0
  50. data/lib/webpacker/manifest.rb +2 -3
  51. data/lib/webpacker/railtie.rb +1 -2
  52. data/lib/webpacker/runner.rb +1 -1
  53. data/lib/webpacker/version.rb +1 -1
  54. data/lib/webpacker/webpack_runner.rb +6 -0
  55. data/lib/webpacker.rb +1 -1
  56. data/package/__tests__/config.js +5 -37
  57. data/package/__tests__/development.js +14 -15
  58. data/package/__tests__/env.js +16 -4
  59. data/package/__tests__/index.js +9 -0
  60. data/package/__tests__/production.js +6 -6
  61. data/package/__tests__/staging.js +7 -6
  62. data/package/__tests__/test.js +4 -5
  63. data/package/babel/preset.js +54 -0
  64. data/package/config.js +6 -14
  65. data/package/env.js +13 -4
  66. data/package/environments/__tests__/base.js +20 -52
  67. data/package/environments/base.js +68 -128
  68. data/package/environments/development.js +50 -44
  69. data/package/environments/production.js +66 -64
  70. data/package/environments/test.js +2 -2
  71. data/package/index.js +15 -8
  72. data/package/inliningCss.js +7 -0
  73. data/package/rules/babel.js +10 -8
  74. data/package/rules/coffee.js +6 -0
  75. data/package/rules/erb.js +15 -0
  76. data/package/rules/file.js +21 -19
  77. data/package/rules/index.js +16 -18
  78. data/package/rules/less.js +22 -0
  79. data/package/rules/raw.js +5 -0
  80. data/package/rules/sass.js +9 -10
  81. data/package/rules/stylus.js +26 -0
  82. data/package/utils/get_style_rule.js +28 -36
  83. data/package/utils/helpers.js +28 -35
  84. data/package.json +12 -32
  85. data/test/command_test.rb +76 -0
  86. data/test/compiler_test.rb +0 -12
  87. data/test/configuration_test.rb +4 -35
  88. data/test/dev_server_runner_test.rb +36 -6
  89. data/test/engine_rake_tasks_test.rb +39 -0
  90. data/test/helper_test.rb +79 -77
  91. data/test/manifest_test.rb +16 -0
  92. data/test/mounted_app/Rakefile +4 -0
  93. data/test/mounted_app/test/dummy/Rakefile +3 -0
  94. data/test/mounted_app/test/dummy/bin/rails +3 -0
  95. data/test/mounted_app/test/dummy/bin/rake +3 -0
  96. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  97. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  98. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  99. data/test/mounted_app/test/dummy/config.ru +5 -0
  100. data/test/mounted_app/test/dummy/package.json +7 -0
  101. data/test/rake_tasks_test.rb +1 -10
  102. data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
  103. data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.css +0 -0
  104. data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.js +0 -0
  105. data/test/test_app/config/webpacker.yml +5 -31
  106. data/test/test_app/config/webpacker_other_location.yml +79 -0
  107. data/test/test_app/config/webpacker_public_root.yml +0 -1
  108. data/test/test_app/public/packs/manifest.json +36 -18
  109. data/test/test_app/some.config.js +0 -0
  110. data/test/webpack_runner_test.rb +9 -3
  111. data/test/webpacker_test.rb +17 -0
  112. data/webpacker.gemspec +1 -1
  113. data/yarn.lock +1050 -3966
  114. metadata +59 -91
  115. data/.travis.yml +0 -43
  116. data/docs/assets.md +0 -119
  117. data/docs/cloud9.md +0 -310
  118. data/docs/css.md +0 -308
  119. data/docs/docker.md +0 -68
  120. data/docs/engines.md +0 -213
  121. data/docs/env.md +0 -63
  122. data/docs/es6.md +0 -72
  123. data/docs/folder-structure.md +0 -66
  124. data/docs/integrations.md +0 -220
  125. data/docs/misc.md +0 -23
  126. data/docs/props.md +0 -223
  127. data/docs/target.md +0 -22
  128. data/docs/testing.md +0 -136
  129. data/docs/typescript.md +0 -190
  130. data/docs/v4-upgrade.md +0 -142
  131. data/docs/webpack-dev-server.md +0 -92
  132. data/docs/webpack.md +0 -364
  133. data/docs/yarn.md +0 -23
  134. data/lib/install/angular.rb +0 -23
  135. data/lib/install/coffee.rb +0 -25
  136. data/lib/install/config/.browserslistrc +0 -1
  137. data/lib/install/config/babel.config.js +0 -76
  138. data/lib/install/config/postcss.config.js +0 -12
  139. data/lib/install/config/webpack/environment.js +0 -3
  140. data/lib/install/elm.rb +0 -39
  141. data/lib/install/erb.rb +0 -25
  142. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  143. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  144. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  145. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  146. data/lib/install/examples/angular/hello_angular.js +0 -7
  147. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  148. data/lib/install/examples/elm/Main.elm +0 -55
  149. data/lib/install/examples/elm/hello_elm.js +0 -16
  150. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  151. data/lib/install/examples/react/babel.config.js +0 -93
  152. data/lib/install/examples/react/hello_react.jsx +0 -26
  153. data/lib/install/examples/react/tsconfig.json +0 -21
  154. data/lib/install/examples/stimulus/application.js +0 -1
  155. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  156. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  157. data/lib/install/examples/svelte/app.svelte +0 -11
  158. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  159. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  160. data/lib/install/examples/typescript/tsconfig.json +0 -24
  161. data/lib/install/examples/vue/app.vue +0 -22
  162. data/lib/install/examples/vue/hello_vue.js +0 -72
  163. data/lib/install/loaders/coffee.js +0 -6
  164. data/lib/install/loaders/elm.js +0 -25
  165. data/lib/install/loaders/erb.js +0 -11
  166. data/lib/install/loaders/svelte.js +0 -9
  167. data/lib/install/loaders/vue.js +0 -6
  168. data/lib/install/react.rb +0 -18
  169. data/lib/install/stimulus.rb +0 -12
  170. data/lib/install/svelte.rb +0 -29
  171. data/lib/install/typescript.rb +0 -39
  172. data/lib/install/vue.rb +0 -49
  173. data/lib/tasks/installers.rake +0 -42
  174. data/package/config_types/__tests__/config_list.js +0 -118
  175. data/package/config_types/__tests__/config_object.js +0 -43
  176. data/package/config_types/config_list.js +0 -75
  177. data/package/config_types/config_object.js +0 -55
  178. data/package/config_types/index.js +0 -7
  179. data/package/rules/module.css.js +0 -3
  180. data/package/rules/module.sass.js +0 -8
  181. data/package/rules/node_modules.js +0 -22
  182. data/package/utils/__tests__/deep_assign.js +0 -32
  183. data/package/utils/__tests__/deep_merge.js +0 -10
  184. data/package/utils/__tests__/get_style_rule.js +0 -65
  185. data/package/utils/__tests__/objectify.js +0 -9
  186. data/package/utils/deep_assign.js +0 -22
  187. data/package/utils/deep_merge.js +0 -22
  188. data/package/utils/objectify.js +0 -3
data/docs/cloud9.md DELETED
@@ -1,310 +0,0 @@
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 DELETED
@@ -1,308 +0,0 @@
1
- # CSS, Sass and SCSS
2
-
3
-
4
- Webpacker supports importing CSS, Sass and SCSS files directly into your JavaScript files.
5
-
6
- Importing and loading styles is a two step process:
7
-
8
- 1. You need to tell webpack which file(s) it has to compile and know how to load
9
-
10
- 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.)
11
-
12
- 2. You need to load those files in your views
13
-
14
- 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.
15
-
16
- 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.
17
-
18
-
19
- ## Import global styles into your JS app
20
-
21
- ### Importing CSS as a multi-file pack (Webpacker v5)
22
-
23
- 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.
24
-
25
- ```
26
- app/
27
- javascript/
28
- packs/
29
- application.js
30
- application.scss
31
- ```
32
-
33
- ### Importing CSS from CSS
34
-
35
- You can import additional CSS/SCSS/SASS files from within a CSS file:
36
-
37
- ```
38
- app/
39
- javascript/
40
- stylesheets/
41
- application.scss
42
- posts.scss
43
- comments.scss
44
- ```
45
-
46
- ```css
47
- /* app/javascript/stylesheets/application.scss */
48
-
49
- @import './posts';
50
- @import './comments';
51
- ```
52
-
53
- ### Importing CSS provided by an NPM package from SCSS/CSS
54
-
55
- 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/`:
56
-
57
- ```js
58
- /* app/javascript/stylesheets/application.scss */
59
-
60
- @import "flatpickr/dist/flatpickr.css"
61
- ```
62
-
63
-
64
- ### Importing CSS from JS
65
-
66
- ```sass
67
- // app/javascript/hello_react/styles/hello-react.sass
68
-
69
- .hello-react
70
- padding: 20px
71
- font-size: 12px
72
- ```
73
-
74
- ```js
75
- // React component example
76
- // app/javascript/packs/hello_react.jsx
77
-
78
- import React from 'react'
79
- import helloIcon from '../hello_react/images/icon.png'
80
- import '../hello_react/styles/hello-react'
81
-
82
- const Hello = props => (
83
- <div className="hello-react">
84
- <img src={helloIcon} alt="hello-icon" />
85
- <p>Hello {props.name}!</p>
86
- </div>
87
- )
88
- ```
89
-
90
- ### Importing CSS provided by an NPM package from JS
91
-
92
- 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:
93
-
94
- ```js
95
- // app/javascript/packs/application.js
96
-
97
- import "flatpickr/dist/flatpickr.css"
98
- ```
99
-
100
- ## Import scoped styles into your JS app
101
-
102
- Stylesheets that end with `.module.*` are treated as [CSS Modules](https://github.com/css-modules/css-modules).
103
-
104
- ```sass
105
- // app/javascript/hello_react/styles/hello-react.module.sass
106
-
107
- .helloReact
108
- padding: 20px
109
- font-size: 12px
110
- ```
111
-
112
- ```js
113
- // React component example
114
- // app/javascript/packs/hello_react.jsx
115
-
116
- import React from 'react'
117
- import helloIcon from '../hello_react/images/icon.png'
118
- import styles from '../hello_react/styles/hello-react'
119
-
120
- const Hello = props => (
121
- <div className={styles.helloReact}>
122
- <img src={helloIcon} alt="hello-icon" />
123
- <p>Hello {props.name}!</p>
124
- </div>
125
- )
126
- ```
127
-
128
- **Note:** Declared class is referenced as object property in JavaScript.
129
-
130
- ## Import scoped styles into your TypeScript app
131
-
132
- Using CSS modules with a TypeScript application requires a few differences from a JavaScript app. The CSS / Sass files are the same:
133
-
134
- ```sass
135
- // app/javascript/hello_react/styles/hello-react.module.sass
136
-
137
- .helloReact
138
- padding: 20px
139
- font-size: 12px
140
- ```
141
-
142
- There must also be a type definition file for these styles:
143
-
144
- ```typescript
145
- export const helloReact: string;
146
- ```
147
-
148
- You can then import the styles like this:
149
-
150
- ```typescript
151
- // React component example
152
- // app/javascripts/packs/hello_react.tsx
153
-
154
- import React from 'react'
155
- import helloIcon from '../hello_react/images/icon.png'
156
- import * as styles from '../hello_react/styles/hello-react.module.sass'
157
-
158
- const Hello = props => (
159
- <div className={styles.helloReact}>
160
- <img src={helloIcon} alt="hello-icon" />
161
- <p>Hello {props.name}!</p>
162
- </div>
163
- )
164
- ```
165
-
166
- You can automatically generate type definitions for the styles by installing the `typed-scss-modules` as a development dependency:
167
-
168
- ```
169
- yarn add typed-scss-modules --dev
170
- ```
171
-
172
- Then by adding these lines to your `package.json`:
173
-
174
- ```
175
- "scripts": {
176
- "gen-typings": "yarn run tsm app/javascript/**/*.sass",
177
- "watch-typings": "yarn run tsm app/javascript/**/*.sass -w"
178
- },
179
- ```
180
-
181
- 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.
182
-
183
-
184
- ## Link styles from your Rails views
185
-
186
- Under the hood webpack uses
187
- [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
188
- a separate `[pack_name].css` bundle so that in your view you can use the
189
- `stylesheet_pack_tag` helper.
190
-
191
- ```erb
192
- <%= stylesheet_pack_tag 'hello_react' %>
193
- ```
194
-
195
- Webpacker emits css files only if `extract_css` is set to true in webpacker.yml otherwise `stylesheet_pack_tag` returns nil.
196
-
197
- ## Add bootstrap
198
-
199
- You can use Yarn to add bootstrap or any other modules available on npm:
200
-
201
- ```bash
202
- yarn add bootstrap
203
- ```
204
-
205
- Import Bootstrap and theme (optional) CSS in your app/javascript/packs/application.js file:
206
-
207
- ```js
208
- import 'bootstrap/dist/css/bootstrap'
209
- import 'bootstrap/dist/css/bootstrap-theme'
210
- ```
211
-
212
- Or in your app/javascript/packs/application.sass file:
213
-
214
- ```sass
215
- // ~ to tell that this is not a relative import
216
-
217
- @import '~bootstrap/dist/css/bootstrap'
218
- @import '~bootstrap/dist/css/bootstrap-theme'
219
- ```
220
-
221
-
222
- ## Post-Processing CSS
223
-
224
- Webpacker out-of-the-box provides CSS post-processing using
225
- [postcss-loader](https://github.com/postcss/postcss-loader)
226
- and the installer sets up a standard `postcss.config.js`
227
- file in your app root with standard plugins.
228
-
229
- ```js
230
- module.exports = {
231
- plugins: [
232
- require('postcss-import'),
233
- require('postcss-flexbugs-fixes'),
234
- require('postcss-preset-env')({
235
- autoprefixer: {
236
- flexbox: 'no-2009'
237
- },
238
- stage: 3
239
- })
240
- ]
241
- }
242
- ```
243
-
244
- ## Using CSS with [vue-loader](https://github.com/vuejs/vue-loader)
245
-
246
- Vue templates require loading the stylesheet in your application in
247
- order for CSS to work. This is in addition to loading the JavaScript
248
- file for the entry point. Loading the stylesheet will also load the
249
- CSS for any nested components.
250
-
251
- ```erb
252
- <%= stylesheet_pack_tag 'hello_vue' %>
253
- <%= javascript_pack_tag 'hello_vue' %>
254
- ```
255
-
256
- ## Resolve url loader
257
-
258
- 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.
259
-
260
-
261
- ```bash
262
- yarn add resolve-url-loader
263
- ```
264
-
265
- ```js
266
- // webpack/environment.js
267
- const { environment } = require('@rails/webpacker')
268
-
269
- // resolve-url-loader must be used before sass-loader
270
- environment.loaders.get('sass').use.splice(-1, 0, {
271
- loader: 'resolve-url-loader'
272
- });
273
-
274
- module.exports = environment
275
- ```
276
-
277
- ## Working with TypeScript
278
-
279
- In order to get CSS to work with typescript you have two options.
280
- You can either use `require` to bypass typescript special `import`.
281
-
282
- ```ts
283
- const styles = require('../hello_react/styles/hello-react');
284
- ```
285
- 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:
286
-
287
- ```js
288
- // app/javascript/packs/hello_react.jsx
289
- import * as styles from '../hello_react.styles/hello-react.module.scss';
290
- ```
291
-
292
- ```bash
293
- yarn add --dev typings-for-css-modules-loader
294
- ```
295
-
296
- ```js
297
- // webpack/environment.js
298
- const { environment } = require('@rails/webpacker')
299
-
300
- // replace css-loader with typings-for-css-modules-loader
301
- environment.loaders.get('moduleSass').use = environment.loaders.get('moduleSass').use.map((u) => {
302
- if(u.loader == 'css-loader') {
303
- return { ...u, loader: 'typings-for-css-modules-loader' };
304
- } else {
305
- return u;
306
- }
307
- });
308
- ```