JavaScript
- Approach
- JavaScript MUST only be used for progressive enhancement - do not rely on libraries such as React, Vue or Angular
- JavaScript MUST be served as a static file and MUST NOT be compiled at runtime
- Compiled JavaScript MUST NOT contain a source file map in the
.js
file itself - Compiled JavaScript SHOULD contain a link to a separate source file (
.map.js
)
- Version
- JavaScript COULD use ESNext
- If written in ESNext, JavaScript MUST be transpiled down to ES5 with Babel
- Babel SHOULD use the
@babel/preset-env
preset - JavaScript COULD be transpiled with Webpack
- The version of NodeJS MUST be 18 or above
- The version of NodeJS SHOULD be a LTS release
- The version of NodeJS MUST be managed with nvm and a
.nvmrc
file in the root of the project
- Style/linting
- JavaScript MUST be linted with ESLint
- ESLint MUST extend
eslint:recommended
- JavaScript MUST be styled with Prettier
- Styling JavaScript with Prettier SHOULD NOT use any custom options (Prettier's philosophy on options)
- Dependencies
- JavaScript dependencies MUST be managed with npm
- Frameworks, tools and libraries
- JavaScript components COULD be developed using Storybook - (the rationale behind using Storybook)
- JavaScript tests SHOULD be written with Jest
- Single page applications MUST not be developed
- Packages
- JavaScript packages SHOULD be made using npm
- JavaScript packages SHOULD be deployed to npm
- If using NPM, packages MUST be published under the
@nationalarchives
organisation - JavaScript packages COULD be hosted in AWS CodeArtifact
- JavaScript MUST be compiled down to ES5 before distribution
- Source files COULD be included in distributed packages alongside the compiled ES5 to allow for the consumer to compile
TypeScript
- Approach
- As TypeScript is a superset of JavaScript, the same JavaScript standards above MUST also apply to TypeScript
- TypeScript COULD be used as an enhancement to JavaScript
- Packages
- TypeScript MUST be compiled down to ES5 before distribution
ESLint
ESLint is a static code analyser for JavaScript.
npm install eslint
An example .eslintrc.js
file that addresses all the standards above:
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"extends": ["eslint:recommended"],
"overrides": [{
"env": {
"node": true
},
"files": [".eslintrc.{js,cjs}"],
"parserOptions": {
"sourceType": "script"
}
}],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {}
};
# Run eslint against all JS files in the src directory
eslint 'src/**/*.js'
TNA Frontend ESLint config
TNA Frontend comes with an ESLint config which you can use in your project.