Skip to content

Commit

Permalink
#438: Implemented middleware configuration option to the Vite plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
petermasking committed Jan 8, 2024
1 parent 3b6f13e commit 47de693
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/plugin-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ function createServerConfig(jitarUrl: string)
};
}

function createBootstrapCode(segments: string[]): string
function createBootstrapCode(segments: string[], middlewares: string[]): string
{
const segmentString = segments.map(segment => `'${segment}'`).join(', ');
const middlewareString = middlewares.map(middleware => `'${middleware}'`).join(', ');

return `<script type="module">const jitar = await import("/jitar/client.js"); await jitar.startClient(document.location.origin, [${segmentString}]);</script>`;
return `<script type="module">const jitar = await import("/jitar/client.js"); await jitar.startClient(document.location.origin, [${segmentString}], [${middlewareString}]);</script>`;
}

async function createImportCode(code: string, id: string, jitarFullPath: string, jitarPath: string): Promise<string>
Expand Down Expand Up @@ -81,7 +82,7 @@ async function createImportCode(code: string, id: string, jitarFullPath: string,
+ exportCode;
}

export default function viteJitar(sourcePath: string, jitarPath: string, jitarUrl: string, segments: string[] = []): PluginOption
export default function viteJitar(sourcePath: string, jitarPath: string, jitarUrl: string, segments: string[] = [], middlewares: string[] = []): PluginOption
{
sourcePath = formatPath(sourcePath);
jitarPath = formatPath(jitarPath);
Expand Down Expand Up @@ -126,7 +127,7 @@ export default function viteJitar(sourcePath: string, jitarPath: string, jitarUr

transformIndexHtml(html)
{
return html.replace('<head>', `<head>${createBootstrapCode(segments)}`);
return html.replace('<head>', `<head>${createBootstrapCode(segments, middlewares)}`);
}

} as PluginOption;
Expand Down

0 comments on commit 47de693

Please sign in to comment.