Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Dynamic imports #2

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
19 changes: 19 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { fileURLToPath } from "node:url";
import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
entries: [
"src/index.ts",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the extension:

Suggested change
"src/index.ts",
"src/index",

{
Copy link
Contributor

@johannschopplich johannschopplich Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, using mkdist might be overcomplicating things. Could you please try just referencing all source files like services/resend in the entries option?

Like this:

export default defineBuildConfig({
  entries: [
    "src/index",
    "src/services/resend",
    "src/services/plunk"
    // etc.
  ],
  // You should now be able to omit the `externals`!
}

Rollup should automatically resolve these as separate chunks.

builder: "mkdist",
input: "src/services",
outDir: "dist",
},
],
clean: false,
declaration: true,
rollup: {
emitCJS: true,
},
externals: [fileURLToPath(new URL("src/services", import.meta.url))],
});
16 changes: 6 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import { PlunkService } from "./services/plunk";
import { PostmarkService } from "./services/postmark";
import { ResendService } from "./services/resend";
import { SendGridService } from "./services/sendgrid";
import { MailgunService } from "./services/mailgun";
import type { EmailProvider } from "./types/email-options";
import type { EmailService } from "./types/email-service";

/**
* Factory function to get the email service based on the provider
* @param provider - The email provider
* @returns The email service instance
* @returns A Promise that resolves to the email service instance
* @throws Error if the provider is not supported
*/
export function useEmail(provider: EmailProvider): EmailService {
export async function useEmail(provider: EmailProvider): Promise<EmailService> {
switch (provider) {
case "resend": {
const { ResendService } = await import("./services/resend");
return new ResendService();
}
case "plunk": {
const { PlunkService } = await import("./services/plunk");
return new PlunkService();
}
case "sendgrid": {
const { SendGridService } = await import("./services/sendgrid");
return new SendGridService();
}
case "postmark": {
const { PostmarkService } = await import("./services/postmark");
return new PostmarkService();
}
case "mailgun": {
return MailgunService();
}
default: {
throw new Error(`Unsupported email provider: ${provider}`);

Check failure on line 29 in src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unhandled error

Error: Unsupported email provider: unsupported ❯ Module.useEmail src/index.ts:29:13 ❯ test/index.test.ts:18:18 ❯ Proxy.assertThrows node_modules/.pnpm/[email protected]/node_modules/chai/chai.js:2682:5 ❯ Proxy.methodWrapper node_modules/.pnpm/[email protected]/node_modules/chai/chai.js:1591:25 ❯ Proxy.<anonymous> node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/expect/dist/index.js:977:16 ❯ Proxy.overwritingMethodWrapper node_modules/.pnpm/[email protected]/node_modules/chai/chai.js:1647:33 ❯ Proxy.<anonymous> node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/expect/dist/index.js:1512:21 ❯ Proxy.<anonymous> node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/expect/dist/index.js:920:17 ❯ Proxy.methodWrapper node_modules/.pnpm/[email protected]/node_modules/chai/chai.js:1591:25 ❯ test/index.test.ts:18:43 This error originated in "test/index.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "test/index.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
}
}
}
Loading