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

Fix issue importing local TS files #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
70 changes: 35 additions & 35 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const serverPath = isWindows ? serverFile.replaceAll("\\", "//") : serverFile;
* @typedef {import('astro').AstroUserConfig} AstroUserConfig
@typedef {import('astro').AstroConfig} AstroConfig
* @typedef {import('vite').Plugin} VitePlugin
*
*
* @typedef {import('./types').IntegrationOptions} IntegrationOptions
*
*
*/

/**
Expand All @@ -38,42 +38,42 @@ function vitePlugin(options) {
return {
name: '@matthewp/astro-fastify:vite',
async configureServer(server) {
const nextSymbol = Symbol('next');

/** @type {import('fastify').FastifyServerFactory} */
const serverFactory = (handler, opts) => {
server.middlewares.use((req, res, next) => {
req[nextSymbol] = next;
handler(req, res);
return async () => {
const nextSymbol = Symbol('next');
/** @type {import('fastify').FastifyServerFactory} */
const serverFactory = (handler, opts) => {
server.middlewares.use((req, res, next) => {
req[nextSymbol] = next;
handler(req, res);
});
return /** @type {import('http').Server} */(server.httpServer ?? new http.Server());
}

const fastify = Fastify({
logger: options?.logger ?? true,
serverFactory
});
return /** @type {import('http').Server} */(server.httpServer ?? new http.Server());
}

const fastify = Fastify({
logger: options?.logger ?? true,
serverFactory
});

if(options?.entry) {
const entry = entryToPath(options.entry);
const entryModule = await server.ssrLoadModule(entry);
const setupRoutes = entryModule.default;
if(typeof setupRoutes !== 'function') {
throw new Error(`@matthewp/astro-fastify: ${entry} should export a default function.`);

if(options?.entry) {
const entry = entryToPath(options.entry);
const entryModule = await server.ssrLoadModule(entry);
const setupRoutes = entryModule.default;
if(typeof setupRoutes !== 'function') {
throw new Error(`@matthewp/astro-fastify: ${entry} should export a default function.`);
}
setupRoutes(fastify);
}
setupRoutes(fastify);

// Final catch-all route forwards back to the Vite server
fastify.all('/*', function (request) {
/** @type {import('connect').NextFunction} */
const next = request.raw[nextSymbol];
if (typeof next === "function") {
next();
}
});
await fastify.ready();
}

// Final catch-all route forwards back to the Vite server
fastify.all('/*', function (request) {
/** @type {import('connect').NextFunction} */
const next = request.raw[nextSymbol];
if (typeof next === "function") {
next();
}
});

await fastify.ready();
},
transform(code, id) {
if(options?.entry && id.includes('@matthewp/astro-fastify/lib/server.js')) {
Expand Down