Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/windows-aliased-dynamic-import-reload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-email': patch
---

Resolve tsconfig path aliases in dynamic import template literals on Windows, so editing a file inside an aliased directory reloads the preview.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import path from 'node:path';
import { resolvePathAliases } from './resolve-path-aliases.js';
import {
resolveAliasedDirectoryPrefix,
resolvePathAliases,
} from './resolve-path-aliases.js';

test('resolveImports()', async () => {
expect(
Expand All @@ -9,3 +12,21 @@ test('resolveImports()', async () => {
),
).toEqual(['./some-file']);
});

test('resolveAliasedDirectoryPrefix() accepts platform-normalized separators', () => {
const sourceDirectory = path.resolve(
import.meta.dirname,
'./test/aliased-import-graph/src',
);
const localesDirectory = path.join(sourceDirectory, 'locales');

expect(resolveAliasedDirectoryPrefix('@/locales', sourceDirectory)).toEqual(
localesDirectory,
);

// On Windows, `path.normalize()` turns the specifier's forward slashes into
// backslashes before the prefix reaches this function.
expect(resolveAliasedDirectoryPrefix('@\\locales', sourceDirectory)).toEqual(
localesDirectory,
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ export const resolveAliasedDirectoryPrefix = (
configLoadResult.absoluteBaseUrl,
configLoadResult.paths,
);
const resolved = matchPath(prefix, undefined, isExistingDirectory, [

// `paths` patterns are always written with forward slashes, but the prefix may
// have been normalized with the platform separator (backslashes on Windows) by
// `path.normalize()`. Restore import specifier separators so patterns match.
const importPrefix = prefix.replace(/\\/g, '/');

const resolved = matchPath(importPrefix, undefined, isExistingDirectory, [
'.tsx',
'.ts',
'.js',
Expand Down