Skip to content

Front End Resources

Adam J. Arling edited this page Feb 9, 2018 · 15 revisions

This document aims to provide some helpful notes on setup and development environments for the avalon-bundle front-end.

JavaScript

ES6

All JS should be written in ES6 format. Here's a list of webpacker's default ES6 support.

In the avalon-bundle application, ES6 JavaScript is automatically transpiled via Babel as part of webpacker's default configuration.

Where should JavaScript files live?

Application JavaScript files should be placed in the following directory, as configured through webpack as the entrance point location:

app/javascript

Individual pack files are located in app/javascript/packs. These are the files which are referenced in a Rails view or partial. So these pack files should be small, simple JavaScript files which import .js files from the parent directory. The parent directory should store files which contain actual application logic.

Package management

yarn

https://yarnpkg.com/en/

yarn is used for front-end package management, over npm. yarn is also the default package manager for webpacker.

If you'd like to install a new package, from the application root directory run:

yarn add my-awesome-new-package, or if it's a development specific dependency, run:

yarn add --dev my-awesome-new-package

Build Tools

Webpack

https://webpack.js.org/ Webpack is made available to the Rails 5.1 environment through the webpacker package.

Webpacker

https://github.com/rails/webpacker

Custom configuration files/folders for our webpacker integration are located in: config/webpack.

If there are errors/warnings in the webpack build, they'll appear in your rails server terminal window.

Browser Debugging

For browser debugging, setting breakpoints, etc, webpacker's default configuration displays original source .es6 files in the browser's dev tools panel. In Chrome: DevTools > Sources > Network, the location is:

top / webpack:// / app/javascript / src

Code Quality Tools

avalon-bundle's configuration uses both ESLint and Prettier to ensure code quality and consistent formatting before committing code. Prettier formats JavaScript code in app/javascript automatically as webpack compiles, then passes the 'prettified' code to ESLint for further linting.

Our webpacker configuration automatically formats and checks front-end code every time webpacker compiles, however if you'd like to run Prettier or ESLint CLI in your terminal window separately, you can do so through the following yarn commands we've created. (Note: the command package names are purposely different from the community package names; ie. lint instead of eslint and pretty instead of prettier):

yarn run lint

yarn run pretty

yarn scripts can be added/edited in /package.json

ESLint

https://eslint.org/

Prettier

https://prettier.io

By far the biggest reason for adopting Prettier is to stop all the on-going debates over styles. It is generally accepted that having a common style guide is valuable for a project and team but getting there is a very painful and unrewarding process. People get very emotional around particular ways of writing code and nobody likes spending time writing and receiving nits.

So why choose the "Prettier style guide" over any other random style guide? Because Prettier is the only "style guide" that is fully automatic. Even if Prettier does not format all code 100% the way you'd like, it's worth the "sacrifice" given the unique benefits of Prettier, don't you think?

CSS

Clone this wiki locally