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

Rails doesn't notice webpack-dev-server running when RAILS_ENV=test #1633

Closed
ryanscottaudio opened this issue Aug 1, 2018 · 11 comments

Comments

@ryanscottaudio
Copy link

commented Aug 1, 2018

Every time I try to load a JS file through Rails when RAILS_ENV=test, I get [Webpacker] [INFO] Compiling… in the console, even if in another console window i have RAILS_ENV=test ./bin/webpack-dev-server running. Here's my webpacker.yml:

# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker

  # Additional paths webpack should lookup modules. If adding to this, also add to test.resolved_paths in this file
  resolved_paths:
    - 'app/assets/js'
    - 'vendor/assets/javascripts'
    - 'node_modules'

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  extensions:
    - .js.coffee.erb
    - .js.coffee
    - .jst.eco
    - .js
    - .es6.jsx

development:
  <<: *default
  compile: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: true
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: /node_modules/

test:
  <<: *default
  compile: true

  # Compile test packs from/to a separate directory
  source_path: spec/javascripts
  source_entry_path: packs
  public_output_path: packs-test

  resolved_paths:
    - 'app/assets/js'
    - 'vendor/assets/javascripts'
    - 'node_modules'
    - 'spec/javascripts/helpers'

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: true
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: /node_modules/

staging:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Cache manifest.json for performance
  cache_manifest: true

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Cache manifest.json for performance
  cache_manifest: true

The purpose of running webpack-dev-server in test mode is just so that I can edit code to fix tests more efficiently (without webpack having to bundle all the affected files any time i want to load them); I wouldn't be running this in any sort of CI setting.

@sevenseacat

This comment has been minimized.

Copy link

commented Oct 18, 2018

Do you have the same problem in dev? I'm seeing this in dev - Rails just not seeing the dev server running in another window...

@ryanscottaudio

This comment has been minimized.

Copy link
Author

commented Oct 18, 2018

no, it works fine in dev, just not in test

@webzorg

This comment has been minimized.

Copy link

commented Mar 23, 2019

It doesn't work for me in dev. Rails 6 beta3. Specifically, there's only manifest.json file in public/packs, nothing else. So the js is not compiled at all.

@ghost

This comment has been minimized.

Copy link

commented Jul 3, 2019

@ryanscottaudio Did you ever get this working? I would also love for my tests not to recompile every time I run them.

@jakeNiemiec

This comment has been minimized.

Copy link
Member

commented Jul 3, 2019

webpack-dev-server will never work in test because the libraries under devDependancies will only be available in dev (hence the name). This is intentional yarn behavior.

@jakeNiemiec jakeNiemiec closed this Jul 3, 2019
@ryanscottaudio

This comment has been minimized.

Copy link
Author

commented Jul 3, 2019

is there a way to work around/change something with that? it makes no sense from a development POV IMO

@ghost

This comment has been minimized.

Copy link

commented Jul 3, 2019

It seems Webpacker is running with NODE_ENV=development in tests, even though RAILS_ENV=test. That means that it should still be possible, right?

I tried inserting the following code in one of my React components:

console.error("NODE ENV", process.env.NODE_ENV)

Which gave me the following in my spec:

SEVERE 2019-07-03 16:07:19 +0000: http://127.0.0.1:42759/packs-test/js/application-a732bbda87d3d4e33dfa.js 83045:12 \"NODE ENV\" \"development\"

Can this issue be reopened?

@jakeNiemiec

This comment has been minimized.

Copy link
Member

commented Jul 3, 2019

it makes no sense from a development POV IMO

@ryanscottaudio, take a look at the comments here #1212 (comment) for the reasoning behind it. The Rails folks have no control over how node.js functions.

That means that it should still be possible, right?

Yes, but there are multiple disparate issues at play here. In order to help debug, can @ryanscottaudio & @kjcommodi please post as much as you can of:

  • ./package.json
  • ./babel.config.js
  • ./config/webpack/environment.js
  • ./config/webpacker.yml
  • ./app/javascript/packs/application.js
  • what are you using to test your JS?
  • the full error message (even the parts you wouldn’t think is relevant)

—to a single https://gist.github.com. Please tag me via @jakeNiemiec in a comment and I’ll try to troubleshoot any problems directly in the gist.

@ryanscottaudio

This comment has been minimized.

Copy link
Author

commented Jul 3, 2019

i don't have access to the codebase i was using anymore, sorry

@ghost

This comment has been minimized.

@jakeNiemiec

This comment has been minimized.

Copy link
Member

commented Jul 3, 2019

@kjcommodi, I'll follow up with you in the gist & #2163

@jakeNiemiec jakeNiemiec closed this Jul 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.