If you migrate a Rails application from Sprockets to Webpack(er), you can either transpile your CoffeeScript files to JavaScript or integrate a CoffeeScript compiler to your new process. This checklist can be used to achieve the latter.
window
directly:-class @User
+class window.User
require
statement with Webpacker's import
statement to load dependencies.-#= require ./person
+import './person'
-class @user extends Person
+class window.user extends window.Person
package.json
:yarn add coffeescript coffee-loader
config/webpack/loaders/coffee.js
)module.exports = {
test: /\.coffee$/,
use: [{
loader: 'coffee-loader'
}]
}
config/webpack/environment.js
)environment.loaders.prepend('coffee', require('./loaders/coffee'))
packs/app.js
). Note that import
is required to expose your JS classes globally.// filePath is a relative path like "./foo.coffee".
// The call to "substring" removes the "./" prefix.
let preparePath = function(filePath) {
return filePath.substring(2);
};
modelFiles = require.context('js/models', true, /\.(js|coffee)$/);
for(let filePath of modelFiles.keys()) { import(`js/models/${preparePath(filePath)}`) };