Grails and modern Javascript development, crossing the gap

View a sample project with the ideas from this post here.

We want to use shiny new JS libraries that tend to only be available on NPM, and we want unit tests to be blazing fast.

Why it’s not easy

The key

In the absence of Grails plugins for Webpack or Browserify I’ve been using a bridge script that Webpacks files from node_modules to a vendor directory within grails-app/assets/javascripts. This gets the files into the asset-pipeline world in a web-friendly format and lets asset-pipeline take over for bundling and minification.

The sample repo has a full example of pulling redux-devtools from NPM, webpacking it to the asset-pipeline world, importing it into an ES2015 module, and testing that module with a Mocha spec.

Module formats

Babel can transpile ES2015 modules to AMD, UMD, and SystemJS module syntaxes, you’re probably covered no matter what you use. You can even use two module syntaxes simultaneously if you have legacy modules in one syntax but create new modules in another.

Incremental upgrades

For existing projects that you’d like to start using ES2015 modules in you can use file extensions to make the change incrementally. Make sure the babel config “processJsFiles” is set to false and use the extension “.es6” on files you’d like transpiled by Babel. This allows interop with older files in other module formats.

There’s more info about all of the implementation details in the sample repo’s readme. If you have questions feel free to reach out via email or twitter.

--

--

Senior Software Development Engineer @ Amazon. Trumpet player, drum corps enthusiast.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevan Ahlquist

Senior Software Development Engineer @ Amazon. Trumpet player, drum corps enthusiast.