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
Closed
Labels
dependencies dependency, yarn or other package management webpack

Comments

@ryanscottaudio
Copy link

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
Copy link

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
Copy link
Author

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

@webzorg
Copy link

webzorg 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
Copy link

ghost 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
Copy link
Member

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.

@ryanscottaudio
Copy link
Author

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

@ghost
Copy link

ghost 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
Copy link
Member

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.

@jakeNiemiec jakeNiemiec reopened this Jul 3, 2019
@jakeNiemiec jakeNiemiec added dependencies dependency, yarn or other package management webpack labels Jul 3, 2019
@ryanscottaudio
Copy link
Author

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

@ghost
Copy link

ghost commented Jul 3, 2019

https://gist.github.com/kjcommodi/2bbb1afdd421ce0b6c13c9099aef4501

@jakeNiemiec
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies dependency, yarn or other package management webpack
Projects
None yet
Development

No branches or pull requests

4 participants