Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with Webpack 5 #2802

Merged
merged 36 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b634d67
Major packages bump
gauravtiwari Aug 8, 2020
bcde488
Major upgrade
gauravtiwari Aug 8, 2020
b4701ed
New automatic API
gauravtiwari Aug 8, 2020
e2ea63a
Automatic rules
gauravtiwari Aug 8, 2020
1153bfb
Update configuration
gauravtiwari Aug 8, 2020
4ad3419
Remove loader installs
gauravtiwari Aug 8, 2020
3564dd5
Move plugins
gauravtiwari Aug 16, 2020
7624056
Restructure
gauravtiwari Aug 30, 2020
e81793b
Remove old confis
gauravtiwari Aug 30, 2020
bcaa218
Update packages
gauravtiwari Sep 29, 2020
b6e54c1
Remove extract_css
gauravtiwari Sep 29, 2020
c23caff
Remove unwanted rules
gauravtiwari Sep 29, 2020
4e4e779
Remove extra installers
gauravtiwari Sep 29, 2020
6346111
Cleanup deps
gauravtiwari Dec 6, 2020
bd5713c
Merge master
gauravtiwari Dec 13, 2020
16f73bc
Upgrade assets plugin and make things work
gauravtiwari Dec 13, 2020
8f0c048
Make eslint happy
gauravtiwari Dec 19, 2020
22aea22
Fix tests
gauravtiwari Dec 19, 2020
86d3b86
Remove extra deps
gauravtiwari Dec 19, 2020
a1e3e54
Merge branch 'master' into automatic-rules
gauravtiwari Dec 19, 2020
78da636
Upgrade to latest
gauravtiwari Dec 19, 2020
ac0f9f5
Update assets plugin
gauravtiwari Dec 19, 2020
4af653b
Use assets key
gauravtiwari Dec 19, 2020
09fdf0d
Use loader objectt
gauravtiwari Dec 20, 2020
33840e6
Update mini css extract plugin
gauravtiwari Dec 20, 2020
f7c9a95
Correct import
gauravtiwari Dec 20, 2020
35e6b4d
Implementation is an object
gauravtiwari Dec 20, 2020
2f08d6b
Remove file loader and use native assets
gauravtiwari Dec 20, 2020
c651b98
Use eslint prettier config
gauravtiwari Dec 20, 2020
a42deef
Cleanup rules
gauravtiwari Dec 20, 2020
2f64b8f
Delete node modules loader
gauravtiwari Dec 20, 2020
7d81bc4
Make mini data uri optional
gauravtiwari Dec 20, 2020
12ddd48
Make postcss optional
gauravtiwari Dec 20, 2020
41f5bc8
Make dev server available by default
gauravtiwari Dec 20, 2020
9cb22c6
Actually not a great idea
gauravtiwari Dec 20, 2020
3d541f3
Support Avif and webp
gauravtiwari Dec 20, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
extends: 'airbnb',
extends: ['airbnb', 'prettier'],
rules: {
'comma-dangle': ['error', 'never'],
'import/no-unresolved': 'off',
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ yarn-error.log*
.yarn-integrity
/log
gemfiles/*.lock
.DS_Store
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10.17.0
10.22.1
53 changes: 24 additions & 29 deletions docs/css.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# CSS, Sass and SCSS


Webpacker supports importing CSS, Sass and SCSS files directly into your JavaScript files.

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

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.


## Import global styles into your JS app

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


### Importing CSS from JS

```sass
Expand All @@ -79,9 +76,9 @@ import React from 'react'
import helloIcon from '../hello_react/images/icon.png'
import '../hello_react/styles/hello-react'

const Hello = props => (
<div className="hello-react">
<img src={helloIcon} alt="hello-icon" />
const Hello = (props) => (
<div className='hello-react'>
<img src={helloIcon} alt='hello-icon' />
<p>Hello {props.name}!</p>
</div>
)
Expand All @@ -94,7 +91,7 @@ Given your application installs an NPM package that provides CSS, such as `flatp
```js
// app/javascript/packs/application.js

import "flatpickr/dist/flatpickr.css"
import 'flatpickr/dist/flatpickr.css'
```

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

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

```typescript
export const helloReact: string;
export const helloReact: string
```

You can then import the styles like this:
Expand All @@ -155,9 +152,9 @@ import React from 'react'
import helloIcon from '../hello_react/images/icon.png'
import * as styles from '../hello_react/styles/hello-react.module.sass'

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

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.


## Link styles from your Rails views

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

Webpacker emits css files only if `extract_css` is set to true in webpacker.yml otherwise `stylesheet_pack_tag` returns nil.

## Add bootstrap

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


## Post-Processing CSS

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

Vue templates require loading the stylesheet in your application in
order for CSS to work. This is in addition to loading the JavaScript
file for the entry point. Loading the stylesheet will also load the
order for CSS to work. This is in addition to loading the JavaScript
file for the entry point. Loading the stylesheet will also load the
CSS for any nested components.

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

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.


```bash
yarn add resolve-url-loader
```
Expand All @@ -269,7 +261,7 @@ const { environment } = require('@rails/webpacker')
// resolve-url-loader must be used before sass-loader
environment.loaders.get('sass').use.splice(-1, 0, {
loader: 'resolve-url-loader'
});
})

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

```ts
const styles = require('../hello_react/styles/hello-react');
const styles = require('../hello_react/styles/hello-react')
```

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:

```js
// app/javascript/packs/hello_react.jsx
import * as styles from '../hello_react.styles/hello-react.module.scss';
import * as styles from '../hello_react.styles/hello-react.module.scss'
```

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

// replace css-loader with typings-for-css-modules-loader
environment.loaders.get('moduleSass').use = environment.loaders.get('moduleSass').use.map((u) => {
if(u.loader == 'css-loader') {
return { ...u, loader: 'typings-for-css-modules-loader' };
} else {
return u;
}
});
environment.loaders.get('moduleSass').use = environment.loaders
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @gauravtiwari

should it be loaders.get('moduleSass').use ?

const { environment, loaders } = require('@rails/webpacker')

console.log('environment', environment)
console.log('loaders', loaders)

environment undefined
loaders [
  {
    test: [

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pustovalov Hey, I think we need to overhaul the documentation. The class-based API is removed in 6.x

I will work on updating docs.

.get('moduleSass')
.use.map((u) => {
if (u.loader == 'css-loader') {
return { ...u, loader: 'typings-for-css-modules-loader' }
} else {
return u
}
})
```
10 changes: 3 additions & 7 deletions docs/webpack-dev-server.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# webpack-dev-server


## HTTPS

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


## Hot Module Replacement

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

```yaml
development:
# ...
extract_css: false
# ...
dev_server:
# ...
hmr: true
inline: true
# ...
```

`dev_server/hmr` option inside `webpacker.yml`.

Check out this guide for more information:
Expand Down Expand Up @@ -83,9 +80,8 @@ server {
## Customizing Logging

By default, the dev server will display a colored progress notification while
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
`foreman` and/or try to log webpack-dev-server's output to a file. You can
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
`foreman` and/or try to log webpack-dev-server's output to a file. You can
disable this stylized output by adding `pretty: false` to your `dev_server`
config:

Expand Down
25 changes: 0 additions & 25 deletions lib/install/angular.rb

This file was deleted.

27 changes: 0 additions & 27 deletions lib/install/coffee.rb

This file was deleted.

12 changes: 0 additions & 12 deletions lib/install/config/postcss.config.js

This file was deleted.

3 changes: 3 additions & 0 deletions lib/install/config/webpack/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { webpackConfig } = require('@rails/webpacker')

module.exports = webpackConfig
4 changes: 2 additions & 2 deletions lib/install/config/webpack/development.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')
const webpackConfig = require('./base')

module.exports = environment.toWebpackConfig()
module.exports = webpackConfig
3 changes: 0 additions & 3 deletions lib/install/config/webpack/environment.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/install/config/webpack/production.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'production'

const environment = require('./environment')
const webpackConfig = require('./base')

module.exports = environment.toWebpackConfig()
module.exports = webpackConfig
4 changes: 2 additions & 2 deletions lib/install/config/webpack/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')
const webpackConfig = require('./base')

module.exports = environment.toWebpackConfig()
module.exports = webpackConfig
36 changes: 0 additions & 36 deletions lib/install/config/webpacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,10 @@ default: &default
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false

# Extract and emit a css file
extract_css: true

static_assets_extensions:
- .jpg
- .jpeg
- .png
- .gif
- .tiff
- .ico
- .svg
- .eot
- .otf
- .ttf
- .woff
- .woff2

extensions:
- .mjs
- .js
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg

development:
<<: *default
compile: true

# Set to false if using HMR for CSS
extract_css: true

# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
Expand Down Expand Up @@ -81,7 +46,6 @@ development:
watch_options:
ignored: '**/node_modules/**'


test:
<<: *default
compile: true
Expand Down