@@ -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 ,
@@ -775,91 +775,6 @@ export async function setupServer(
775775 return configuration ;
776776}
777777
778- type ViteEsBuildPlugin = NonNullable <
779- NonNullable < DepOptimizationConfig [ 'esbuildOptions' ] > [ 'plugins' ]
780- > [ 0 ] ;
781-
782- type EsbuildLoaderOption = Exclude < DepOptimizationConfig [ 'esbuildOptions' ] , undefined > [ 'loader' ] ;
783-
784- function getDepOptimizationConfig ( {
785- disabled,
786- exclude,
787- include,
788- target,
789- zoneless,
790- prebundleTransformer,
791- ssr,
792- loader,
793- thirdPartySourcemaps,
794- } : {
795- disabled : boolean ;
796- exclude : string [ ] ;
797- include : string [ ] ;
798- target : string [ ] ;
799- prebundleTransformer : JavaScriptTransformer ;
800- ssr : boolean ;
801- zoneless : boolean ;
802- loader ?: EsbuildLoaderOption ;
803- thirdPartySourcemaps : boolean ;
804- } ) : DepOptimizationConfig {
805- const plugins : ViteEsBuildPlugin [ ] = [
806- {
807- name : 'angular-browser-node-built-in' ,
808- setup ( build ) {
809- // This namespace is configured by vite.
810- // @see : https://github.com/vitejs/vite/blob/a1dd396da856401a12c921d0cd2c4e97cb63f1b5/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L109
811- build . onLoad ( { filter : / .* / , namespace : 'browser-external' } , ( args ) => {
812- if ( ! isBuiltin ( args . path ) ) {
813- return ;
814- }
815-
816- return {
817- errors : [
818- {
819- text : `The package "${ args . path } " wasn't found on the file system but is built into node.` ,
820- } ,
821- ] ,
822- } ;
823- } ) ;
824- } ,
825- } ,
826- {
827- name : `angular-vite-optimize-deps${ ssr ? '-ssr' : '' } ${
828- thirdPartySourcemaps ? '-vendor-sourcemap' : ''
829- } `,
830- setup ( build ) {
831- build . onLoad ( { filter : / \. [ c m ] ? j s $ / } , async ( args ) => {
832- return {
833- contents : await prebundleTransformer . transformFile ( args . path ) ,
834- loader : 'js' ,
835- } ;
836- } ) ;
837- } ,
838- } ,
839- ] ;
840-
841- return {
842- // Exclude any explicitly defined dependencies (currently build defined externals)
843- exclude,
844- // NB: to disable the deps optimizer, set optimizeDeps.noDiscovery to true and optimizeDeps.include as undefined.
845- // Include all implict dependencies from the external packages internal option
846- include : disabled ? undefined : include ,
847- noDiscovery : disabled ,
848- // Add an esbuild plugin to run the Angular linker on dependencies
849- esbuildOptions : {
850- // Set esbuild supported targets.
851- target,
852- supported : getFeatureSupport ( target , zoneless ) ,
853- plugins,
854- loader,
855- define : {
856- 'ngServerMode' : `${ ssr } ` ,
857- } ,
858- resolveExtensions : [ '.mjs' , '.js' , '.cjs' ] ,
859- } ,
860- } ;
861- }
862-
863778/**
864779 * Checks if the given value is an absolute URL.
865780 *
0 commit comments