coffeescript and webpacker
enable the use of coffeescript files in a rails 6 application
It is not that hard to do. And, with the rails-6 philosophy in mind, i wanted to avoid creating a javascripts-directory in the assets-path... Following the readme at the github rails/webpacker site, just a few commands:
bundle exec rails webpacker:install:coffee
This will alter my config/webpack/environment.js:
const { environment } = require('@rails/webpacker')
const coffee = require('./loaders/coffee')
const webpack = require('webpack')
environment.plugins.prepend('Provide',
new webpack.ProvidePlugin({
$: 'jquery/src/jquery',
jQuery: 'jquery/src/jquery'
})
)
environment.loaders.prepend('coffee', coffee)
module.exports = environment
It will create a folder 'loader' in config/webpack, which now contains a file called 'coffee.js'
module.exports = {
test: /\.coffee(\.erb)?$/,
use: [{
loader: 'coffee-loader'
}]
}
In your 'javascripts/packs' directory you will find a file 'hello_coffee.coffee', which will output a message in the console when you require it in 'javascripts/packs/application.js' (the script will not do this for you...)
require("packs/hello_coffee")
config/webpacker.yml will have an extension added:
- .coffee
restart your server, look for the message in the console - if it is there, you're good to go.
happy days ;-)