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

webpack-dev-server on custom environment #1291

Closed
Jaroost opened this issue Feb 24, 2018 · 9 comments
Closed

webpack-dev-server on custom environment #1291

Jaroost opened this issue Feb 24, 2018 · 9 comments

Comments

@Jaroost
Copy link

Jaroost commented Feb 24, 2018

I try to use the webpack-dev-server script on my custom environment home.

I added a new configuration in webpacker.yml

home:
  <<: *default
  compile: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: true
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: false
    # 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/

Then I start the ruby .\bin\webpack-dev-server (I am on Windows10)

The script compiles on demand works and the server is up and running at https://localhost:3035

But when I make changes, the navigator doesn't refresh automatically and rails server compile instead of the on demande script.

Do i miss something?

PS: In development environment, all works just fine

@gauravtiwari
Copy link
Member

@Jaroost Could you set compile to false in home environment please? If I understood correctly you want to use dev server in home environment instead of on demand compiler right?

@Jaroost
Copy link
Author

Jaroost commented Feb 24, 2018

I want to use the webpack-dev-server script in my home environnent (it's a special development environment).

I tried to change complie to false but it doesn't work.

Am i clearer?

@akaspick
Copy link

akaspick commented Mar 8, 2018

@Jaroost Custom environments are somewhat broken in webpacker. See #1274

You'll need a config/webpack/home.js file for your environment (it used to default to dev or prod before) and I think you need to run the server like RAILS_ENV=home NODE_ENV=development bin/webpack-dev-server. Custom environments used to work, but somewhere along the line they've stopped working out of the box.

@gauravtiwari
Copy link
Member

Just released 3.4.0 and there is some documentation added in README.

@Jaroost
Copy link
Author

Jaroost commented Apr 2, 2018

@gauravtiwari
I think the custom environment doesn't work corectly:

I explain my current situation:

  • I have 2 development environments (development and home)

  • Development is my configuration at work: I use Oracle database, LDAP login, SMTP server, Some network connexions.

  • Home is my configuration at home: I use Mysql database, Database login, gmail SMTP, Some other network connexions.

So i need to use the webpack-dev-server in this 2 enviroments.

Actually this doesn't work in webpacker for 2 reasons:

the fetch method in lib/webpacker/dev_server.rb:

def fetch(key)
  ENV["WEBPACKER_DEV_SERVER_#{key.upcase}"] || config.dev_server.fetch(key, defaults[key])
end

config.dev_server.fetch(key, defaults[key]) will throw an exception because defaults is nil

defaults is nil in that case because in configuration.rb the defaults methods try to retreive defaults values from install/config/webpacker.yml using the current enviroment home and this methods will retrurn an empty hash

def defaults
      @defaults ||= \
        HashWithIndifferentAccess.new(YAML.load_file(File.expand_path("../../install/config/webpacker.yml", __FILE__))[env])
    end

but in that case, we can set in custom environment (home.rb) the following enviornment variables:

ENV['WEBPACKER_DEV_SERVER_HOST']='localhost'
ENV['WEBPACKER_DEV_SERVER_PORT']='3035'

The second problem is in lib/webpacker/railtie.rb:

When we retreive pack files with javascript_pack_tag or stylesheet_pack_tag rails need to know that we need files from the webpacker-dev-server instead of plain files so to do that we use the dev_server_proxy

initializer "webpacker.proxy" do |app|
    if Rails.env.development?
      app.middleware.insert_before 0,
        Rails::VERSION::MAJOR >= 5 ?
          Webpacker::DevServerProxy : "Webpacker::DevServerProxy", ssl_verify_none: true
    end
  end

In case of a custom environment, the proxy will never be used. So to patch this i just replace the Rails.env.development? to 'Webpacker.dev_server.running?'

like this, home and webpack-dev-server works but i think that this issue is not resolved.

@gauravtiwari
Copy link
Member

@Jaroost Do you have a home environment declared in webpacker.yml?

@Jaroost
Copy link
Author

Jaroost commented Apr 2, 2018

@gauravtiwari Yes I have

Maybe it's a Windows issue

@gauravtiwari
Copy link
Member

@Jaroost Thanks for confirming. Will take a look at this later today.

@akaspick
Copy link

akaspick commented Apr 2, 2018

@gauravtiwari Just a fyi, but this now works for my staging env.

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

No branches or pull requests

3 participants