{ "id": "guide/workspace-config", "title": "Angular workspace configuration", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Angular workspace configurationlink

\n

A file named angular.json at the root level of an Angular workspace provides workspace-wide and project-specific configuration defaults for build and development tools provided by the Angular CLI.\nPath values given in the configuration are relative to the root workspace folder.

\n

Overall JSON structurelink

\n

At the top level of angular.json, a few properties configure the workspace, and a projects section contains the remaining per-project configuration options. CLI defaults set at the workspace level can be overridden by defaults set at the project level, and defaults set at the project level can be overridden on the command line.

\n

The following properties, at the top level of the file, configure the workspace.

\n\n

The initial app that you create with ng new app_name is listed under \"projects\":

\n\n\n\"projects\": {\n \"app_name\": {\n ...\n }\n ...\n}\n\n\n

When you create a library project with ng generate library, the library project is also added to the projects section.

\n
\n

Note that the projects section of the configuration file does not correspond exactly to the workspace file structure.

\n\n
\n

Strict modelink

\n

When you create new workspaces and projects, you have the option to use Angular's strict mode, which can help you write better, more maintainable code.\nFor more information, see Strict mode.

\n

Project configuration optionslink

\n

The following top-level configuration properties are available for each project, under projects:<project_name>.

\n\n\n \"my-app\": {\n \"root\": \"\",\n \"sourceRoot\": \"src\",\n \"projectType\": \"application\",\n \"prefix\": \"app\",\n \"schematics\": {},\n \"architect\": {}\n }\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
PROPERTYDESCRIPTION
rootThe root folder for this project's files, relative to the workspace folder. Empty for the initial app, which resides at the top level of the workspace.
sourceRootThe root folder for this project's source files.
projectTypeOne of \"application\" or \"library\". An application can run independently in a browser, while a library cannot.
prefixA string that Angular prepends to generated selectors. Can be customized to identify an app or feature area.
schematicsA set of schematics that customize the ng generate sub-command option defaults for this project. See Generation schematics below.
architectConfiguration defaults for Architect builder targets for this project.
\n\n

Generation schematicslink

\n

Angular generation schematics are instructions for modifying a project by adding files or modifying existing files.\nIndividual schematics for the default Angular CLI ng generate sub-commands are collected in the package @schematics/angular.\nSpecify the schematic name for a subcommand in the format schematic-package:schematic-name;\nfor example, the schematic for generating a component is @schematics/angular:component.

\n

The JSON schemas for the default schematics used by the CLI to generate projects and parts of projects are collected in the package @schematics/angular.\nThe schema describes the options available to the CLI for each of the ng generate sub-commands, as shown in the --help output.

\n

The fields given in the schema correspond to the allowed argument values and defaults for the CLI sub-command options.\nYou can update your workspace schema file to set a different default for a sub-command option.

\n\n

Project tool configuration optionslink

\n

Architect is the tool that the CLI uses to perform complex tasks, such as compilation and test running.\nArchitect is a shell that runs a specified builder to perform a given task, according to a target configuration.\nYou can define and configure new builders and targets to extend the CLI.\nSee Angular CLI Builders.

\n\n

Default Architect builders and targetslink

\n

Angular defines default builders for use with specific CLI commands, or with the general ng run command.\nThe JSON schemas that define the options and defaults for each of these default builders are collected in the @angular-devkit/build-angular package.\nThe schemas configure options for the following builders.

\n\n

Configuring builder targetslink

\n

The architect section of angular.json contains a set of Architect targets.\nMany of the targets correspond to the CLI commands that run them.\nSome additional predefined targets can be run using the ng run command, and you can define your own targets.

\n

Each target object specifies the builder for that target, which is the npm package for the tool that Architect runs.\nIn addition, each target has an options section that configures default options for the target, and a configurations section that names and specifies alternative configurations for the target.\nSee the example in Build target below.

\n\n\n \"architect\": {\n \"build\": { },\n \"serve\": { },\n \"e2e\" : { },\n \"test\": { },\n \"lint\": { },\n \"extract-i18n\": { },\n \"server\": { },\n \"app-shell\": { }\n }\n\n\n\n

In general, the options for which you can configure defaults correspond to the command options listed in the CLI reference page for each command.\nNote that all options in the configuration file must use camelCase, rather than dash-case.

\n\n

Build targetlink

\n

The architect/build section configures defaults for options of the ng build command. It has the following top-level properties.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
PROPERTYDESCRIPTION
builderThe npm package for the build tool used to create this target. The default builder for an application (ng build myApp) is @angular-devkit/build-angular:browser, which uses the webpack package bundler. Note that a different builder is used for building a library (ng build myLib).
optionsThis section contains default build target options, used when no named alternative configuration is specified. See Default build targets below.
configurationsThis section defines and names alternative configurations for different intended destinations. It contains a section for each named configuration, which sets the default options for that intended environment. See Alternate build configurations below.
\n\n

Alternate build configurationslink

\n

Angular CLI comes with two build configurations: production and development. By default, the ng build command uses the production configuration, which applies a number of build optimizations, including:

\n\n

You can define and name additional alternate configurations (such as stage, for instance) appropriate to your development process. Some examples of different build configurations are stable, archive and next used by AIO itself, and the individual locale-specific configurations required for building localized versions of an app. For details, see Internationalization (i18n).

\n

You can select an alternate configuration by passing its name to the --configuration command line flag.

\n

You can also pass in more than one configuration name as a comma-separated list. For example, to apply both stage and fr build configurations, use the command ng build --configuration stage,fr. In this case, the command parses the named configurations from left to right. If multiple configurations change the same setting, the last-set value is the final one.

\n\n

Additional build and test optionslink

\n

The configurable options for a default or targeted build generally correspond to the options available for the ng build, ng serve, and ng test commands. For details of those options and their possible values, see the CLI Reference.

\n

Some additional options can only be set through the configuration file, either by direct editing or with the ng config command.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OPTIONS PROPERTIESDESCRIPTION
assetsAn object containing paths to static assets to add to the global context of the project. The default paths point to the project's icon file and its assets folder. See more in Assets configuration below.
stylesAn array of style files to add to the global context of the project. Angular CLI supports CSS imports and all major CSS preprocessors: sass/scss, less, and stylus. See more in Styles and scripts configuration below.
stylePreprocessorOptionsAn object containing option-value pairs to pass to style preprocessors. See more in Styles and scripts configuration below.
scriptsAn object containing JavaScript script files to add to the global context of the project. The scripts are loaded exactly as if you had added them in a <script> tag inside index.html. See more in Styles and scripts configuration below.
budgetsDefault size-budget type and threshholds for all or parts of your app. You can configure the builder to report a warning or an error when the output reaches or exceeds a threshold size. See Configure size budgets. (Not available in test section.)
fileReplacementsAn object containing files and their compile-time replacements. See more in Configure target-specific file replacements.
\n\n

Complex configuration valueslink

\n

The options assets, styles, and scripts can have either simple path string values, or object values with specific fields.\nThe sourceMap and optimization options can be set to a simple Boolean value with a command flag, but can also be given a complex value using the configuration file.\nThe following sections provide more details of how these complex values are used in each case.

\n\n

Assets configurationlink

\n

Each build target configuration can include an assets array that lists files or folders you want to copy as-is when building your project.\nBy default, the src/assets/ folder and src/favicon.ico are copied over.

\n\n\n\"assets\": [\n \"src/assets\",\n \"src/favicon.ico\"\n]\n\n\n

To exclude an asset, you can remove it from the assets configuration.

\n

You can further configure assets to be copied by specifying assets as objects, rather than as simple paths relative to the workspace root.\nA asset specification object can have the following fields.

\n\n

For example, the default asset paths can be represented in more detail using the following objects.

\n\n\n\"assets\": [\n {\n \"glob\": \"**/*\",\n \"input\": \"src/assets/\",\n \"output\": \"/assets/\"\n },\n {\n \"glob\": \"favicon.ico\",\n \"input\": \"src/\",\n \"output\": \"/\"\n }\n]\n\n\n

You can use this extended configuration to copy assets from outside your project.\nFor example, the following configuration copies assets from a node package:

\n\n\n\"assets\": [\n {\n \"glob\": \"**/*\",\n \"input\": \"./node_modules/some-package/images\",\n \"output\": \"/some-package/\"\n }\n]\n\n\n

The contents of node_modules/some-package/images/ will be available in dist/some-package/.

\n

The following example uses the ignore field to exclude certain files in the assets folder from being copied into the build:

\n\n\n\"assets\": [\n { \n \"glob\": \"**/*\",\n \"input\": \"src/assets/\",\n \"ignore\": [\"**/*.svg\"],\n \"output\": \"/assets/\" \n }\n]\n\n\n\n

Styles and scripts configurationlink

\n

An array entry for the styles and scripts options can be a simple path string, or an object that points to an extra entry-point file.\nThe associated builder will load that file and its dependencies as a separate bundle during the build.\nWith a configuration object, you have the option of naming the bundle for the entry point, using a bundleName field.

\n

The bundle is injected by default, but you can set inject to false to exclude the bundle from injection.\nFor example, the following object values create and name a bundle that contains styles and scripts, and excludes it from injection:

\n\n\n \"styles\": [\n {\n \"input\": \"src/external-module/styles.scss\",\n \"inject\": false,\n \"bundleName\": \"external-module\"\n }\n ],\n \"scripts\": [\n { \n \"input\": \"src/external-module/main.js\",\n \"inject\": false,\n \"bundleName\": \"external-module\"\n }\n ]\n\n\n

You can mix simple and complex file references for styles and scripts.

\n\n\n\"styles\": [\n \"src/styles.css\",\n \"src/more-styles.css\",\n { \"input\": \"src/lazy-style.scss\", \"inject\": false },\n { \"input\": \"src/pre-rename-style.scss\", \"bundleName\": \"renamed-style\" },\n]\n\n\n\n

Style preprocessor optionslink

\n

In Sass and Stylus you can make use of the includePaths functionality for both component and global styles, which allows you to add extra base paths that will be checked for imports.

\n

To add paths, use the stylePreprocessorOptions option:

\n\n\n\"stylePreprocessorOptions\": {\n \"includePaths\": [\n \"src/style-paths\"\n ]\n}\n\n\n

Files in that folder, such as src/style-paths/_variables.scss, can be imported from anywhere in your project without the need for a relative path:

\n\n// src/app/app.component.scss\n// A relative path works\n@import '../style-paths/variables';\n// But now this works as well\n@import 'variables';\n\n

Note that you will also need to add any styles or scripts to the test builder if you need them for unit tests.\nSee also Using runtime-global libraries inside your app.

\n

Optimization configurationlink

\n

The optimization browser builder option can be either a Boolean or an Object for more fine-tune configuration. This option enables various optimizations of the build output, including:

\n\n

There are several options that can be used to fine-tune the optimization of an application.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OptionDescriptionValue TypeDefault Value
scriptsEnables optimization of the scripts output.booleantrue
stylesEnables optimization of the styles output.boolean|Styles optimization optionstrue
fontsEnables optimization for fonts.
Note: This requires internet access.
boolean|Fonts optimization optionstrue
\n

Styles optimization optionslink

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OptionDescriptionValue TypeDefault Value
minifyMinify CSS definitions by removing extraneous whitespace and comments, merging identifiers and minimizing values.booleantrue
inlineCriticalExtract and inline critical CSS definitions to improve First Contentful Paint.booleantrue
\n

Fonts optimization optionslink

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OptionDescriptionValue TypeDefault Value
inlineReduce render blocking requests by inlining external Google fonts and icons CSS definitions in the application's HTML index file.
Note:This requires internet access.
booleantrue
\n

You can supply a value such as the following to apply optimization to one or the other:

\n\n\n \"optimization\": { \n \"scripts\": true,\n \"styles\": {\n \"minify\": true,\n \"inlineCritical\": true\n },\n \"fonts\": true\n }\n\n\n
\n

For Universal, you can reduce the code rendered in the HTML page by\nsetting styles optimization to true.

\n
\n

Source map configurationlink

\n

The sourceMap browser builder option can be either a Boolean or an Object for more fine-tune configuration to control the source maps of an application.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OptionDescriptionValue TypeDefault Value
scriptsOutput source maps for all scripts.booleantrue
stylesOutput source maps for all styles.booleantrue
vendorResolve vendor packages source maps.booleanfalse
hiddenOutput source maps used for error reporting tools.booleanfalse
\n

The example below shows how to toggle one or more values to configure the source map outputs:

\n\n\n \"sourceMap\": {\n \"scripts\": true,\n \"styles\": false,\n \"hidden\": true,\n \"vendor\": true\n }\n\n\n
\n

When using hidden source maps, source maps will not be referenced in the bundle.\nThese are useful if you only want source maps to map error stack traces in error reporting tools,\nbut don't want to expose your source maps in the browser developer tools.

\n
\n\n \n
\n\n\n" }