Skip to content

Commit b8b561d

Browse files
committed
refactor(@angular/build): move getDepOptimizationConfig into utils file
Reduce the amount of code in vite-server.ts
1 parent 2c2ac84 commit b8b561d

File tree

2 files changed

+94
-87
lines changed

2 files changed

+94
-87
lines changed

Diff for: 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,
@@ -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: /\.[cm]?js$/ }, 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
*

Diff for: 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)