There are a few good articles by Ashley Grant and Patrick Walters and Tiago Morais on structuring an Aurelia app but nothing I can find on the subject that is that recent. I have a reasonable sized app that’s over a year old that I’ve restructured a few times and am considering doing it again as I’m not happy with it.
root directory
Setup specific folders
|-- .github (Github templates)
|-- .vscode (VSCode only)
|-- aurelia_project (CLI only)
|-- custom_typings (Typescript only)
Ignored folders
|-- node_modules
|-- scripts
|-- assets (fonts, images, icons)
|-- doc (Changelog and other useful docs)
|-- locales (i18n translation files)
|-- src (the source code)
|-- test (just test setup)
My test folder is pretty empty as I prefer to have a __test__
folder in the same directory as each component.
Current source code structure
I’m not happy with the current structure as it doesn’t reflect the app. I can’t find out where I heard/read but it was something like your folder source should scream what your app does not how it’s built. The current structure does the latter.
|-- config (plugin config)
|-- controllers (will probably be dropped)
|-- features (encapsulated features)
|-- interfaces (typescript interfaces)
|-- models (models used by the views)
|-- private (the routes of the app)
| |-- page 1
| |-- page 2
| |-- page 2a
| |-- page 2b
| |-- ...
| |-- app-shell.css
| |-- app-shell.html
| |-- app-shell.ts
| |- ...
|-- resources
| |-- attributes
| |-- dialogs
| |-- elements
| |-- forms (reusable forms)
| |-- utilities
| |-- value-converters
| |-- events.ts (constants)
| |-- index.ts
| |-- key-codes.ts (more constants)
|-- services
| |-- api
| |-- service1.ts
| |-- service2.ts
| |-- ...
| |-- log.ts
|-- styles
| |-- base.css
| |-- spacing.css
| |-- ...
|-- environment.ts
|-- main.ts
|-- vars.css (css custom properties used through out the app)
It has a few problems
- Too many top level folders
- Folders with hardly anything in
- Doesn’t show the features of the app
Version 2
Considering this instead to help focus on the features.
|-- app-shell
| |-- app-shell.css
| |-- app-shell.html
| |-- app-shell.ts
|-- page 1
|-- page 2
| |-- page 2a
| |-- page 2b
| |-- ...
|-- ...
|-- resources
| |-- config (plugin config)
| |-- controllers (will probably be dropped)
| |-- features (encapsulated features)
| |-- interfaces (typescript interfaces)
| |-- models (models used by the views)
| |-- attributes
| |-- dialogs
| |-- elements
| |-- forms (resuable forms)
| |-- services
| |-- api
| |-- service1.ts
| |-- service2.ts
| |-- ...
| |-- log.ts
| |-- utilities
| |-- events.ts (constants)
| |-- key-codes.ts (more constants)
| |-- value-converters
| |-- index.ts
|-- styles
| |-- base.css
| |-- spacing.css
| |-- vars.css (css custom properties used through out the app)
| |-- ...
|-- environment.ts
|-- main.ts
This flattens the private
folder and puts the page/screens down into the src
folder and everything else into resources
(maybe lib
would be better?). The resources folder is a little busier and probably still in need of tidying up.
For now there is a very little in the page
folders other than the view and view-model pair but this might make it easier to restructure.
Bonus tip
You can set your paths in typescript so you don’t have to worry about relative paths. So your imports could be something like import { AccountService } from 'services/account-service'
rather than import { AccountService } from '../../../../resources/services/account-service'
What do you think? What does yours look like? Is there anything you’re not happy with?