1
- import path from 'node:path '
1
+ import { type AliasOptions } from 'vite '
2
2
3
- import { escapeRegExp } from 'lodash'
4
- import resolve from 'resolve.exports'
5
- import { type Alias , type AliasOptions } from 'vite'
6
-
7
- import { type SanityMonorepo } from './sanityMonorepo'
3
+ import { getSanityPkgExportAliases } from './getBrowserAliases'
4
+ import { getMonorepoAliases } from './sanityMonorepo'
8
5
9
6
/**
10
7
* @internal
11
8
*/
12
9
export interface GetAliasesOptions {
13
- /** An optional monorepo configuration object . */
14
- monorepo ?: SanityMonorepo
10
+ /** An optional monorepo path . */
11
+ monorepoPath ?: string
15
12
/** The path to the sanity package.json file. */
16
- sanityPkgPath ? : string
13
+ sanityPkgPath : string
17
14
}
18
15
19
- /**
20
- * The following are the specifiers that are expected/allowed to be used within
21
- * a built Sanity studio in the browser. These are used in combination with
22
- * `resolve.exports` to determine the final entry point locations for each allowed specifier.
23
- *
24
- * There is also a corresponding test for this file that expects these to be
25
- * included in the `sanity` package.json. That test is meant to keep this list
26
- * in sync in the event we add another package subpath.
27
- *
28
- * @internal
29
- */
30
- export const browserCompatibleSanityPackageSpecifiers = [
31
- 'sanity' ,
32
- 'sanity/_createContext' ,
33
- 'sanity/_singletons' ,
34
- 'sanity/desk' ,
35
- 'sanity/presentation' ,
36
- 'sanity/router' ,
37
- 'sanity/structure' ,
38
- 'sanity/package.json' ,
39
- ]
40
-
41
- /**
42
- * These conditions should align with the conditions present in the
43
- * `package.json` of the `'sanity'` module. they are given to `resolve.exports`
44
- * in order to determine the correct entrypoint for the browser-compatible
45
- * package specifiers listed above.
46
- */
47
- const conditions = [ 'import' , 'browser' , 'default' ]
48
-
49
16
/**
50
17
* Returns an object of aliases for Vite to use.
51
18
*
@@ -55,61 +22,13 @@ const conditions = ['import', 'browser', 'default']
55
22
*
56
23
* If the project is within the monorepo, it uses the source files directly for a better
57
24
* development experience. Otherwise, it uses the `sanityPkgPath` and `conditions` to locate
58
- * the entry points for each subpath the Sanity module exports.
25
+ * the entry points for each subpath of the Sanity module exports.
59
26
*
60
27
* @internal
61
28
*/
62
- export function getAliases ( { monorepo, sanityPkgPath} : GetAliasesOptions ) : AliasOptions {
63
- // If the current Studio is located within the Sanity monorepo
64
- if ( monorepo ?. path ) {
65
- // Load monorepo aliases. This ensures that the Vite server uses the source files
66
- // instead of the compiled output, allowing for a better development experience.
67
- const aliasesPath = path . resolve ( monorepo . path , 'dev/aliases.cjs' )
68
-
69
- // Import the development aliases configuration
70
- // eslint-disable-next-line import/no-dynamic-require
71
- const devAliases : Record < string , string > = require ( aliasesPath )
72
-
73
- // Resolve each alias path relative to the monorepo path
74
- const monorepoAliases = Object . fromEntries (
75
- Object . entries ( devAliases ) . map ( ( [ key , modulePath ] ) => {
76
- return [ key , path . resolve ( monorepo . path , modulePath ) ]
77
- } ) ,
78
- )
79
-
80
- // Return the aliases configuration for monorepo
81
- return monorepoAliases
82
- }
83
-
84
- // If not in the monorepo, use the `sanityPkgPath`
85
- // to locate the entry points for each subpath the Sanity module exports
86
- if ( sanityPkgPath ) {
87
- // Load the package.json of the Sanity package
88
- // eslint-disable-next-line import/no-dynamic-require
89
- const pkg = require ( sanityPkgPath )
90
- const dirname = path . dirname ( sanityPkgPath )
91
-
92
- // Resolve the entry points for each allowed specifier
93
- const unifiedSanityAliases = browserCompatibleSanityPackageSpecifiers . reduce < Alias [ ] > (
94
- ( acc , next ) => {
95
- // Resolve the export path for the specifier using resolve.exports
96
- const dest = resolve . exports ( pkg , next , { browser : true , conditions} ) ?. [ 0 ]
97
- if ( ! dest ) return acc
98
-
99
- // Map the specifier to its resolved path
100
- acc . push ( {
101
- find : new RegExp ( `^${ escapeRegExp ( next ) } $` ) ,
102
- replacement : path . resolve ( dirname , dest ) ,
103
- } )
104
- return acc
105
- } ,
106
- [ ] ,
107
- )
108
-
109
- // Return the aliases configuration for external projects
110
- return unifiedSanityAliases
111
- }
112
-
113
- // Return an empty aliases configuration if no conditions are met
114
- return { }
29
+ export async function getAliases ( {
30
+ monorepoPath,
31
+ sanityPkgPath,
32
+ } : GetAliasesOptions ) : Promise < AliasOptions > {
33
+ return monorepoPath ? getMonorepoAliases ( monorepoPath ) : getSanityPkgExportAliases ( sanityPkgPath )
115
34
}
0 commit comments