Skip to content

Commit f46d0b1

Browse files
committed
refactor(@angular/build): move getDepOptimizationConfig into utils file
Reduce the amount of code in vite-server.ts (cherry picked from commit b8b561d)
1 parent 6cfbb88 commit f46d0b1

File tree

2 files changed

+94
-87
lines changed

2 files changed

+94
-87
lines changed

packages/angular/build/src/builders/dev-server/vite-server.ts

+2-87
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import assert from 'node:assert';
1313
import { readFile } from 'node:fs/promises';
1414
import { builtinModules, isBuiltin } from 'node:module';
1515
import { join } from 'node:path';
16-
import type { Connect, DepOptimizationConfig, InlineConfig, ViteDevServer } from 'vite';
16+
import type { Connect, InlineConfig, ViteDevServer } from 'vite';
1717
import type { ComponentStyleRecord } from '../../tools/vite/middlewares';
1818
import {
1919
ServerSsrMode,
@@ -23,6 +23,7 @@ import {
2323
createAngularSsrTransformPlugin,
2424
createRemoveIdPrefixPlugin,
2525
} from '../../tools/vite/plugins';
26+
import { EsbuildLoaderOption, getDepOptimizationConfig } from '../../tools/vite/utils';
2627
import { loadProxyConfiguration, normalizeSourceMaps } from '../../utils';
2728
import { useComponentStyleHmr, useComponentTemplateHmr } from '../../utils/environment-options';
2829
import { 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: /\.[cm]?js$/ }, 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
*

packages/angular/build/src/tools/vite/utils.ts

+92
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
*/
88

99
import { lookup as lookupMimeType } from 'mrmime';
10+
import { isBuiltin } from 'node:module';
1011
import { extname } from 'node:path';
12+
import type { DepOptimizationConfig } from 'vite';
13+
import { JavaScriptTransformer } from '../esbuild/javascript-transformer';
14+
import { getFeatureSupport } from '../esbuild/utils';
1115

1216
export type AngularMemoryOutputFiles = Map<
1317
string,
@@ -33,3 +37,91 @@ export function lookupMimeTypeFromRequest(url: string): string | undefined {
3337

3438
return extension && lookupMimeType(extension);
3539
}
40+
41+
type ViteEsBuildPlugin = NonNullable<
42+
NonNullable<DepOptimizationConfig['esbuildOptions']>['plugins']
43+
>[0];
44+
45+
export type EsbuildLoaderOption = Exclude<
46+
DepOptimizationConfig['esbuildOptions'],
47+
undefined
48+
>['loader'];
49+
50+
export function getDepOptimizationConfig({
51+
disabled,
52+
exclude,
53+
include,
54+
target,
55+
zoneless,
56+
prebundleTransformer,
57+
ssr,
58+
loader,
59+
thirdPartySourcemaps,
60+
}: {
61+
disabled: boolean;
62+
exclude: string[];
63+
include: string[];
64+
target: string[];
65+
prebundleTransformer: JavaScriptTransformer;
66+
ssr: boolean;
67+
zoneless: boolean;
68+
loader?: EsbuildLoaderOption;
69+
thirdPartySourcemaps: boolean;
70+
}): DepOptimizationConfig {
71+
const plugins: ViteEsBuildPlugin[] = [
72+
{
73+
name: 'angular-browser-node-built-in',
74+
setup(build) {
75+
// This namespace is configured by vite.
76+
// @see: https://github.com/vitejs/vite/blob/a1dd396da856401a12c921d0cd2c4e97cb63f1b5/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L109
77+
build.onLoad({ filter: /.*/, namespace: 'browser-external' }, (args) => {
78+
if (!isBuiltin(args.path)) {
79+
return;
80+
}
81+
82+
return {
83+
errors: [
84+
{
85+
text: `The package "${args.path}" wasn't found on the file system but is built into node.`,
86+
},
87+
],
88+
};
89+
});
90+
},
91+
},
92+
{
93+
name: `angular-vite-optimize-deps${ssr ? '-ssr' : ''}${
94+
thirdPartySourcemaps ? '-vendor-sourcemap' : ''
95+
}`,
96+
setup(build) {
97+
build.onLoad({ filter: /\.[cm]?js$/ }, async (args) => {
98+
return {
99+
contents: await prebundleTransformer.transformFile(args.path),
100+
loader: 'js',
101+
};
102+
});
103+
},
104+
},
105+
];
106+
107+
return {
108+
// Exclude any explicitly defined dependencies (currently build defined externals)
109+
exclude,
110+
// NB: to disable the deps optimizer, set optimizeDeps.noDiscovery to true and optimizeDeps.include as undefined.
111+
// Include all implict dependencies from the external packages internal option
112+
include: disabled ? undefined : include,
113+
noDiscovery: disabled,
114+
// Add an esbuild plugin to run the Angular linker on dependencies
115+
esbuildOptions: {
116+
// Set esbuild supported targets.
117+
target,
118+
supported: getFeatureSupport(target, zoneless),
119+
plugins,
120+
loader,
121+
define: {
122+
'ngServerMode': `${ssr}`,
123+
},
124+
resolveExtensions: ['.mjs', '.js', '.cjs'],
125+
},
126+
};
127+
}

0 commit comments

Comments
 (0)