Search code examples
ruby-on-railscoffeescriptwebpacker

How do I import a js file in Coffeescript?


I'm currently trying to Code Split & Lazy Load the javascript in my huge app and got to a point where I have to lazy import a .js file from a .coffee file.

In a normal .js file I do something like this:

import(/* webpackChunkName: "vnd-forms" */ 'js/vendor/forms').then(() => {
  do_something
});

I knew writing the same thing in .coffee will fail, but here it is:

error: regular expressions cannot begin with *
import(/* webpackChunkName: "vnd-forms" */ 'js/vendor/forms').then(() ->

How can I lazy import that file in .coffee files?

I'm using Webpacker 4.3, Rails 4.2. We are migrating from coffeescript to es6 in baby steps since the codebase is humongous.


Solution

  • It turns out coffeescript has these backticks :)

    `import(/* webpackChunkName: "vnd-forms" */ 'js/vendor/forms')`.then(() ->
      do_something
    )
    

    Now everything loads properly!