@@ -63,13 +63,18 @@ const debug = createDebugger('vite:resolve-details', {
63
63
64
64
export interface ResolveOptions {
65
65
mainFields ?: string [ ]
66
+ /**
67
+ * @deprecated In future, `mainFields` should be used instead.
68
+ * @default true
69
+ */
70
+ browserField ?: boolean
66
71
conditions ?: string [ ]
67
72
extensions ?: string [ ]
68
73
dedupe ?: string [ ]
69
74
preserveSymlinks ?: boolean
70
75
}
71
76
72
- export interface InternalResolveOptions extends ResolveOptions {
77
+ export interface InternalResolveOptions extends Required < ResolveOptions > {
73
78
root : string
74
79
isBuild : boolean
75
80
isProduction : boolean
@@ -85,7 +90,6 @@ export interface InternalResolveOptions extends ResolveOptions {
85
90
tryPrefix ?: string
86
91
skipPackageJson ?: boolean
87
92
preferRelative ?: boolean
88
- preserveSymlinks ?: boolean
89
93
isRequire ?: boolean
90
94
// #3040
91
95
// when the importer is a ts module,
@@ -238,6 +242,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
238
242
239
243
if (
240
244
targetWeb &&
245
+ options . browserField &&
241
246
( res = tryResolveBrowserMapping ( fsPath , importer , options , true ) )
242
247
) {
243
248
return res
@@ -308,6 +313,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
308
313
309
314
if (
310
315
targetWeb &&
316
+ options . browserField &&
311
317
( res = tryResolveBrowserMapping (
312
318
id ,
313
319
importer ,
@@ -451,7 +457,7 @@ function tryFsResolve(
451
457
return res
452
458
}
453
459
454
- for ( const ext of options . extensions || DEFAULT_EXTENSIONS ) {
460
+ for ( const ext of options . extensions ) {
455
461
if (
456
462
postfix &&
457
463
( res = tryResolveFile (
@@ -892,7 +898,11 @@ export function resolvePackageEntry(
892
898
// This is because .mjs files can technically import .cjs files which would
893
899
// make them invalid for pure ESM environments - so if other module/browser
894
900
// fields are present, prioritize those instead.
895
- if ( targetWeb && ( ! entryPoint || entryPoint . endsWith ( '.mjs' ) ) ) {
901
+ if (
902
+ targetWeb &&
903
+ options . browserField &&
904
+ ( ! entryPoint || entryPoint . endsWith ( '.mjs' ) )
905
+ ) {
896
906
// check browser field
897
907
// https://github.com/defunctzombie/package-browser-field-spec
898
908
const browserEntry =
@@ -903,6 +913,7 @@ export function resolvePackageEntry(
903
913
// check if the package also has a "module" field.
904
914
if (
905
915
! options . isRequire &&
916
+ options . mainFields . includes ( 'module' ) &&
906
917
typeof data . module === 'string' &&
907
918
data . module !== browserEntry
908
919
) {
@@ -933,7 +944,8 @@ export function resolvePackageEntry(
933
944
}
934
945
935
946
if ( ! entryPoint || entryPoint . endsWith ( '.mjs' ) ) {
936
- for ( const field of options . mainFields || DEFAULT_MAIN_FIELDS ) {
947
+ for ( const field of options . mainFields ) {
948
+ if ( field === 'browser' ) continue // already checked above
937
949
if ( typeof data [ field ] === 'string' ) {
938
950
entryPoint = data [ field ]
939
951
break
@@ -951,16 +963,16 @@ export function resolvePackageEntry(
951
963
for ( let entry of entryPoints ) {
952
964
// make sure we don't get scripts when looking for sass
953
965
if (
954
- options . mainFields ?. [ 0 ] === 'sass' &&
955
- ! options . extensions ? .includes ( path . extname ( entry ) )
966
+ options . mainFields [ 0 ] === 'sass' &&
967
+ ! options . extensions . includes ( path . extname ( entry ) )
956
968
) {
957
969
entry = ''
958
970
options . skipPackageJson = true
959
971
}
960
972
961
973
// resolve object browser field in package.json
962
974
const { browser : browserField } = data
963
- if ( targetWeb && isObject ( browserField ) ) {
975
+ if ( targetWeb && options . browserField && isObject ( browserField ) ) {
964
976
entry = mapWithBrowserField ( entry , browserField ) || entry
965
977
}
966
978
@@ -1001,7 +1013,7 @@ function resolveExports(
1001
1013
if ( ! options . isRequire ) {
1002
1014
conditions . push ( 'module' )
1003
1015
}
1004
- if ( options . conditions ) {
1016
+ if ( options . conditions . length > 0 ) {
1005
1017
conditions . push ( ...options . conditions )
1006
1018
}
1007
1019
@@ -1053,7 +1065,7 @@ function resolveDeepImport(
1053
1065
`${ path . join ( dir , 'package.json' ) } .`
1054
1066
)
1055
1067
}
1056
- } else if ( targetWeb && isObject ( browserField ) ) {
1068
+ } else if ( targetWeb && options . browserField && isObject ( browserField ) ) {
1057
1069
// resolve without postfix (see #7098)
1058
1070
const { file, postfix } = splitFileAndPostfix ( relativeId )
1059
1071
const mapped = mapWithBrowserField ( file , browserField )
0 commit comments