Skip to content

Commit 37a2138

Browse files
committed
fix(@angular/build): account for HTML base HREF for dev-server externals
When adjusting URLs to support explicit external dependencies when using Vite, the workaround will now account for the presence of a base HREF value within the specifier. Vite will automatically add the base HREF as a prefix to the path when specified. This previously resulted in invalid specifiers due to the partial removal of the Vite specific `@id` path prefix.
1 parent 64b5081 commit 37a2138

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

packages/angular/build/src/builders/dev-server/tests/behavior/build-external-dependencies_spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,43 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
4545
expect(text).toContain(`import { BehaviorSubject } from "rxjs";`);
4646
expect(text).toContain(`import { map } from "rxjs/operators";`);
4747
});
48+
49+
it('respects import specifiers when using baseHref with trailing slash', async () => {
50+
setupTarget(harness, {
51+
externalDependencies: ['rxjs', 'rxjs/operators'],
52+
baseHref: '/test/',
53+
});
54+
55+
harness.useTarget('serve', {
56+
...BASE_OPTIONS,
57+
});
58+
59+
const { result, response } = await executeOnceAndFetch(harness, 'main.js');
60+
61+
expect(result?.success).toBeTrue();
62+
63+
const text = await response?.text();
64+
expect(text).toContain(`import { BehaviorSubject } from "rxjs";`);
65+
expect(text).toContain(`import { map } from "rxjs/operators";`);
66+
});
67+
68+
it('respects import specifiers when using baseHref without trailing slash', async () => {
69+
setupTarget(harness, {
70+
externalDependencies: ['rxjs', 'rxjs/operators'],
71+
baseHref: '/test',
72+
});
73+
74+
harness.useTarget('serve', {
75+
...BASE_OPTIONS,
76+
});
77+
78+
const { result, response } = await executeOnceAndFetch(harness, 'main.js');
79+
80+
expect(result?.success).toBeTrue();
81+
82+
const text = await response?.text();
83+
expect(text).toContain(`import { BehaviorSubject } from "rxjs";`);
84+
expect(text).toContain(`import { map } from "rxjs/operators";`);
85+
});
4886
});
4987
});

packages/angular/build/src/tools/vite/id-prefix-plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { Plugin } from 'vite';
1111
// NOTE: the implementation for this Vite plugin is roughly based on:
1212
// https://github.com/MilanKovacic/vite-plugin-externalize-dependencies
1313

14-
const VITE_ID_PREFIX = '/@id/';
14+
const VITE_ID_PREFIX = '@id/';
1515

1616
const escapeRegexSpecialChars = (inputString: string): string => {
1717
return inputString.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
@@ -29,7 +29,7 @@ export function createRemoveIdPrefixPlugin(externals: string[]): Plugin {
2929

3030
const escapedExternals = externals.map(escapeRegexSpecialChars);
3131
const prefixedExternalRegex = new RegExp(
32-
`${VITE_ID_PREFIX}(${escapedExternals.join('|')})`,
32+
`${resolvedConfig.base}${VITE_ID_PREFIX}(${escapedExternals.join('|')})`,
3333
'g',
3434
);
3535

0 commit comments

Comments
 (0)