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

Only include dev-server parts if for dev server #2644

Conversation

justin808
Copy link
Contributor

Without this change, webpack still includes things like window
in the build even if not using the webpack-dev-server.

When that happens, the bundle cannot be used for server-side rendering.

Without this change, webpack still includes things like window
in the build even if not using the webpack-dev-server.

When that happens, the bundle cannot be used for server-side rendering.
})

if (process.env.WEBPACK_DEV_SERVER) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #2629 for why this variable is there.

and

webpack/webpack-dev-server#1929

@justin808
Copy link
Contributor Author

@gauravtiwari This allows a simple setup to work for SSR. Otherwise, SSR can work if HMR and inline are set to false for the devServer. That's how I figured out this was needed.

@justin808
Copy link
Contributor Author

@gauravtiwari @rossta @JakeLaCombe any chance of a quick review on this? It blocks me from making a super cool demo of React on Rails v12 where server rendering just works, so long as the webpack dev server is not used. It's a little more complicated with that one!

@justin808
Copy link
Contributor Author

How do I fix the ruby specs?

2020-06-25_14-28-14

})

if (process.env.WEBPACK_DEV_SERVER
&& process.env.WEBPACK_DEV_SERVER !== 'undefined') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I ran the tests, for some reason, the undefined comes as a string for comparison.

test('should use development config and environment if WEBPACK_DEV_SERVER', () => {
process.env.RAILS_ENV = 'development'
process.env.NODE_ENV = 'development'
process.env.WEBPACK_DEV_SERVER = undefined
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is per the definition here: https://github.com/webpack/webpack-dev-server/pull/1929/files#diff-15fb51940da53816af13330d8ce69b4eR66

if (!process.env.WEBPACK_DEV_SERVER) {
  process.env.WEBPACK_DEV_SERVER = true;
}

@justin808
Copy link
Contributor Author

@gauravtiwari @jakeNiemiec Any feedback on why this is not getting merged?

@gauravtiwari
Copy link
Member

gauravtiwari commented Jul 8, 2020

Thanks for this @justin808 and apologies for delay.

The code looks good, the only reason I have been hesitating is the dependence on env variable. I was thinking perhaps, we should have a completely different config class for dev server which extends development.js and then in the dev server config we instantiate that class explicitly.

https://github.com/rails/webpacker/blob/master/lib/webpacker/runner.rb#L14

      @webpack_config        = File.join(@app_path, "config/webpack/#{ENV["NODE_ENV"]}.js")

This line here should be overridden in dev server runner and set to dev server config class regardless of NODE_ENV.

@webpack_config        = File.join(@app_path, "config/webpack/dev_server.js")

@justin808
Copy link
Contributor Author

@gauravtiwari I think this change is small enough that we could merge this PR and do a bigger change as you suggest next. AFAIK, I don't think the webpack-dev-server would remove that ENV value as many other apps might depend on it.

I could try to work on your suggestion as a followup PR.

For right now, it's very confusing that the HMR settings break the non-HMR non-webpack-dev-server usage of bin/webpack.

@gauravtiwari gauravtiwari merged commit 7e5083f into rails:master Jul 9, 2020
@justin808 justin808 deleted the justin808/do-not-merge-devServer-automatically branch July 13, 2020 00:32
@justin808
Copy link
Contributor Author

Thanks @gauravtiwari!

gauravtiwari pushed a commit that referenced this pull request Aug 16, 2020
* Only include dev-server parts if for dev server

Without this change, webpack still includes things like window
in the build even if not using the webpack-dev-server.

When that happens, the bundle cannot be used for server-side rendering.

* Linting

* Add jest test
@aried3r
Copy link
Contributor

aried3r commented Aug 31, 2020

This change broke our test suite, since WEBPACK_DEV_SERVER isn't set during tests anymore after this.

What exactly is the migration path from webpacker 5.1.1 to 5.2.1 regarding this and where to best set this variable in a "webpacker-way"?

The code causing our issues is the following in config/webpack/development.js:

const chokidar = require("chokidar")
environment.config.devServer.before = (app, server) => {
  chokidar
    .watch([
      "config/locales/**/*.yml",
      "app/views/**/*.html.erb",
      "app/assets/**/*.scss",
    ])
    .on("change", () => server.sockWrite(server.sockets, "content-changed"))
}

Error:

TypeError: Cannot set property 'before' of undefined
    at Object.<anonymous> (config/webpack/development.js:10:37)

Taken from #1879 (comment)

I don't know how development.js is influencing the test env in this case. The following fixes the issue.

$ WEBPACK_DEV_SERVER=true rspec

@aried3r
Copy link
Contributor

aried3r commented Aug 31, 2020

Okay, in classic fashion of rubber duck debugging, I found the solution shortly after writing it down.

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

is causing it and we'll write a guard clause similar to what's in this PR for our use case.

@justin808
Copy link
Contributor Author

@aried3r how does setting the before on the dev_server object work?

@justin808
Copy link
Contributor Author

@aried3r the NODE_ENV of test picks the test.js file by default. Resetting the NODE_ENV to 'development' in the test.js file seems very odd.

@aried3r
Copy link
Contributor

aried3r commented Sep 8, 2020

Hey @justin808, thank you for your comment. I believe #2721 and #2654 answer my question much better than I did in my follow-up comment.

how does setting the before on the dev_server object work?

I don't quite understand, could you elaborate?

@justin808
Copy link
Contributor Author

@aried3r, what code uses the function set in property environment.config.devServer.before?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants