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

Settings in config/webpack/test.js stopped working after of upgrade from 3.2 to 3.5 #1502

Closed
guilleiguaran opened this issue May 16, 2018 · 14 comments

Comments

@guilleiguaran
Copy link
Member

guilleiguaran commented May 16, 2018

I have expose loader set in config/webpack/test.js in a project with Webpacker 3.2:

const environment = require('./environment')
const webpack = require('webpack')

// We must expose jQuery to the global object to be able to use `JQuery.active`
// in `wait_for_ajax` (spec/support/wait_for_ajax.rb)

environment.loaders.set('expose', {
  test: require.resolve('jquery'),
  use: {
    loader: 'expose-loader',
    options: 'jQuery'
  }
})

module.exports = environment.toWebpackConfig()

and I've modified the current configuration to match new conventions in 3.5 setting NODE_ENV and replacing environment.loaders.set with environment.loaders.append:

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

const environment = require('./environment')
const webpack = require('webpack')

// We must expose jQuery to the global object to be able to use `JQuery.active`
// in `wait_for_ajax` (spec/support/wait_for_ajax.rb)

environment.loaders.append('expose', {
  test: require.resolve('jquery'),
  use: {
    loader: 'expose-loader',
    options: 'jQuery'
  }
})

module.exports = environment.toWebpackConfig()

But after of upgrading, this loader stopped working.

Everything else in the project work as expected and compiling success without problems but I can't get this working, does someone has any pointers about this?

My config/webpack/environment.js is this:

const { environment } = require('@rails/webpacker')
const webpack = require('webpack')

environment.plugins.append(
  'Provide',
  new webpack.ProvidePlugin({
    $: "jquery",
    jQuery: "jquery",
    "window.jQuery": "jquery"
  })
)

// resolve-url-loader must be used before sass-loader
environment.loaders.get('sass').use.splice(-1, 0, {
  loader: 'resolve-url-loader',
});

module.exports = environment
@gauravtiwari
Copy link
Member

@guilleiguaran Is ProvidePlugin not working either? Have made an example repo and everything seems to work fine: https://github.com/gauravtiwari/webpacker-provide-plugin

@gauravtiwari
Copy link
Member

expose loader issue might be related to this: webpack-contrib/expose-loader#66

@guilleiguaran
Copy link
Member Author

guilleiguaran commented May 22, 2018

@gauravtiwari ProvidePlugin seems to be working normally, I'll check the expose-loader issue.

@guilleiguaran
Copy link
Member Author

guilleiguaran commented May 23, 2018

@gauravtiwari I've cloned your app and added a console.log to every file under config/webpack/ and looks like the test.js isn't loaded:

~/src/webpacker-provide-plugin[master] % RAILS_ENV=test bundle exec bin/webpack --debug --verbose
Hello from environment.js
Hello from development.js
Hash: f2664d78a19c6b3cef68
Version: webpack 3.12.0
Time: 904ms
PublicPath: /packs-test/

Passing NODE_ENV=test works though (yup, I know that NODE_ENV should be development or production):

~/src/webpacker-provide-plugin[master] % NODE_ENV=test RAILS_ENV=test bundle exec bin/webpack --debug --verbose
Hello from environment.js
Hello from test.js
Hash: 122b4486b6ceb759c441
Version: webpack 3.12.0
Time: 2018ms
PublicPath: /packs-test/

is this expected? should I set NODE_ENV=test for testing environment?

@seanabrahams
Copy link

seanabrahams commented May 24, 2018

I'm seeing the same behavior as @guilleiguaran where bin/webpack is loading config/webpack/<env>.js files based on NODE_ENV instead of RAILS_ENV.

If this is the intended behavior there should be a migration guide for those of us with custom environments (config/webpack/staging.js, config/webpack/demo.js) to which I'd be a happy contributor.

@guilleiguaran guilleiguaran changed the title Loader stopped working after of upgrade from Webpacker 3.2 to 3.5 Settings in config/webpack/test.js stopped working after of upgrade from 3.2 to 3.5 May 25, 2018
@gauravtiwari
Copy link
Member

