Skip to content

Commit

Permalink
feat(jsx-email,app-preview): --base-path flag, deploy preview to sub …
Browse files Browse the repository at this point in the history
…path
  • Loading branch information
shellscape committed Nov 23, 2024
1 parent 6c8fdd2 commit 6080649
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
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

0 comments on commit 6080649

Please sign in to comment.