From 60eb62374bce804e45d9caaf0eefe56d5248b2b6 Mon Sep 17 00:00:00 2001 From: shellscape Date: Fri, 25 Oct 2024 22:10:41 -0400 Subject: [PATCH] fix(jsx-email): modify core plugin imports for bundler resolution --- .npmrc | 1 + packages/jsx-email/src/config.ts | 55 +++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/.npmrc b/.npmrc index 27c7b2b6..16df544a 100644 --- a/.npmrc +++ b/.npmrc @@ -5,6 +5,7 @@ auth-type=legacy # pnpm options always-auth = true +auto-install-peers = true enable-pre-post-scripts = true link-workspace-packages = false shamefully-hoist = true diff --git a/packages/jsx-email/src/config.ts b/packages/jsx-email/src/config.ts index b85d4b02..44f2326d 100644 --- a/packages/jsx-email/src/config.ts +++ b/packages/jsx-email/src/config.ts @@ -78,20 +78,49 @@ const checkName = (plugin: JsxEmailPlugin, source?: string) => { } }; -const importPlugin = async (name: string) => { +interface PluginImport { + plugin: JsxEmailPlugin; +} + +const handleImportError = (error: any, name: string) => { + if (error.code === 'ERR_MODULE_NOT_FOUND') { + log.error(chalk`{red jsx-email}: Tried to import plugin '${name}' but it wasn't found`); + } else { + log.error(error); + } +}; + +// Note: We have to be verbose here so that bundlers pick up on the imports. +// Most of them can't handle dynamically importing these plugins from variables containing +// their names without additional config, and we don't want that burden on users +const importInlinePlugin = async () => { + try { + const { plugin } = (await import('@jsx-email/plugin-inline')) as unknown as PluginImport; + return plugin; + } catch (error) { + handleImportError(error, '@jsx-email/plugin-inline'); + } + + return null; +}; + +const importMinifyPlugin = async () => { try { - const { plugin } = (await import(name)) as { plugin: JsxEmailPlugin }; + const { plugin } = (await import('@jsx-email/plugin-minify')) as unknown as PluginImport; + return plugin; + } catch (error) { + handleImportError(error, '@jsx-email/plugin-inline'); + } - checkSymbol(plugin, name); - checkName(plugin, name); + return null; +}; +const importPrettyPlugin = async () => { + try { + const { plugin } = (await import('@jsx-email/plugin-pretty')) as unknown as PluginImport; return plugin; } catch (error) { - if ((error as any).code === 'ERR_MODULE_NOT_FOUND') { - log.error(chalk`{red jsx-email}: Tried to import a plugin '${name}' but it wasn't found`); - } else { - log.error(error); - } + handleImportError(error, '@jsx-email/plugin-inline'); } return null; @@ -109,17 +138,17 @@ export const defineConfig = async (config: DefineConfigOptions = {}): Promise value); for (const plugin of result.plugins || []) { + checkName(plugin); + checkSymbol(plugin); (plugin as PluginInternal).log = getPluginLog(plugin.name); }