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: only use Nitro during serve, build #156

Merged
merged 2 commits into from
Nov 19, 2022
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
6 changes: 3 additions & 3 deletions packages/platform/src/lib/vite-nitro-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ describe('viteNitroPlugin', () => {
expect(viteNitroPlugin({}).name).toEqual('vite-nitro-plugin');
});

it(`should have the route middleware "/api" `, async () => {
it(`should not call the route middleware in test mode `, async () => {
// Arrange
const spy = vi.spyOn(mockViteDevServer.middlewares, 'use');

// Act
await (viteNitroPlugin({}).configureServer as any)(mockViteDevServer);

// Assert
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('/api', expect.anything());
expect(spy).toHaveBeenCalledTimes(0);
expect(spy).not.toHaveBeenCalledWith('/api', expect.anything());
});
});
60 changes: 35 additions & 25 deletions packages/platform/src/lib/vite-nitro-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,49 @@ export function viteNitroPlugin(opts?: NitroConfig): Plugin {
buildDir: '../dist/.nitro',
...opts,
};
let isBuild = false;
let isServe = false;
let isTest = process.env['NODE_ENV'] === 'test' || !!process.env['VITEST'];
return {
name: 'vite-nitro-plugin',

config(_config, { command }) {
isServe = command === 'serve';
isBuild = command === 'build';
},
async configureServer(viteServer: ViteDevServer) {
const { createNitro, createDevServer, build, prepare } =
await loadEsmModule<typeof import('nitropack')>('nitropack');
if (isServe && !isTest) {
const { createNitro, createDevServer, build, prepare } =
await loadEsmModule<typeof import('nitropack')>('nitropack');

const nitro = await createNitro({ ...nitroConfig, dev: true });
const server = createDevServer(nitro);
await prepare(nitro);
await build(nitro);
viteServer.middlewares.use('/api', toNodeListener(server.app));
console.log(
`\n\nThe '@analogjs/platform' successfully started.\nThe server endpoints are accessible under the "/api"`
);
const nitro = await createNitro({ ...nitroConfig, dev: true });
const server = createDevServer(nitro);
await prepare(nitro);
await build(nitro);
viteServer.middlewares.use('/api', toNodeListener(server.app));
console.log(
`\n\nThe '@analogjs/platform' successfully started.\nThe server endpoints are accessible under the "/api"`
);
}
},

async closeBundle() {
const { createNitro, build, prepare } = await loadEsmModule<
typeof import('nitropack')
>('nitropack');
if (isBuild) {
const { createNitro, build, prepare } = await loadEsmModule<
typeof import('nitropack')
>('nitropack');

const nitro = await createNitro({
...nitroConfig,
baseURL: '/api',
dev: false,
});
await prepare(nitro);
await build(nitro);
await nitro.close();
console.log(
`\n\nThe '@analogjs/platform' server has been successfully built.`
);
const nitro = await createNitro({
...nitroConfig,
baseURL: '/api',
dev: false,
});
await prepare(nitro);
await build(nitro);
await nitro.close();
console.log(
`\n\nThe '@analogjs/platform' server has been successfully built.`
);
}
},
};
}