Skip to content

Commit 06690d8

Browse files
clydinalan-agius4
authored andcommittedDec 3, 2024·
fix(@angular/build): apply define option to JavaScript from scripts option
The `define` option will now apply to JavaScript that is included in the output via the `scripts` option. This allows the replacement of identifiers within any included scripts in addition to the already supported replacement within application code. (cherry picked from commit 5bd937b)
1 parent 5880a02 commit 06690d8

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
 

‎packages/angular/build/src/builders/application/tests/options/define_spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,22 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
6161
harness.expectFile('dist/browser/main.js').content.not.toContain('A_BOOLEAN');
6262
harness.expectFile('dist/browser/main.js').content.toContain('(true)');
6363
});
64+
65+
it('should replace a value in script code', async () => {
66+
harness.useTarget('build', {
67+
...BASE_OPTIONS,
68+
define: {
69+
'A_BOOLEAN': 'true',
70+
},
71+
scripts: ['./src/script.js'],
72+
});
73+
74+
await harness.writeFile('src/script.js', 'console.log(A_BOOLEAN);');
75+
76+
const { result } = await harness.executeOnce();
77+
expect(result?.success).toBe(true);
78+
harness.expectFile('dist/browser/scripts.js').content.not.toContain('A_BOOLEAN');
79+
harness.expectFile('dist/browser/scripts.js').content.toContain('(true)');
80+
});
6481
});
6582
});

‎packages/angular/build/src/tools/esbuild/global-scripts.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function createGlobalScriptsBundleOptions(
3636
sourcemapOptions,
3737
jsonLogs,
3838
workspaceRoot,
39+
define,
3940
} = options;
4041

4142
const namespace = 'angular:script/global';
@@ -73,6 +74,7 @@ export function createGlobalScriptsBundleOptions(
7374
platform: 'neutral',
7475
target,
7576
preserveSymlinks,
77+
define,
7678
plugins: [
7779
createSourcemapIgnorelistPlugin(),
7880
createVirtualModulePlugin({

0 commit comments

Comments
 (0)
Please sign in to comment.