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(jsx-email,app-preview): --base-path flag, deploy preview to sub path #247

Merged
merged 1 commit into from
Nov 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion apps/preview/app/src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const getRouter = (templates: Record<string, TemplateData>) => {
</Layout>
),
errorElement: <Error />,
path: '/'
path: import.meta.env.VITE_JSXEMAIL_BASE_PATH || '/'
},
...routes
]);
Expand Down
12 changes: 7 additions & 5 deletions packages/jsx-email/src/cli/commands/preview.mts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Starts the preview server for a directory of email templates
$ email preview <template dir path> [...options]

{underline Options}
--base-path Default: /. Defines the path on a server that the preview app will be deployed to or available at
--build-path An absolute path. When set, builds the preview as a deployable app and saves to disk
--exclude A micromatch glob pattern that specifies files to exclude from the preview
--host Allow thew preview server to listen on all addresses (0.0.0.0)
Expand All @@ -46,15 +47,15 @@ Starts the preview server for a directory of email templates
$ email preview ./src/templates --build-path /tmp/email-preview
`;

const buildDeployable = async ({ targetPath, argv }: PreviewCommonParams) => {
const { buildPath } = argv;
const buildDeployable = async ({ argv, targetPath }: PreviewCommonParams) => {
const { basePath = '/', buildPath } = argv;
const common = { argv, targetPath };
await prepareBuild(common);
const config = await getConfig(common);

await viteBuild({
...config,
base: '/',
base: basePath,
build: {
minify: false,
outDir: buildPath,
Expand All @@ -73,17 +74,18 @@ const buildDeployable = async ({ targetPath, argv }: PreviewCommonParams) => {
});
};

const getConfig = async ({ targetPath, argv }: PreviewCommonParams) => {
const getConfig = async ({ argv, targetPath }: PreviewCommonParams) => {
const buildPath = await getTempPath('preview');
// @ts-ignore
const root = join(dirname(fileURLToPath(import.meta.resolve('@jsx-email/app-preview'))), 'app');
const { host = false, port = 55420 } = argv;
const { basePath = '/', host = false, port = 55420 } = argv;
// Note: The trailing slash is required
const relativePath = `${normalizePath(relative(root, targetPath))}/`;

newline();
log.info(chalk`{blue Starting build...}`);

process.env.VITE_JSXEMAIL_BASE_PATH = basePath;
process.env.VITE_JSXEMAIL_BUILD_PATH = `${normalizePath(relative(root, buildPath))}/`;
process.env.VITE_JSXEMAIL_RELATIVE_PATH = relativePath;

Expand Down
5 changes: 3 additions & 2 deletions packages/jsx-email/src/cli/commands/types.mts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type React from 'react';
import { boolean, number, object, optional, string, union, type Output as Infer } from 'valibot';

export type CommandFn = (flags: Flags, inputs: string[]) => Promise<boolean>;

export type Flags = Record<string, string | boolean | undefined>;

export type CommandFn = (flags: Flags, inputs: string[]) => Promise<boolean>;

export const PreviewCommandOptionsStruct = object({
basePath: optional(string()),
buildPath: optional(string()),
exclude: optional(string()),
host: optional(boolean()),
Expand Down
Loading