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
@guilleiguaran Is ProvidePlugin not working either? Have made an example repo and everything seems to work fine: https://github.com/gauravtiwari/webpacker-provide-plugin
expose loader issue might be related to this: https://github.com/webpack-contrib/expose-loader/issues/66
@gauravtiwari ProvidePlugin seems to be working normally, I'll check the expose-loader issue.
@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?
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.
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
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)
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.
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
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
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?
@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.
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? 馃槥
Made a pre-release (with babel 7 support):
yarn add @rails/webpacker@next
bundle update webpacker
Also, has test.js included.
Most helpful comment
It's very common to have more then development and production environments. For example the
test.jswas very helpful when using Bundle Analyzer(or anything that starts a server and blocks the Webpacker precompile) indevelopment.js. Also, we put tools like jshint indevelopment.jswhich we don't need to be executed when runingrspectests.Is there any way to have
test.jsin the new version? 馃槥