Skip to content

Commit

Permalink
fix(jsx-email): allow preview server to serve files outside of worksp…
Browse files Browse the repository at this point in the history
…ace root (#105)

Co-authored-by: Affan Shahid <[email protected]>
Co-authored-by: Paul Ehikhuemen <[email protected]>
Co-authored-by: Andrew Powell <[email protected]>
  • Loading branch information
4 people authored Dec 26, 2023
1 parent df9347e commit 5747cc4
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 10 deletions.
68 changes: 68 additions & 0 deletions apps/demo/emails/local-assets.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Body, Container, Head, Hr, Html, Img, Link, Preview, Section, Text } from 'jsx-email';

import * as React from 'react';

export const TemplateName = 'local-assets';

const main = {
backgroundColor: '#f6f9fc',
fontFamily:
'-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Ubuntu,sans-serif'
};

const container = {
backgroundColor: '#ffffff',
margin: '0 auto',
marginBottom: '64px',
padding: '20px 0 48px'
};

const box = {
padding: '0 48px'
};

const hr = {
borderColor: '#e6ebf1',
margin: '20px 0'
};

const paragraph = {
color: '#777',
fontSize: '16px',
lineHeight: '24px',
textAlign: 'left' as const
};

const anchor: React.CSSProperties = {
color: '#3869d4',
textDecoration: 'underline'
};

const baseUrl = import.meta.env.DEV
? import.meta.resolve('./static/')
: 'https://assets.example.com/';

export const Template = () => {
const catUrl = `${baseUrl}cat.jpeg`;
return (
<Html>
<Head />
<Preview>This is our email preview text for </Preview>
<Body style={main}>
<Container style={container}>
<Section style={box}>
<Img src={catUrl} alt="Cat" width="200" height="200" />
<Hr style={hr} />
<Text style={paragraph}>
URL:
<br />
<Link style={anchor} href={catUrl}>
{catUrl}
</Link>
</Text>
</Section>
</Container>
</Body>
</Html>
);
};
Binary file added apps/demo/emails/static/cat.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
},
"dependencies": {
"jsx-email": "workspace:*"
},
"devDependencies": {
"@types/node": "20.10.5"
}
}
7 changes: 7 additions & 0 deletions apps/demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../shared/tsconfig.base.json",
"compilerOptions": {
"module": "ESNext",
"types": ["./node_modules/jsx-email/node_modules/vite/client", "./node_modules/@types/node"]
}
}
17 changes: 17 additions & 0 deletions docs/components/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ referenced, so avoid using these. See [Can I
Email](https://www.caniemail.com/features/image-svg/) for more information.
:::

::: tip
To use local images during development, you can resolve the path with node.
Just remember, for production, you'll need to host the images somewhere and
reference them with a URL.

```jsx
import { Img } from 'jsx-email';

const baseUrl = import.meta.DEV ? import.meta.resolve('../assets/') : 'https://assets.example.com/';

const Email = () => {
return <Img src={`${baseUrl}cat.jpg`} alt="Cat" width="300" height="300" />;
};
```

:::

## Component Props

```ts
Expand Down
4 changes: 2 additions & 2 deletions packages/jsx-email/src/cli/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export const start = async (targetPath: string, argv: PreviewOptions) => {
...viteConfig.resolve?.alias
}
},
server: { host, port: parseInt(port as any, 10) }
} as InlineConfig;
server: { fs: { strict: false }, host, port: parseInt(port.toString(), 10) }
} satisfies InlineConfig;

const server = await createServer(mergedConfig);

Expand Down
66 changes: 58 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5747cc4

Please sign in to comment.