better-svelte-email 0.3.5 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,6 +34,7 @@ type EmailListProps = {
34
34
  * ```
35
35
  */
36
36
  export declare const emailList: ({ path: emailPath, root }?: EmailListProps) => PreviewData;
37
+ export declare const getEmailComponent: (emailPath: string, file: string) => Promise<any>;
37
38
  /**
38
39
  * SvelteKit form action to render an email component.
39
40
  * Use this with the Preview component to render email templates on demand.
@@ -100,4 +101,5 @@ export declare const sendEmail: ({ customSendEmailFunction, resendApiKey }?: {
100
101
  error: any;
101
102
  }>;
102
103
  };
104
+ export declare function getFiles(dir: string, files?: string[]): string[];
103
105
  export { default as EmailPreview } from './EmailPreview.svelte';
@@ -42,17 +42,22 @@ export const emailList = ({ path: emailPath = '/src/lib/emails', root } = {}) =>
42
42
  console.warn(`Email directory not found: ${fullPath}`);
43
43
  return { files: null, path: emailPath };
44
44
  }
45
- const files = createEmailComponentList(emailPath, getFiles(fullPath));
45
+ // Use the absolute folder path as the root when creating the component list so
46
+ // we can compute correct relative paths on all platforms.
47
+ const files = createEmailComponentList(fullPath, getFiles(fullPath));
46
48
  if (!files.length) {
47
49
  return { files: null, path: emailPath };
48
50
  }
49
51
  return { files, path: emailPath };
50
52
  };
51
- const getEmailComponent = async (emailPath, file) => {
53
+ export const getEmailComponent = async (emailPath, file) => {
52
54
  const fileName = `${file}.svelte`;
53
55
  try {
54
56
  // Import the email component dynamically
55
- return (await import(/* @vite-ignore */ path.join(emailPath, fileName))).default;
57
+ const normalizedEmailPath = emailPath.replace(/\\/g, '/').replace(/\/+$/, '');
58
+ const normalizedFile = file.replace(/\\/g, '/').replace(/^\/+/, '');
59
+ const importPath = `${normalizedEmailPath}/${normalizedFile}.svelte`;
60
+ return (await import(/* @vite-ignore */ importPath)).default;
56
61
  }
57
62
  catch (err) {
58
63
  throw new Error(`Failed to import email component '${fileName}'. Make sure the file exists and includes the <Head /> component.\nOriginal error: ${err}`);
@@ -179,7 +184,7 @@ export const sendEmail = ({ customSendEmailFunction, resendApiKey } = {}) => {
179
184
  };
180
185
  };
181
186
  // Recursive function to get files
182
- function getFiles(dir, files = []) {
187
+ export function getFiles(dir, files = []) {
183
188
  // Get an array of all files and directories in the passed directory using fs.readdirSync
184
189
  const fileList = fs.readdirSync(dir);
185
190
  // Create the full path of the file/directory by concatenating the passed directory and file/directory name
@@ -203,9 +208,25 @@ function getFiles(dir, files = []) {
203
208
  function createEmailComponentList(root, paths) {
204
209
  const emailComponentList = [];
205
210
  paths.forEach((filePath) => {
206
- if (filePath.includes(`.svelte`)) {
207
- const fileName = filePath.substring(filePath.indexOf(root) + root.length + 1, filePath.indexOf('.svelte'));
208
- emailComponentList.push(fileName);
211
+ if (filePath.endsWith('.svelte')) {
212
+ // Get the directory name from the full path
213
+ const fileDir = path.dirname(filePath);
214
+ // Get the base name without extension
215
+ const baseName = path.basename(filePath, '.svelte');
216
+ // Normalize paths for cross-platform comparison
217
+ const rootNormalized = path.normalize(root);
218
+ const fileDirNormalized = path.normalize(fileDir);
219
+ // Find where root appears in the full directory path
220
+ const rootIndex = fileDirNormalized.indexOf(rootNormalized);
221
+ if (rootIndex !== -1) {
222
+ // Get everything after the root path
223
+ const afterRoot = fileDirNormalized.substring(rootIndex + rootNormalized.length);
224
+ // Combine with the base name using path.join for proper separators
225
+ const relativePath = afterRoot ? path.join(afterRoot, baseName) : baseName;
226
+ // Remove leading path separators
227
+ const cleanPath = relativePath.replace(/^[/\\]+/, '');
228
+ emailComponentList.push(cleanPath);
229
+ }
209
230
  }
210
231
  });
211
232
  return emailComponentList;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-svelte-email",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "author": "Konixy",
5
5
  "repository": {
6
6
  "type": "git",