Video lecture
Watch on YouTube(opens in a new tab)Sub topic 5
Touring the Generated Project
This chapter assumes you have run ng new my-app and can read JSON and basic TypeScript. It explains what each generated file does, so you know which ones to edit and which ones to leave alone. File names follow recent CLI versions. Older versions use the .component suffix, for example app.component.ts instead of app.ts.
The Workspace Root
An Angular workspace is the folder that ng new creates. It holds configuration for one or more projects. By default the workspace contains a single application project, so the two terms often refer to the same folder.
| File or folder | Purpose |
|---|---|
| `angular.json` | Workspace configuration: build, serve and test settings for each project |
| `package.json` | npm dependencies and scripts such as `npm start` and `npm test` |
| `tsconfig.json` | Base TypeScript compiler options, including strict mode |
| `tsconfig.app.json` | Extends the base config for application code |
| `tsconfig.spec.json` | Extends the base config for test files |
| `public/` | Static assets copied unchanged into the build, such as `favicon.ico` |
| `node_modules/` | Installed packages. Never edit or commit this folder |
| `src/` | Your application source code |
angular.json connects the CLI to your files. Each project in it has an architect section listing targets such as build, serve and test, and each target names a builder and its options. When you run ng build, the CLI reads the build target to find the entry file, styles and output path.
Inside src
| File | Purpose |
|---|---|
| `index.html` | The single HTML page. It contains the `<app-root>` element |
| `main.ts` | The entry point that starts the application |
| `styles.css` | Global styles applied to the whole app |
| `app/app.ts` | The root component class |
| `app/app.html` | The root component's template |
| `app/app.css` | Styles scoped to the root component |
| `app/app.spec.ts` | Unit tests for the root component |
| `app/app.config.ts` | Application-wide providers |
| `app/app.routes.ts` | Route definitions, initially empty |
A later chapter covers how main.ts and app.config.ts work together. For now, note that index.html contains no script tags. The build adds them for you.
Inspecting the Project from the Terminal
You can list the source files and ask the CLI how the build is configured. Run these commands from the workspace root:
find src public -type f | sort
ng config projects.my-app.architect.build.options.browser
ng config projects.my-app.architect.build.options.stylesThe second command prints src/main.ts, the entry file the build starts from. The third prints the list of global stylesheets.
Common Mistakes
- Putting assets in the wrong folder. Files in
public/are served from the site root, sopublic/logo.pngis available at/logo.png. Files placed elsewhere insrc/are not copied unlessangular.jsonlists them. - Editing global styles to fix one component. Component CSS is scoped to that component. Put a rule in
styles.cssonly when it really applies to the whole app. - Renaming the project folder only. The project name also appears in
angular.jsonandpackage.json. Commands likeng config projects.my-app...fail if those names no longer match. - Committing
node_modules. The generated.gitignorealready excludes it. Runnpm installto recreate it.
Exercise
You add a file at public/images/banner.jpg and a global font rule. Which URL serves the image, and which generated file should hold the font rule?
Show answer
The image is served at /images/banner.jpg because public/ maps to the site root. The font rule belongs in src/styles.css, which angular.json lists as a global stylesheet.
Exercise
Which file would you check to find out which builder runs when you type ng test?
Show answer
angular.json. The project's architect section has a test target that names the builder and its options.