Skip to content

Commit

Permalink
fix(core): use find replacement syntax for aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler committed Jul 19, 2024
1 parent 196b560 commit 732b37b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
26 changes: 19 additions & 7 deletions packages/sanity/src/_internal/cli/server/__tests__/aliases.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from 'node:path'

import {describe, expect, it, jest} from '@jest/globals'
import {escapeRegExp} from 'lodash'
import resolve from 'resolve.exports'
import {type Alias} from 'vite'

import {browserCompatibleSanityPackageSpecifiers, getAliases} from '../aliases'

Expand Down Expand Up @@ -49,24 +51,28 @@ describe('getAliases', () => {
const aliases = getAliases({
sanityPkgPath,
conditions: ['import', 'browser'],
browser: true,
})

const expectedAliases = browserCompatibleSanityPackageSpecifiers.reduce<Record<string, string>>(
(acc, specifier) => {
const dest = resolve.exports(pkg, specifier, {
// Prepare expected aliases
const dirname = path.dirname(sanityPkgPath)
const expectedAliases = browserCompatibleSanityPackageSpecifiers.reduce<Alias[]>(
(acc, next) => {
const dest = resolve.exports(pkg, next, {
browser: true,
conditions: ['import', 'browser'],
})?.[0]
if (dest) {
acc[specifier] = path.resolve(path.dirname(sanityPkgPath), dest)
acc.push({
find: new RegExp(`^${escapeRegExp(next)}$`),
replacement: path.resolve(dirname, dest),
})
}
return acc
},
{},
[],
)

expect(aliases).toMatchObject(expectedAliases)
expect(aliases).toEqual(expectedAliases)
})

it('returns the correct aliases for the monorepo', () => {
Expand All @@ -90,4 +96,10 @@ describe('getAliases', () => {

expect(aliases).toMatchObject(expectedAliases)
})

it('returns an empty object if no conditions are met', () => {
const aliases = getAliases({})

expect(aliases).toEqual({})
})
})
27 changes: 16 additions & 11 deletions packages/sanity/src/_internal/cli/server/aliases.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'node:path'

import {escapeRegExp} from 'lodash'
import resolve from 'resolve.exports'
import {type AliasOptions} from 'vite'
import {type Alias, type AliasOptions} from 'vite'

import {type SanityMonorepo} from './sanityMonorepo'

Expand Down Expand Up @@ -83,17 +84,21 @@ export function getAliases({monorepo, sanityPkgPath, conditions}: GetAliasesOpti
const dirname = path.dirname(sanityPkgPath)

// Resolve the entry points for each allowed specifier
const unifiedSanityAliases = browserCompatibleSanityPackageSpecifiers.reduce<
Record<string, string>
>((acc, next) => {
// Resolve the export path for the specifier using resolve.exports
const dest = resolve.exports(pkg, next, {browser: true, conditions})?.[0]
if (!dest) return acc
const unifiedSanityAliases = browserCompatibleSanityPackageSpecifiers.reduce<Alias[]>(
(acc, next) => {
// Resolve the export path for the specifier using resolve.exports
const dest = resolve.exports(pkg, next, {browser: true, conditions})?.[0]
if (!dest) return acc

// Map the specifier to its resolved path
acc[next] = path.resolve(dirname, dest)
return acc
}, {})
// Map the specifier to its resolved path
acc.push({
find: new RegExp(`^${escapeRegExp(next)}$`),
replacement: path.resolve(dirname, dest),
})
return acc
},
[],
)

// Return the aliases configuration for external projects
return unifiedSanityAliases
Expand Down

0 comments on commit 732b37b

Please sign in to comment.