Skip to content

Create a new Angular project with a minimal configuration of ESLint & Prettier, set up your editor (PHPStorm, VSCode) and launch the project in StackBlitz.

License

Notifications You must be signed in to change notification settings

jprivet-dev/angular-eslint-prettier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

75 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Angular, Eslint, Prettier & Editors

Project release Angular release Open in StackBlitz

Summary

1. Presentation

Create a new Angular project with a minimal configuration of ESLint & Prettier, set up your editor (PHPStorm, VSCode) and launch the project in StackBlitz.

Versions used :

Label Version

node

v18.13.0

npm

8.19.3

Angular

15.1.5

@angular-eslint/schematics

15.2.1

Prettier

2.7.1

PHPStorm

2022.3.2

VS Code

1.75.1

2. Prerequisites

2.1. Install NVM or Node

To launch locally that project, you will need to install NVM (Node Version Manager) or Node.

πŸ“Ž

More information on:

In my case, I used Node in KDE Neon (see https://github.com/nodejs/snap).

2.2. Install a global Angular CLI (optional)

If you want to regenerate step by step a new Angular project, you will need to install/update a global Angular CLI:

$ npm install -g @angular/cli
πŸ“Ž
More information on https://angular.io/cli

3. Installation

  1. $ git clone git@github.com:jprivet-dev/angular-eslint-prettier.git

  2. $ cd angular-eslint-prettier/app

  3. $ npm install

  4. $ ng serve

  5. Open your browser on http://localhost:4200/

welcome

After installation, you can Set up your editor.

4. StackBlitz

4.1. Launching the project

Open in StackBlitz

4.2. Avoid error with zone.js

Angular CLI locally generates by default the main.ts file without zone.js:

main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

Which creates an error (NG0908) when I launch the project in StackBlitz:

Error: NG0908: In this configuration Angular requires Zone.js
stackblitz zone error

At this stage, locally everything works fine with $ ng serve.

So to solve the problem in StackBlitz, I have to import zone.js in main.ts:

main.ts
import 'zone.js/dist/zone'; // Avoid error in StackBlitz
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

After that, everything worked perfectly on StackBlitz:

stackblitz zone ok

But triggers a warning locally with $ ng serve:

Warning: .../angular-stackblitz/src/main.ts depends on 'zone.js/dist/zone'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
⚠️

I created an issue on StackBlitz: stackblitz/core#2366.

I am looking for a single solution without zone.js, to make everything works, without errors and alerts, in StackBlitz and locally with $ ng serve.

Still searching…​

5. Create your Angular project step by step

πŸ’‘

It is possible to create the content of the angular-eslint-prettier/app folder, from scratch.

To do this, just remove angular-eslint-prettier/app folder and generate a new Angular projet in angular-eslint-prettier : the /app folder will be regenerated by Angular CLI.

5.1. Case 1: Generate a new Angular app using directly ESlint schematics & use Prettier

5.1.1. Step #1: Remove the old Angular app

$ [[ -d app ]] && rm -rf app

5.1.2. Step #2: Generate an empty Angular workspace

πŸ“Ž
Prerequisites: Install a global Angular CLI (optional)
$ ng new app --create-application false --defaults
$ cd app

5.1.3. Step #3: Install ESLint

$ ng add @angular-eslint/schematics
πŸ’‘

If you have that error:

βœ” Packages successfully installed.
NOT SUPPORTED: keyword "id", use "$id" for schema ID

Execute the following command (the next tag is used by some projects to identify the upcoming version):

$ ng add @angular-eslint/schematics@next

More information on:

At the end of the process, you will get a confirmation message:

...
CREATE .eslintrc.json (984 bytes)
UPDATE package.json (1451 bytes)
UPDATE angular.json (3456 bytes)
βœ” Packages installed successfully.

5.1.4. Step #4: Generate a new Angular app in the workspace using ESLint

$ ng generate @angular-eslint/schematics:application app --project-root --routing --style scss --defaults --strict

5.1.5. Step #5: Install Prettier locally

$ npm install --save-dev --save-exact prettier
$ echo {} >.prettierrc.json
$ cp .gitignore .prettierignore
πŸ’‘
Base your .prettierignore on .gitignore and .eslintignore (if you have one).
πŸ“Ž
More information on https://prettier.io/docs/en/install.html.

5.1.6. Step #6: Format all files with Prettier

$ npx prettier --write .

5.1.7. Script available

Automatically execute all the above commands, in the angular-eslint-prettier folder, with the following script:

$ . scripts/generate-app.sh

5.2. Case 2: Create an original Angular app, convert TSLint to ESlint & use Prettier

5.2.1. Step #1: Remove the old Angular app

$ [[ -d app ]] && rm -rf app

5.2.2. Step #2: Create an Angular app

πŸ“Ž
Prerequisites: Install a global Angular CLI (optional)
$ ng new app --skip-git true --routing --style scss --defaults --strict
$ cd app

5.2.3. Step #3: Install ESLint

$ ng add @angular-eslint/schematics
πŸ’‘

If you have that error:

βœ” Packages successfully installed.
NOT SUPPORTED: keyword "id", use "$id" for schema ID

Execute the following command (the next tag is used by some projects to identify the upcoming version):

$ ng add @angular-eslint/schematics@next

More information on:

At the end of the process, you will get a confirmation message:

...
CREATE .eslintrc.json (984 bytes)
UPDATE package.json (1451 bytes)
UPDATE angular.json (3456 bytes)
βœ” Packages installed successfully.

5.2.4. Step #4: Convert TSLint to ESlint

# Avoid error: Path "/tslint.json" does not exist.
$ echo {} >tslint.json

$ ng g @angular-eslint/schematics:convert-tslint-to-eslint

At the end of the process, you will get a confirmation message:

...
DELETE tslint.json
UPDATE angular.json (3456 bytes)
UPDATE package.json (1451 bytes)
βœ” Packages installed successfully.

5.2.5. Step #5: Install Prettier locally

$ npm install --save-dev --save-exact prettier
$ echo {} >.prettierrc.json
$ cp .gitignore .prettierignore
πŸ’‘
Base your .prettierignore on .gitignore and .eslintignore (if you have one).
πŸ“Ž
More information on https://prettier.io/docs/en/install.html

5.2.6. Step #6: Format all files with Prettier

$ npx prettier --write .

5.2.7. Script available

Automatically execute all the above commands, in the angular-eslint-prettier folder, with the following script:

$ . scripts/convert-app.sh

6. Set up your editor

6.1. PHPStorm

6.1.1. Install PHPStorm

PhpStorm is a cross-platform IDE that provides consistent experience on the Windows, macOS, and Linux operating systems.

6.1.2. Configure a local Node.js interpreter

πŸ’‘
In my case I use Node.

Configure in Settings > Languages & Frameworks > Node.js. PHPStorm automatically finds Node and NPM:

phpstorm settings node interpreter

If you use NVM, you can have (for example):

  • Node interpreter: ~/.nvm/versions/node/v16.15.1/bin/node

  • Package manager: npm ~/.nvm/versions/node/v16.15.1/bin/npm

6.1.3. Configure ESLint

πŸ”₯
Before you start: Configure a local Node.js interpreter.

Configure in Settings > Languages & Frameworks > JavaScript > Code Quality Tools > ESLint :

phpstorm settings eslint

After the configuration, you can see the ESLint alerts in your code. For example:

phpstorm settings eslint error

6.1.4. Configure Prettier

πŸ”₯
Before you start: Configure a local Node.js interpreter.

Configure in Settings > Languages & Frameworks > JavaScript > Prettier :

phpstorm settings prettier

6.1.5. Reformat with Prettier: From the contextual menu

After the configuration, you can reformat your code :

  • With the shortcut Ctrl+Alt+Maj+P.

  • From the contextual menu (Right click > Reformat with Prettier).

phpstorm settings prettier contextual menu

6.1.6. Reformat with Prettier: On save

To reformat on save, Go in Settings > Languages & Frameworks > JavaScript > Prettier, and check On save option:

phpstorm settings prettier on save

If you click on All actions on save…​, you will see the list of all activated actions:

phpstorm settings tools actions on save
πŸ’‘
I also use the Optimize import option. This removes unused imports and organizes import statements in the current file. See https://www.jetbrains.com/help/phpstorm/creating-and-optimizing-imports.html#optimize-imports.

6.2. VS Code

6.2.1. Install VS Code

Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.

πŸ“Ž
More information on https://code.visualstudio.com/

6.2.2. Install ESLint

vscode eslint install

6.2.3. Configure ESLint

After the installation, you can immediately see the ESLint alerts in your code. For example:

vscode eslint alert

6.2.4. Install Prettier

Install the Prettier - Code formatter extension: https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode.

vscode prettier install

6.2.5. Reformat with Prettier: From the contextual menu

After the installation, you can reformat your code :

  • With the shortcut Ctrl+Alt+I.

  • From the contextual menu (Right click > Format Document).

vscode format document

If you have an alert Configure Default Formatter:

vscode alert configure default formatter

Click on the button Configure…​ and select Prettier - Code formatter:

vscode select default formatter

6.2.6. Reformat with Prettier: On save

To reformat on save, go on File > Preferences > Settings [Ctrl+,], and choose Text Editor > Formatting. Check Format On Paste and Format On Save:

vscode settings tab

From now on, whenever you paste code or save, the code will be reformatted.

Before:

vscode reformat before

After (on paste code or save):

vscode reformat after

7. Semantic Versioning

πŸ“Ž
Based on https://semver.org/
v[MAJOR].[ANGULAR_VERSION].[MINOR].[PATCH]

With [ANGULAR_VERSION] = [ANGULAR MAJOR + ANGULAR MINOR]

Example, with v1 of this repository with Angular 15.1.6:

v1.1501.0.0

8. Resources

9. License

This repository is released under the MIT License


About

Create a new Angular project with a minimal configuration of ESLint & Prettier, set up your editor (PHPStorm, VSCode) and launch the project in StackBlitz.

Topics

Resources

License

Stars

Watchers

Forks