-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
CSS Modules HMR working again #3031
Changes from all commits
37f487c
156a068
07c8bde
1992dd3
b68964f
3a44839
9e282f6
ab5a7a5
2bb3441
605ade6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Developing Webpacker | ||
|
||
It's a little trickier for Rails developers to work on the JS code of a project like rails/webpacker. So here are some tips! | ||
|
||
## Use some test app | ||
For example, for React on Rails Changes, I'm using [shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh). | ||
This directory is the `TEST_APP_DIR`. | ||
|
||
## Fork rails/webpacker | ||
Let's call the rails/webpacker directory `WEBPACKER_DIR` which has rails/webpacker's `package.json`. | ||
|
||
## Changing the Package | ||
### Setup with Yalc | ||
Use [`yalc`](https://github.com/wclr/yalc) unless you like yak shaving weird errors. | ||
1. In `WEBPACKER_DIR`, run `yalc publish` | ||
2. In `TEST_APP_DIR`, run `yarn link @rails/webpacker` | ||
|
||
## Update the Package Code | ||
1. Make some JS change in WEBPACKER_DIR | ||
2. Run `yalc push` and your changes will be pushed to your `TEST_APP_DIR`'s node_modules. | ||
3. You may need to run `yarn` in `TEST_APP_DIR` if you added or removed dependencies of rails/webpacker. | ||
|
||
## Updating the Ruby Code | ||
|
||
For the Ruby part, just change the gem reference `TEST_APP_DIR`, like: | ||
|
||
```ruby | ||
gem "webpacker", path: "../../forks/webpacker" | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,10 @@ def preload_pack_asset(name, **options) | |
# <link rel="stylesheet" media="screen" href="/packs/calendar-8c7ce31a.chunk.css" /> | ||
# <link rel="stylesheet" media="screen" href="/packs/map-8c7ce31a.chunk.css" /> | ||
# | ||
# When using the webpack-dev-server, CSS is inlined so HMR can be turned on for CSS, | ||
# including CSS modules | ||
# <%= stylesheet_pack_tag 'calendar', 'map' %> # => nil | ||
# | ||
# DO: | ||
# | ||
# <%= stylesheet_pack_tag 'calendar', 'map' %> | ||
|
@@ -137,6 +141,8 @@ def preload_pack_asset(name, **options) | |
# <%= stylesheet_pack_tag 'calendar' %> | ||
# <%= stylesheet_pack_tag 'map' %> | ||
def stylesheet_pack_tag(*names, **options) | ||
return "" if Webpacker.inlining_css? | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would pull this up into Webpacker, so it's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. How's this look? |
||
stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options) | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,7 @@ class Webpacker::Engine < ::Rails::Engine | |
config.webpacker = ActiveSupport::OrderedOptions.new | ||
|
||
initializer "webpacker.proxy" do |app| | ||
insert_middleware = Webpacker.config.dev_server.present? rescue nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to rescue and swallow errors here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think the local variable explains anything. Should just inline into the conditional. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. |
||
if insert_middleware | ||
if Webpacker.config.dev_server.present? | ||
app.middleware.insert_before 0, | ||
Rails::VERSION::MAJOR >= 5 ? | ||
Webpacker::DevServerProxy : "Webpacker::DevServerProxy", ssl_verify_none: true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,15 @@ const config = safeLoad(readFileSync(configPath), 'utf8') | |
const availableEnvironments = Object.keys(config).join('|') | ||
const regex = new RegExp(`^(${availableEnvironments})$`, 'g') | ||
|
||
// v4 of webpack-dev-server will switch to WEBPACK_DEV_SERVE | ||
// https://github.com/rails/webpacker/issues/3057 | ||
const runningWebpackDevServer = process.env.WEBPACK_DEV_SERVER === 'true' || | ||
process.env.WEBPACK_DEV_SERVE === 'true' | ||
|
||
module.exports = { | ||
railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT, | ||
nodeEnv, | ||
isProduction, | ||
isDevelopment | ||
isDevelopment, | ||
runningWebpackDevServer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. runningWebpackDevServer feels like an env value. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
const { merge } = require('webpack-merge') | ||
const webpack = require('webpack') | ||
|
||
const baseConfig = require('./base') | ||
const devServer = require('../dev_server') | ||
const { runningWebpackDevServer } = require('../env') | ||
|
||
const { outputPath: contentBase, publicPath } = require('../config') | ||
|
||
|
@@ -11,14 +11,10 @@ let devConfig = { | |
devtool: 'cheap-module-source-map' | ||
} | ||
|
||
if ( | ||
process.env.WEBPACK_DEV_SERVER && | ||
process.env.WEBPACK_DEV_SERVER !== 'undefined' | ||
) { | ||
if (runningWebpackDevServer) { | ||
if (devServer.hmr) { | ||
devConfig = merge(devConfig, { | ||
output: { filename: '[name]-[hash].js' }, | ||
plugins: [new webpack.HotModuleReplacementPlugin()] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, we always included the HotModuleReplacementPlugin even if HMR was off. So it's no change if the command line option |
||
output: { filename: '[name]-[hash].js' } | ||
}) | ||
} | ||
|
||
|
@@ -33,8 +29,9 @@ if ( | |
https: devServer.https, | ||
hot: devServer.hmr, | ||
contentBase, | ||
inline: devServer.inline, | ||
injectClient: devServer.inject_client, | ||
inline: devServer.inline || devServer.hmr, | ||
injectClient: devServer.hmr, | ||
injectHot: devServer.hmr, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's base the default "it just works" settings based on the one |
||
useLocalIp: devServer.use_local_ip, | ||
public: devServer.public, | ||
publicPath, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { runningWebpackDevServer } = require('./env') | ||
const devServer = require('./dev_server') | ||
|
||
// This logic is tied to lib/webpacker/instance.rb | ||
const inliningCss = runningWebpackDevServer && devServer.hmr | ||
|
||
module.exports = inliningCss |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recent docs on using HMR with the webpack-dev-server all suggest using the command line option rather than the plugin.