Skip to content

Commit

Permalink
fix(@angular/build): apply define option to JavaScript from scripts o…
Browse files Browse the repository at this point in the history
…ption

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)
  • Loading branch information
clydin authored and alan-agius4 committed Dec 3, 2024
1 parent 5880a02 commit 06690d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,22 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
harness.expectFile('dist/browser/main.js').content.not.toContain('A_BOOLEAN');
harness.expectFile('dist/browser/main.js').content.toContain('(true)');
});

it('should replace a value in script code', async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
define: {
'A_BOOLEAN': 'true',
},
scripts: ['./src/script.js'],
});

await harness.writeFile('src/script.js', 'console.log(A_BOOLEAN);');

const { result } = await harness.executeOnce();
expect(result?.success).toBe(true);
harness.expectFile('dist/browser/scripts.js').content.not.toContain('A_BOOLEAN');
harness.expectFile('dist/browser/scripts.js').content.toContain('(true)');
});
});
});
2 changes: 2 additions & 0 deletions packages/angular/build/src/tools/esbuild/global-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function createGlobalScriptsBundleOptions(
sourcemapOptions,
jsonLogs,
workspaceRoot,
define,
} = options;

const namespace = 'angular:script/global';
Expand Down Expand Up @@ -73,6 +74,7 @@ export function createGlobalScriptsBundleOptions(
platform: 'neutral',
target,
preserveSymlinks,
define,
plugins: [
createSourcemapIgnorelistPlugin(),
createVirtualModulePlugin({
Expand Down

0 comments on commit 06690d8

Please sign in to comment.