Skip to content

Commit 7f57123

Browse files
committed
feat(@angular-devkit/build-angular): add define build option to application builder
The `application` builder now supports a new option named `define`. This option allows global identifiers present in the code to be replaced with another value at build time. This is similar to the behavior of Webpack's `DefinePlugin` which was previously used with some custom Webpack configurations that used third-party builders. The option has similar capabilities to the `esbuild` option of the same name. The documentation for that option can be found here: https://esbuild.github.io/api/#define The command line capabilities of the Angular CLI option are not yet implemented and will added in a future change. The option within the `angular.json` configuration file is of the form of an object. The keys of the object represent the global identifier to replace and the values of the object represent the corresponding replacement value for the identifier. An example is as follows: ``` "define": { "SOME_CONSTANT": "5", "ANOTHER": "'this is a string literal'" } ``` All replacement values are defined as strings within the configuration file. If the replacement is intended to be an actual string literal, it should be enclosed in single quote marks. This allows the flexibility of using any valid JSON type as well as a different identifier as a replacement. Additionally, TypeScript needs to be aware of the module type for the import to prevent type-checking errors during the build. This can be accomplished with an additional type definition file within the application source code (`src/types.d.ts`, for example) with the following or similar content: ``` declare const SOME_CONSTANT: number; declare const ANOTHER: string; ``` The default project configuration is already setup to use any type definition files present in the project source directories. If the TypeScript configuration for the project has been altered, the tsconfig may need to be adjusted to reference this newly added type definition file. An important caveat to the option is that it does not function when used with values contained within Angular metadata such as a Component or Directive decorator. This limitation was present with previous third-party builder usage as well.
1 parent ad52f8e commit 7f57123

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

goldens/public-api/angular_devkit/build_angular/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export interface ApplicationBuilderOptions {
3232
budgets?: Budget_2[];
3333
clearScreen?: boolean;
3434
crossOrigin?: CrossOrigin_2;
35+
define?: {
36+
[key: string]: string;
37+
};
3538
deleteOutputPath?: boolean;
3639
externalDependencies?: string[];
3740
extractLicenses?: boolean;

packages/angular_devkit/build_angular/src/builders/application/options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export async function normalizeOptions(
295295
budgets,
296296
deployUrl,
297297
clearScreen,
298+
define,
298299
} = options;
299300

300301
// Return all the normalized options
@@ -350,6 +351,7 @@ export async function normalizeOptions(
350351
jsonLogs: useJSONBuildLogs,
351352
colors: colors.enabled,
352353
clearScreen,
354+
define,
353355
};
354356
}
355357

packages/angular_devkit/build_angular/src/builders/application/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@
216216
"^\\.\\S+$": { "enum": ["text", "binary", "file", "empty"] }
217217
}
218218
},
219+
"define": {
220+
"description": "Defines global identifiers that will be replaced with a specified constant value when found in any JavaScript or TypeScript code including libraries. The value will be used directly. String values must be put in quotes. Identifiers within Angular metadata such as Component Decorators will not be replaced.",
221+
"type": "object",
222+
"additionalProperties": {
223+
"type": "string"
224+
}
225+
},
219226
"fileReplacements": {
220227
"description": "Replace compilation source files with other compilation source files in the build.",
221228
"type": "array",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { buildApplication } from '../../index';
10+
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
11+
12+
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
13+
describe('Option: "define"', () => {
14+
it('should replace a value in application code when specified as a number', async () => {
15+
harness.useTarget('build', {
16+
...BASE_OPTIONS,
17+
define: {
18+
'AN_INTEGER': '42',
19+
},
20+
});
21+
22+
await harness.writeFile('./src/types.d.ts', 'declare const AN_INTEGER: number;');
23+
await harness.writeFile('src/main.ts', 'console.log(AN_INTEGER);');
24+
25+
const { result } = await harness.executeOnce();
26+
expect(result?.success).toBe(true);
27+
harness.expectFile('dist/browser/main.js').content.not.toContain('AN_INTEGER');
28+
harness.expectFile('dist/browser/main.js').content.toContain('(42)');
29+
});
30+
31+
it('should replace a value in application code when specified as a string', async () => {
32+
harness.useTarget('build', {
33+
...BASE_OPTIONS,
34+
define: {
35+
'A_STRING': '"42"',
36+
},
37+
});
38+
39+
await harness.writeFile('./src/types.d.ts', 'declare const A_STRING: string;');
40+
await harness.writeFile('src/main.ts', 'console.log(A_STRING);');
41+
42+
const { result } = await harness.executeOnce();
43+
expect(result?.success).toBe(true);
44+
harness.expectFile('dist/browser/main.js').content.not.toContain('A_STRING');
45+
harness.expectFile('dist/browser/main.js').content.toContain('("42")');
46+
});
47+
48+
it('should replace a value in application code when specified as a boolean', async () => {
49+
harness.useTarget('build', {
50+
...BASE_OPTIONS,
51+
define: {
52+
'A_BOOLEAN': 'true',
53+
},
54+
});
55+
56+
await harness.writeFile('./src/types.d.ts', 'declare const A_BOOLEAN: boolean;');
57+
await harness.writeFile('src/main.ts', 'console.log(A_BOOLEAN);');
58+
59+
const { result } = await harness.executeOnce();
60+
expect(result?.success).toBe(true);
61+
harness.expectFile('dist/browser/main.js').content.not.toContain('A_BOOLEAN');
62+
harness.expectFile('dist/browser/main.js').content.toContain('(true)');
63+
});
64+
});
65+
});

packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
379379
write: false,
380380
preserveSymlinks,
381381
define: {
382+
...options.define,
382383
// Only set to false when script optimizations are enabled. It should not be set to true because
383384
// Angular turns `ngDevMode` into an object for development debugging purposes when not defined
384385
// which a constant true value would break.

0 commit comments

Comments
 (0)