gauravtiwari commented May 25, 2018

Thanks, @guilleiguaran Sorry I am a bit confused 😄 Is this expose loader problem related to test environment?

@seanabrahams

If this is the intended behavior there should be a migration guide for those of us with custom environments (config/webpack/staging.js, config/webpack/demo.js) to which I'd be a happy contributor.

Yes, those JS files are no longer necessary and you don't need to set a NODE_ENV anywhere. RAILS_ENV is now used to load settings from webpacker.yml and either development or production.js is used to compile the packs. Please see #1359

@guilleiguaran
Copy link
Member Author

guilleiguaran commented May 25, 2018

Thanks, @guilleiguaran Sorry I am a bit confused 😄 Is this expose loader problem related to test environment?

The problem is that after of upgrading the config/webpack/test.js isn't loaded anymore when RAILS_ENV=test but reading other threads looks like that's expected (but it wasn't mentioned in CHANGELOG)

@gauravtiwari
Copy link
Member

Oh yes, sorry so the new intended behaviour is we will use RAILS_ENV to load configuration from webpacker.yml and one doesn't need to set NODE_ENV at all, since it will either be development or production. The configuration settings like paths etc. will be applied based on RAILS_ENV

I have added some information here: https://github.com/rails/webpacker#custom-rails-environments but I guess it's not very clear. Happy to add it to Changelog as well.

@seanabrahams
Copy link

Thanks @gauravtiwari Just to add some context here's my config/webpack/test.js:

const webpack = require('webpack')
const environment = require('./environment')

environment.plugins.set(
  'Environment',
  new webpack.EnvironmentPlugin({
    SITE_URL: 'http://localhost:57712'
    // SOMETHING_ELSE: 'GOOD'
  })
)

module.exports = environment.toWebpackConfig()

Since I was using it for environment variables it looks like I'll need to remove config/webpack/test.jsand use dotenv per https://github.com/rails/webpacker/blob/master/docs/env.md

@gauravtiwari
Copy link
Member

Thanks @seanabrahams

Yes, please. We already have environment plugin added to webpack config: https://github.com/rails/webpacker/blob/master/package/environments/base.js#L27

so all you need to do is pass the env variable when running tests or binstubs.

SITE_URL=foo bundle exec rails test

@seanabrahams
Copy link

Thanks @gauravtiwari

One more follow up. I ran into the following issue on our continuous integration system after having removed config/webpack/test.js:

Building Webpack assets...
================================================================================
React on Rails FATAL ERROR!
Error in building webpack assets!
cmd: cd /home/ubuntu/countable && RAILS_ENV=test bin/webpack
exitstatus: 1
stderr:
webpack config /home/ubuntu/countable/config/webpack/test.js not found, please run 'bundle exec rails webpacker:install' to install Webpacker with default configs or add the missing config file for your custom environment.
================================================================================

I then ran bundle exec rails webpacker:install and noticed that a config/webpack/test.js file was in-fact generated. This strikes me as odd if NODE_ENV is only concerned with production and development. I'd expect a config/webpack/test.js file to not be generated. Or, is config/webpack/test.js a special case?

@gauravtiwari
Copy link
Member

@seanabrahams Oh yes, it's because the latest changes are not yet published so test.js file will still be generated (unless you are using master). Will make a release over the weekend.

@goranovs
Copy link

goranovs commented Jun 5, 2018

Oh yes, sorry so the new intended behaviour is we will use RAILS_ENV to load configuration from webpacker.yml and one doesn't need to set NODE_ENV at all, since it will either be development or production.

It's very common to have more then development and production environments. For example the test.js was very helpful when using Bundle Analyzer(or anything that starts a server and blocks the Webpacker precompile) in development.js. Also, we put tools like jshint in development.js which we don't need to be executed when runing rspec tests.

Is there any way to have test.js in the new version? 😞

@gauravtiwari
Copy link
Member

Made a pre-release (with babel 7 support):

yarn add @rails/webpacker@next
bundle update webpacker

Also, has test.js included.

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

4 participants