@@ -13,7 +13,7 @@ import assert from 'node:assert';
1313import { readFile } from 'node:fs/promises' ;
1414import { builtinModules , isBuiltin } from 'node:module' ;
1515import { join } from 'node:path' ;
16- import type { Connect , DepOptimizationConfig , InlineConfig , ViteDevServer } from 'vite' ;
16+ import type { Connect , InlineConfig , ViteDevServer } from 'vite' ;
1717import type { ComponentStyleRecord } from '../../tools/vite/middlewares' ;
1818import {
1919 ServerSsrMode ,
@@ -23,6 +23,7 @@ import {
2323 createAngularSsrTransformPlugin ,
2424 createRemoveIdPrefixPlugin ,
2525} from '../../tools/vite/plugins' ;
26+ import { EsbuildLoaderOption , getDepOptimizationConfig } from '../../tools/vite/utils' ;
2627import { loadProxyConfiguration , normalizeSourceMaps } from '../../utils' ;
2728import { useComponentStyleHmr , useComponentTemplateHmr } from '../../utils/environment-options' ;
2829import { loadEsmModule } from '../../utils/load-esm' ;
@@ -32,7 +33,6 @@ import {
3233 BuildOutputFileType ,
3334 type ExternalResultMetadata ,
3435 JavaScriptTransformer ,
35- getFeatureSupport ,
3636 getSupportedBrowsers ,
3737 isZonelessApp ,
3838 transformSupportedBrowsersToTargets ,
@@ -791,91 +791,6 @@ export async function setupServer(
791791 return configuration ;
792792}
793793
794- type ViteEsBuildPlugin = NonNullable <
795- NonNullable < DepOptimizationConfig [ 'esbuildOptions' ] > [ 'plugins' ]
796- > [ 0 ] ;
797-
798- type EsbuildLoaderOption = Exclude < DepOptimizationConfig [ 'esbuildOptions' ] , undefined > [ 'loader' ] ;
799-
800- function getDepOptimizationConfig ( {
801- disabled,
802- exclude,
803- include,
804- target,
805- zoneless,
806- prebundleTransformer,
807- ssr,
808- loader,
809- thirdPartySourcemaps,
810- } : {
811- disabled : boolean ;
812- exclude : string [ ] ;
813- include : string [ ] ;
814- target : string [ ] ;
815- prebundleTransformer : JavaScriptTransformer ;
816- ssr : boolean ;
817- zoneless : boolean ;
818- loader ?: EsbuildLoaderOption ;
819- thirdPartySourcemaps : boolean ;
820- } ) : DepOptimizationConfig {
821- const plugins : ViteEsBuildPlugin [ ] = [
822- {
823- name : 'angular-browser-node-built-in' ,
824- setup ( build ) {
825- // This namespace is configured by vite.
826- // @see : https://github.com/vitejs/vite/blob/a1dd396da856401a12c921d0cd2c4e97cb63f1b5/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L109
827- build . onLoad ( { filter : / .* / , namespace : 'browser-external' } , ( args ) => {
828- if ( ! isBuiltin ( args . path ) ) {
829- return ;
830- }
831-
832- return {
833- errors : [
834- {
835- text : `The package "${ args . path } " wasn't found on the file system but is built into node.` ,
836- } ,
837- ] ,
838- } ;
839- } ) ;
840- } ,
841- } ,
842- {
843- name : `angular-vite-optimize-deps${ ssr ? '-ssr' : '' } ${
844- thirdPartySourcemaps ? '-vendor-sourcemap' : ''
845- } `,
846- setup ( build ) {
847- build . onLoad ( { filter : / \. [ c m ] ? j s $ / } , async ( args ) => {
848- return {
849- contents : await prebundleTransformer . transformFile ( args . path ) ,
850- loader : 'js' ,
851- } ;
852- } ) ;
853- } ,
854- } ,
855- ] ;
856-
857- return {
858- // Exclude any explicitly defined dependencies (currently build defined externals)
859- exclude,
860- // NB: to disable the deps optimizer, set optimizeDeps.noDiscovery to true and optimizeDeps.include as undefined.
861- // Include all implict dependencies from the external packages internal option
862- include : disabled ? undefined : include ,
863- noDiscovery : disabled ,
864- // Add an esbuild plugin to run the Angular linker on dependencies
865- esbuildOptions : {
866- // Set esbuild supported targets.
867- target,
868- supported : getFeatureSupport ( target , zoneless ) ,
869- plugins,
870- loader,
871- define : {
872- 'ngServerMode' : `${ ssr } ` ,
873- } ,
874- resolveExtensions : [ '.mjs' , '.js' , '.cjs' ] ,
875- } ,
876- } ;
877- }
878-
879794/**
880795 * Checks if the given value is an absolute URL.
881796 *
0 commit comments