Skip to content

externalDependencies not working as expected during serve with Vite #28028

@kfrederix

Description

@kfrederix

Command

serve

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When marking certain dependencies as external via option externalDependencies we get runtime errors during dev mode with the Vite server because Vite will prefix the import specifiers with /@id/ (during import-analysis).

For example:

import { BehaviorSubject, Subject, Subscription } from "/@id/rxjs";
import { map, first } from "/@id/rxjs/operators";

This breaks expected behavior, for example when we want to use an importmap to resolve external dependencies at runtime.

I realize that this is an issue with Vite itself, more then Angular: open issues exist on Vite since quite some time already:

However, I do believe that we can work around this issue by adding a small Vite plugin in vite-server.ts to remove this /@id/ prefix for explicit externalDependencies. I would be more than willing to submit a PR after discussion.

Minimal Reproduction

Here's a StackBlitz link with a simple repro case:
https://stackblitz.com/edit/stackblitz-starters-rbro7s?file=angular.json

  1. rxjs and rxjs/operators are declared as externalDependencies
  2. importmap is added in index.html to resolve these dependencies at runtime

Exception or Error

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Your Environment

Angular CLI: 18.0.7
Node: 18.20.3
Package Manager: npm 10.2.3
OS: linux x64

Angular: 18.0.6
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.7
@angular-devkit/build-angular   18.0.7
@angular-devkit/core            18.0.7
@angular-devkit/schematics      18.0.7
@angular/cli                    18.0.7
@schematics/angular             18.0.7
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.7

Anything else relevant?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions