Skip to content

Commit 142a36a

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/build-angular): prevent vite from resolving explicit external dependencies
When using the Vite-based development server combined with the `externalDependencies` option, Vite will no longer attempt to resolve the explicitly marked externals. This is still not ideal since it vite will still transform the import specifier to `/@id/${source}` but is currently closer to a raw external than a resolved file path. Further investigation with a possible feature request for Vite may be needed to achieve the desired outcome of an unresolved and untransformed external import specifier.
1 parent cdea518 commit 142a36a

File tree

1 file changed

+10
-0
lines changed
  • packages/angular_devkit/build_angular/src/builders/dev-server

1 file changed

+10
-0
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts

+10
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ export async function setupServer(
391391
watch: {
392392
ignored: ['**/*'],
393393
},
394+
// This is needed when `externalDependencies` is used to prevent Vite load errors.
395+
// NOTE: If Vite adds direct support for externals, this can be removed.
396+
preTransformRequests: externalMetadata.explicit.length === 0,
394397
},
395398
ssr: {
396399
// Exclude any provided dependencies (currently build defined externals)
@@ -403,6 +406,13 @@ export async function setupServer(
403406
// Ensures plugin hooks run before built-in Vite hooks
404407
enforce: 'pre',
405408
async resolveId(source, importer) {
409+
// Prevent vite from resolving an explicit external dependency (`externalDependencies` option)
410+
if (externalMetadata.explicit.includes(source)) {
411+
// This is still not ideal since Vite will still transform the import specifier to
412+
// `/@id/${source}` but is currently closer to a raw external than a resolved file path.
413+
return source;
414+
}
415+
406416
if (importer && source.startsWith('.')) {
407417
// Remove query if present
408418
const [importerFile] = importer.split('?', 1);

0 commit comments

Comments
 (0)