Skip to content

Commit 6ba995a

Browse files
authored
Compatibility with Webpack 5 (#2802)
1 parent 4210dd5 commit 6ba995a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1599
-7114
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
extends: 'airbnb',
2+
extends: ['airbnb', 'prettier'],
33
rules: {
44
'comma-dangle': ['error', 'never'],
55
'import/no-unresolved': 'off',

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ yarn-error.log*
1010
.yarn-integrity
1111
/log
1212
gemfiles/*.lock
13+
.DS_Store

.node-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10.17.0
1+
10.22.1

docs/css.md

+24-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# CSS, Sass and SCSS
22

3-
43
Webpacker supports importing CSS, Sass and SCSS files directly into your JavaScript files.
54

65
Importing and loading styles is a two step process:
@@ -15,7 +14,6 @@ Importing and loading styles is a two step process:
1514

1615
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.
1716

18-
1917
## Import global styles into your JS app
2018

2119
### Importing CSS as a multi-file pack (Webpacker v5)
@@ -60,7 +58,6 @@ Given your application installs an NPM package that provides CSS, such as `flatp
6058
@import "flatpickr/dist/flatpickr.css"
6159
```
6260

63-
6461
### Importing CSS from JS
6562

6663
```sass
@@ -79,9 +76,9 @@ import React from 'react'
7976
import helloIcon from '../hello_react/images/icon.png'
8077
import '../hello_react/styles/hello-react'
8178

82-
const Hello = props => (
83-
<div className="hello-react">
84-
<img src={helloIcon} alt="hello-icon" />
79+
const Hello = (props) => (
80+
<div className='hello-react'>
81+
<img src={helloIcon} alt='hello-icon' />
8582
<p>Hello {props.name}!</p>
8683
</div>
8784
)
@@ -94,7 +91,7 @@ Given your application installs an NPM package that provides CSS, such as `flatp
9491
```js
9592
// app/javascript/packs/application.js
9693

97-
import "flatpickr/dist/flatpickr.css"
94+
import 'flatpickr/dist/flatpickr.css'
9895
```
9996

10097
## Import scoped styles into your JS app
@@ -117,9 +114,9 @@ import React from 'react'
117114
import helloIcon from '../hello_react/images/icon.png'
118115
import styles from '../hello_react/styles/hello-react'
119116

120-
const Hello = props => (
117+
const Hello = (props) => (
121118
<div className={styles.helloReact}>
122-
<img src={helloIcon} alt="hello-icon" />
119+
<img src={helloIcon} alt='hello-icon' />
123120
<p>Hello {props.name}!</p>
124121
</div>
125122
)
@@ -142,7 +139,7 @@ Using CSS modules with a TypeScript application requires a few differences from
142139
There must also be a type definition file for these styles:
143140

144141
```typescript
145-
export const helloReact: string;
142+
export const helloReact: string
146143
```
147144
148145
You can then import the styles like this:
@@ -155,9 +152,9 @@ import React from 'react'
155152
import helloIcon from '../hello_react/images/icon.png'
156153
import * as styles from '../hello_react/styles/hello-react.module.sass'
157154

158-
const Hello = props => (
155+
const Hello = (props) => (
159156
<div className={styles.helloReact}>
160-
<img src={helloIcon} alt="hello-icon" />
157+
<img src={helloIcon} alt='hello-icon' />
161158
<p>Hello {props.name}!</p>
162159
</div>
163160
)
@@ -180,7 +177,6 @@ Then by adding these lines to your `package.json`:
180177

181178
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.
182179

183-
184180
## Link styles from your Rails views
185181

186182
Under the hood webpack uses
@@ -192,8 +188,6 @@ a separate `[pack_name].css` bundle so that in your view you can use the
192188
<%= stylesheet_pack_tag 'hello_react' %>
193189
```
194190

195-
Webpacker emits css files only if `extract_css` is set to true in webpacker.yml otherwise `stylesheet_pack_tag` returns nil.
196-
197191
## Add bootstrap
198192

199193
You can use Yarn to add bootstrap or any other modules available on npm:
@@ -218,7 +212,6 @@ Or in your app/javascript/packs/application.sass file:
218212
@import '~bootstrap/dist/css/bootstrap-theme'
219213
```
220214

221-
222215
## Post-Processing CSS
223216

224217
Webpacker out-of-the-box provides CSS post-processing using
@@ -244,8 +237,8 @@ module.exports = {
244237
## Using CSS with [vue-loader](https://github.com/vuejs/vue-loader)
245238

246239
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
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
249242
CSS for any nested components.
250243

251244
```erb
@@ -257,7 +250,6 @@ CSS for any nested components.
257250

258251
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.
259252

260-
261253
```bash
262254
yarn add resolve-url-loader
263255
```
@@ -269,7 +261,7 @@ const { environment } = require('@rails/webpacker')
269261
// resolve-url-loader must be used before sass-loader
270262
environment.loaders.get('sass').use.splice(-1, 0, {
271263
loader: 'resolve-url-loader'
272-
});
264+
})
273265

274266
module.exports = environment
275267
```
@@ -280,13 +272,14 @@ In order to get CSS to work with typescript you have two options.
280272
You can either use `require` to bypass typescript special `import`.
281273

282274
```ts
283-
const styles = require('../hello_react/styles/hello-react');
275+
const styles = require('../hello_react/styles/hello-react')
284276
```
277+
285278
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:
286279

287280
```js
288281
// app/javascript/packs/hello_react.jsx
289-
import * as styles from '../hello_react.styles/hello-react.module.scss';
282+
import * as styles from '../hello_react.styles/hello-react.module.scss'
290283
```
291284

292285
```bash
@@ -298,11 +291,13 @@ yarn add --dev typings-for-css-modules-loader
298291
const { environment } = require('@rails/webpacker')
299292

300293
// 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-
});
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+
})
308303
```

docs/webpack-dev-server.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# webpack-dev-server
22

3-
43
## HTTPS
54

65
If you're using the `webpack-dev-server` in development, you can serve your packs over HTTPS
@@ -13,23 +12,21 @@ so your web browser will display a warning/exception upon accessing the page. If
1312
in your console, simply open the link in your browser and accept the SSL exception.
1413
Now if you refresh your Rails view everything should work as expected.
1514

16-
1715
## Hot Module Replacement
1816

1917
Webpacker out-of-the-box supports HMR with `webpack-dev-server` and
2018
you can toggle it by setting options in `config/webpacker.yml`:
2119

2220
```yaml
2321
development:
24-
# ...
25-
extract_css: false
2622
# ...
2723
dev_server:
2824
# ...
2925
hmr: true
3026
inline: true
3127
# ...
3228
```
29+
3330
`dev_server/hmr` option inside `webpacker.yml`.
3431

3532
Check out this guide for more information:
@@ -83,9 +80,8 @@ server {
8380
## Customizing Logging
8481

8582
By default, the dev server will display a colored progress notification while
86-
your code is being compiled. (Under the hood, we are using `webpack-dev-server
87-
--progress --color`). However, this might cause issues if you don't use
88-
`foreman` and/or try to log webpack-dev-server's output to a file. You can
83+
your code is being compiled. (Under the hood, we are using `webpack-dev-server --progress --color`). However, this might cause issues if you don't use
84+
`foreman` and/or try to log webpack-dev-server's output to a file. You can
8985
disable this stylized output by adding `pretty: false` to your `dev_server`
9086
config:
9187

lib/install/angular.rb

-25
This file was deleted.

lib/install/coffee.rb

-27
This file was deleted.

lib/install/config/postcss.config.js

-12
This file was deleted.

lib/install/config/webpack/base.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { webpackConfig } = require('@rails/webpacker')
2+
3+
module.exports = webpackConfig
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
22

3-
const environment = require('./environment')
3+
const webpackConfig = require('./base')
44

5-
module.exports = environment.toWebpackConfig()
5+
module.exports = webpackConfig

lib/install/config/webpack/environment.js

-3
This file was deleted.
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
22

3-
const environment = require('./environment')
3+
const webpackConfig = require('./base')
44

5-
module.exports = environment.toWebpackConfig()
5+
module.exports = webpackConfig

lib/install/config/webpack/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
22

3-
const environment = require('./environment')
3+
const webpackConfig = require('./base')
44

5-
module.exports = environment.toWebpackConfig()
5+
module.exports = webpackConfig

lib/install/config/webpacker.yml

-36
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,10 @@ default: &default
1515
# Reload manifest.json on all requests so we reload latest compiled packs
1616
cache_manifest: false
1717

18-
# Extract and emit a css file
19-
extract_css: true
20-
21-
static_assets_extensions:
22-
- .jpg
23-
- .jpeg
24-
- .png
25-
- .gif
26-
- .tiff
27-
- .ico
28-
- .svg
29-
- .eot
30-
- .otf
31-
- .ttf
32-
- .woff
33-
- .woff2
34-
35-
extensions:
36-
- .mjs
37-
- .js
38-
- .sass
39-
- .scss
40-
- .css
41-
- .module.sass
42-
- .module.scss
43-
- .module.css
44-
- .png
45-
- .svg
46-
- .gif
47-
- .jpeg
48-
- .jpg
49-
5018
development:
5119
<<: *default
5220
compile: true
5321

54-
# Set to false if using HMR for CSS
55-
extract_css: true
56-
5722
# Reference: https://webpack.js.org/configuration/dev-server/
5823
dev_server:
5924
https: false
@@ -81,7 +46,6 @@ development:
8146
watch_options:
8247
ignored: '**/node_modules/**'
8348

84-
8549
test:
8650
<<: *default
8751
compile: true

0 commit comments

Comments
 (0)