Skip to content

Commit

Permalink
Support basename in path generation
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck committed May 23, 2024
1 parent 686a5fa commit 5d55bdf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import { remixRoutes } from "remix-routes/vite";
export default defineConfig({
plugins: [
remix(),
remixRoutes(options?)
remixRoutes({
// Optional configuration for basename
basename: "/basepath", // Specify the basename here
})
],
});
```
Expand All @@ -35,6 +38,7 @@ Supported config options:

- `strict: boolean`
- `outDir: string`
- `basename: string` <!---> - Specify the basename for your routes.

### Without Vite

Expand Down
7 changes: 4 additions & 3 deletions packages/remix-routes/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { template } from './template';
interface Options {
strict?: boolean;
outputDirPath: string;
basename?: string; // Add basename to Options interface
}

type RoutesInfo = Record<string, {
Expand All @@ -21,7 +22,7 @@ type RoutesInfo = Record<string, {

export const DEFAULT_OUTPUT_DIR_PATH = './node_modules'

async function buildHelpers(config: RemixConfig): Promise<[RoutesInfo, string[]]> {
async function buildHelpers(config: RemixConfig, basename: string = ''): Promise<[RoutesInfo, string[]]> { // Add basename parameter to buildHelpers function
const routesInfo: RoutesInfo = {};
const routeIds: string[] = [];
const handleRoutesRecursive = (
Expand Down Expand Up @@ -83,7 +84,7 @@ function expandOptionalStaticSegments(path: string) {
}

export async function build(remixRoot: string, remixConfig: RemixConfig, options: Options) {
const [routesInfo, routeIds] = await buildHelpers(remixConfig);
const [routesInfo, routeIds] = await buildHelpers(remixConfig, options.basename); // Pass basename to buildHelpers
generate(remixRoot, remixConfig, routesInfo, routeIds, options);
}

Expand Down Expand Up @@ -112,7 +113,7 @@ function generate(remixRoot: string, remixConfig: RemixConfig, routesInfo: Route
strictMode: options.strict,
relativeAppDirPath,
routes: Object.entries(routesInfo).map(([route, { fileName, params }]) => ({
route,
route: options.basename ? `${options.basename}${route}` : route, // Prepend basename to route
params,
fileName: slash(fileName.replace(/\.tsx?$/, '')),
})).sort((a, b) => a.route.localeCompare(b.route)),
Expand Down
8 changes: 7 additions & 1 deletion packages/remix-routes/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DEFAULT_OUTPUT_DIR_PATH, build } from './build';
interface PluginConfig {
strict?: boolean;
outDir?: string;
basename?: string; // Add basename to PluginConfig
}

const RemixPluginContextName = '__remixPluginContext';
Expand All @@ -19,7 +20,8 @@ export function remixRoutes(pluginConfig: PluginConfig = {}): Vite.Plugin {
if (!ctx) {
return;
}
build(rootDirectory, ctx.remixConfig, { strict: pluginConfig.strict, outputDirPath: pluginConfig.outDir || DEFAULT_OUTPUT_DIR_PATH });
// Utilize the basename parameter when generating paths
build(rootDirectory, ctx.remixConfig, { strict: pluginConfig.strict, outputDirPath: pluginConfig.outDir || DEFAULT_OUTPUT_DIR_PATH, basename: pluginConfig.basename });
}

async function reloadCtx() {
Expand All @@ -41,6 +43,10 @@ export function remixRoutes(pluginConfig: PluginConfig = {}): Vite.Plugin {
}
rootDirectory = config.root;
ctx = (config as any)[RemixPluginContextName];
// Extract basename from the Remix plugin options and pass it to generateTypeFile
if (remixPlugin.options && remixPlugin.options.basename) {
pluginConfig.basename = remixPlugin.options.basename;
}
generateTypeFile();
},
async watchChange(id, change) {
Expand Down

0 comments on commit 5d55bdf

Please sign in to comment.