-
We have a mature Rails 6 app that is transitioning from jQuery+CoffeeScript to React, and are currently upgrading it from Rails 6 to 7. We are following the instructions to upgrade from Webpacker to Shakapacker, and are quite close to replicating our prior setup. However, we are blocked by compilation errors with our CoffeeScript. All CS modules are failing to compile with this error:
This error suggests that the Babel loader is being run before the CoffeeScript loader, and treating the CS files like JS. In our prior Webpacker v5 configuration, we used the environment's "loaders" property to prepend the CoffeeScript loader to the beginning of all loaders so that CS would be translated into JS before any other transpilations occurred. We have tried adding a custom configuration as follows, with the "use" array specifying coffee-loader at the bottom (to run first) and babel-loader (to run second): const { webpackConfig, merge } = require('shakapacker')
const config = merge({
module: {
rules: [
{
test: require.resolve('jquery'),
loader: 'expose-loader',
options: {
exposes: ['$', 'jQuery']
}
},
{
test: /\.coffee(\.erb)?$/,
use: [
"babel-loader",
"coffee-loader",
]
},
],
}, ... however this does not appear to work. When printing the generated webpackConfig, we see:
Interestingly, all "test" objects appear empty in the final config. Any suggestions appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Solved by discovering that it is not necessary to add coffee-loader as part of the custom configuration -- by doing so we were compiling the coffee script twice. Looking at how Shakapacker works... it's smart enough to recognize that when coffee-loader is included, it automatically includes coffee-loader in the right place within the configuration. |
Beta Was this translation helpful? Give feedback.
Solved by discovering that it is not necessary to add coffee-loader as part of the custom configuration -- by doing so we were compiling the coffee script twice.
Looking at how Shakapacker works... it's smart enough to recognize that when coffee-loader is included, it automatically includes coffee-loader in the right place within the configuration.