Arasaac frontend: React (redux) app
What it will (should) be?
- Universal/Isomorphic ReactJS App. T
- The new release for Arasaac Web
Current status: In development. Using redux for application-state management.
Table of Contents
- Getting Started
- React (
- Includes react-addons-test-utils (
- Includes react-addons-test-utils (
- Redux (
- react-redux (
npm run dev:nwto display them in a separate window.
- redux-thunk middleware
- react-redux (
- react-router (
- react-router-redux (
- CSS modules!
- postcss-loader with cssnano for style autoprefixing and minification
- Bundle splitting for app and vendor dependencies
- CSS extraction during builts that are not using HMR (like
npm run compile)
- Loaders for fonts and images
- Koa (
- Babel (
- babel-plugin-transform-runtime so transforms aren't inlined
- babel-preset-react-hmre for:
- react-transform-hmr (HMR for React components)
- redbox-react (visible error reporting for React components)
- babel-plugin-transform-react-constant-elements save some memory allocation
- babel-plugin-transform-react-remove-prop-types remove
- Uses Standard Style by default, but you're welcome to change this!
- Includes separate test-specific
.eslintrcto support chai assertions
Before delving into the descriptions of each available npm script, here's a brief summary of the three which will most likely be your bread and butter:
- Doing live development? Use
npm startto spin up the dev server.
- Compiling the application to disk? Use
npm run compile.
- Deploying to an environment?
npm run deploycan help with that.
NOTE: This package makes use of debug to improve your debugging experience. For convenience, all of messages are prefixed with
app:*. If you'd like to to change what debug statements are displayed, you can override the
DEBUG environment variable via the CLI (e.g.
DEBUG=app:* npm start) or tweak the npm scripts (
Great, now that introductions have been made here's everything in full detail:
npm start- Spins up Koa server to serve your app at
localhost:3000. HMR will be enabled in development.
npm run compile- Compiles the application to disk (
npm run dev- Same as
npm start, but enables nodemon to automatically restart the server when server-related code is changed.
npm run dev:nw- Same as
npm run dev, but opens the redux devtools in a new window.
npm run dev:no-debug- Same as
npm run devbut disables redux devtools.
npm run test- Runs unit tests with Karma and generates a coverage report.
npm run test:dev- Runs Karma and watches for changes to re-run tests; does not generate coverage reports.
npm run deploy- Runs linter, tests, and then, on success, compiles your application to disk.
npm run lint- Lint all
npm run lint:fix- Lint and fix all
.jsfiles. Read more on this.
NOTE: Deploying to a specific environment? Make sure to specify your target
NODE_ENV so webpack will use the correct configuration. For example:
NODE_ENV=production npm run compile will compile your application with
Basic project configuration can be found in
~/config/_base.js. Here you'll be able to redefine your
dist directories, adjust compilation settings, tweak your vendor dependencies, and more. For the most part, you should be able to make changes in here without ever having to touch the webpack build configuration. If you need environment-specific overrides, create a file with the name of target
NODE_ENV prefixed by an
~/config/_production.js for an example).
Common configuration options:
dir_src- application source code base path
dir_dist- path to build compiled application to
server_host- hostname for the Koa server
server_port- port for the Koa server
compiler_css_modules- whether or not to enable CSS modules
compiler_devtool- what type of source-maps to generate (set to
compiler_vendor- packages to separate into to the vendor bundle
The folder structure provided is only meant to serve as a guide, it is by no means prescriptive. It is something that has worked very well for me and my team, but use only what makes sense to you.
. ├── bin # Build/Start scripts ├── build # All build-related configuration │ └── webpack # Environment-specific configuration files for webpack ├── config # Project configuration settings ├── server # Koa application (uses webpack middleware) │ └── main.js # Server application entry point ├── src # Application source code │ ├── components # Generic React Components (generally Dumb components) │ ├── containers # Components that provide context (e.g. Redux Provider) │ ├── layouts # Components that dictate major page structure │ ├── redux # Redux-specific pieces │ │ ├── modules # Collections of reducers/constants/actions │ │ └── utils # Redux-specific helpers │ ├── routes # Application route definitions │ ├── static # Static assets (not imported anywhere in source code) │ ├── styles # Application-wide styles (generally settings) │ ├── views # Components that live at a route │ └── main.js # Application bootstrap and rendering └── tests # Unit tests
Components vs. Views vs. Layouts
TL;DR: They're all components.
This distinction may not be important for you, but as an explanation: A Layout is something that describes an entire page structure, such as a fixed navigation, viewport, sidebar, and footer. Most applications will probably only have one layout, but keeping these components separate makes their intent clear. Views are components that live at routes, and are generally rendered within a Layout. What this ends up meaning is that, with this structure, nearly everything inside of Components ends up being a dumb component.
You can redefine which packages to bundle separately by modifying
~/config/_base.js. These default to:
[ 'history', 'react', 'react-redux', 'react-router', 'react-router-redux', 'redux' ]
Webpack Root Resolve
Webpack is configured to make use of resolve.root, which lets you import local packages as if you were traversing from the root of your
~/src directory. Here's an example:
// current file: ~/src/views/some/nested/View.js // What used to be this: import SomeComponent from '../../../components/SomeComponent' // Can now be this: import SomeComponent from 'components/SomeComponent' // Hooray!
These are global variables available to you anywhere in your source code. If you wish to modify them, they can be found as the
globals key in
~/config/_base.js. When adding new globals, also add them to
process.env.NODE_ENV- the active
NODE_ENVwhen the build started
__DEV__- True when
__PROD__- True when
__TEST__- True when
__DEBUG__- True when
developmentand cli arg
--no_debugis not set (
npm run dev:no-debug)
__BASENAME__- npm history basename option
This starter kit comes packaged with an Koa server. It's important to note that the sole purpose of this server is to provide
webpack-hot-middleware for hot module replacement. Using a custom Koa app in place of webpack-dev-server will hopefully make it easier for users to extend the starter kit to include functionality such as back-end API's, isomorphic/universal rendering, and more -- all without bloating the base boilerplate. Because of this, it should be noted that the provided server is not production-ready. If you're deploying to production, take a look at the deployment section.
.css file extensions are supported out of the box and are configured to use CSS Modules. After being imported, styles will be processed with PostCSS for minification and autoprefixing, and will be extracted to a
.css file during production builds.
NOTE: If you're importing styles from a base styles directory (useful for generic, app-wide styles), you can make use of the
styles alias, e.g.:
// current file: ~/src/components/some/nested/component/index.jsx import 'styles/core.scss' // this imports ~/src/styles/core.scss
styles directory is aliased for sass imports, which further eliminates manual directory traversing; this is especially useful for importing variables/mixins.
Here's an example:
// current file: ~/src/styles/some/nested/style.scss // what used to be this (where base is ~/src/styles/_base.scss): @import '../../base'; // can now be this: @import 'base';
To add a unit test, simply create a
.spec.js file anywhere in
~/tests. Karma will pick up on these files automatically, and Mocha and Chai will be available within your test without the need to import them.
Coverage reports will be compiled to
~/coverage by default. If you wish to change what reporters are used and where reports are compiled, you can do so by modifying
Out of the box, this starter kit is deployable by serving the
~/dist folder generated by
npm run compile (make sure to specify your target
NODE_ENV as well). This project does not concern itself with the details of server-side rendering or API structure, since that demands an opinionated structure that makes it difficult to extend the starter kit. However, if you do need help with more advanced deployment strategies, here are a few tips:
If you are serving the application via a web server such as nginx, make sure to direct incoming routes to the root
~/dist/index.html file and let react-router take care of the rest. The Koa server that comes with the starter kit is able to be extended to serve as an API or whatever else you need, but that's entirely up to you.
Have more questions? Feel free to submit an issue or join the Gitter chat!
npm run dev:nw produces
cannot read location of undefined.
This is most likely because the new window has been blocked by your popup blocker, so make sure it's disabled before trying again.
Reference: issue 110
Running into issues with Babel? Babel 6 can be tricky, please either report an issue or try out the stable v0.18.1 release with Babel 5. If you do report an issue, please try to include relevant debugging information such as your node, npm, and babel versions.
All messages in this website are localized and rendered using
There is also a babel plugin to extract all the default messages into
./_translations/lib to be provided to translators.
$ npm run build:i18n
You can also run a script to extract all those translations as key-value.
$ npm run build:i18n:langs
High editor CPU usage after compilation
While this is common to any sizable application, it's worth noting for those who may not know: if you happen to notice higher CPU usage in your editor after compiling the application, you may need to tell your editor not to process the dist folder. For example, in Sublime you can add:
"folder_exclude_patterns": [".svn", ".git", ".hg", "CVS", "node_modules", "dist"]