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

feat(vite-plugin-nitro): adjust output paths for vercel preset #525

Merged
merged 2 commits into from
Jul 8, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ export const mockNitroConfig: NitroConfig = {
},
};

export async function mockBuildFunctions(): Promise<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[Mock<any, any>, Mock<any, any>, Mock<any, any>]
> {
export async function mockBuildFunctions() {
const buildServerImport = await import('./build-server');
const buildServerImportSpy = vi.fn();
buildServerImport.buildServer = buildServerImportSpy;
Expand All @@ -54,7 +51,7 @@ export async function mockBuildFunctions(): Promise<
const buildSitemapImportSpy = vi.fn();
buildSitemapImport.buildSitemap = buildSitemapImportSpy;

return [buildSSRAppImportSpy, buildServerImportSpy, buildSitemapImportSpy];
return { buildSSRAppImportSpy, buildServerImportSpy, buildSitemapImportSpy };
}

export async function runConfigAndCloseBundle(plugin: Plugin): Promise<void> {
Expand Down
131 changes: 119 additions & 12 deletions packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ describe('nitro', () => {

it('should build the server with prerender route "/" if nothing was provided', async () => {
// Arrange
const [buildSSRAppImportSpy, buildServerImportSpy, buildSitemapImportSpy] =
await mockBuildFunctions();
const {
buildSSRAppImportSpy,
buildServerImportSpy,
buildSitemapImportSpy,
} = await mockBuildFunctions();
const plugin = nitro({
ssr: true,
});
Expand All @@ -59,8 +62,11 @@ describe('nitro', () => {

it('should build the server with prerender route "/" even if ssr is false', async () => {
// Arrange
const [buildSSRAppImportSpy, buildServerImportSpy, buildSitemapImportSpy] =
await mockBuildFunctions();
const {
buildSSRAppImportSpy,
buildServerImportSpy,
buildSitemapImportSpy,
} = await mockBuildFunctions();
const plugin = nitro({
ssr: false,
});
Expand All @@ -84,12 +90,15 @@ describe('nitro', () => {

it('should build the server without prerender route when an empty array was passed', async () => {
// Arrange
const [buildSSRAppImportSpy, buildServerImportSpy, buildSitemapImportSpy] =
await mockBuildFunctions();
const {
buildSSRAppImportSpy,
buildServerImportSpy,
buildSitemapImportSpy,
} = await mockBuildFunctions();
const prerenderRoutes = {
prerender: {
routes: [],
sitemap: { domain: 'example.com' },
sitemap: { host: 'example.com' },
},
};
const plugin = nitro({
Expand All @@ -115,19 +124,22 @@ describe('nitro', () => {
);
expect(buildSitemapImportSpy).toHaveBeenCalledWith(
{},
{ domain: 'example.com' },
{ host: 'example.com' },
prerenderRoutes.prerender.routes
);
});

it('should build the server with provided routes', async () => {
// Arrange
const [buildSSRAppImportSpy, buildServerImportSpy, buildSitemapImportSpy] =
await mockBuildFunctions();
const {
buildSSRAppImportSpy,
buildServerImportSpy,
buildSitemapImportSpy,
} = await mockBuildFunctions();
const prerenderRoutes = {
prerender: {
routes: ['/blog', '/about'],
sitemap: { domain: 'example.com' },
sitemap: { host: 'example.com' },
},
};
const plugin = nitro({
Expand Down Expand Up @@ -158,8 +170,103 @@ describe('nitro', () => {

expect(buildSitemapImportSpy).toHaveBeenCalledWith(
{},
{ domain: 'example.com' },
{ host: 'example.com' },
prerenderRoutes.prerender.routes
);
});

describe('preset output', () => {
it('should use the analog output paths when preset is not vercel', async () => {
// Arrange
vi.mock('process');
process.cwd = vi.fn().mockReturnValue('/custom-root-directory');
const { buildServerImportSpy } = await mockBuildFunctions();

const plugin = nitro({}, {});

// Act
await runConfigAndCloseBundle(plugin);

// Assert
expect(buildServerImportSpy).toHaveBeenCalledWith(
{},
expect.objectContaining({
output: {
dir: '/custom-root-directory/dist/analog',
publicDir: '/custom-root-directory/dist/analog/public',
},
})
);
});

it('should use the .vercel output paths when preset is vercel', async () => {
// Arrange
vi.mock('process');
process.cwd = vi.fn().mockReturnValue('/custom-root-directory');
const { buildServerImportSpy } = await mockBuildFunctions();

const plugin = nitro({}, { preset: 'vercel' });

// Act
await runConfigAndCloseBundle(plugin);

// Assert
expect(buildServerImportSpy).toHaveBeenCalledWith(
{},
expect.objectContaining({
output: {
dir: '/custom-root-directory/.vercel/output',
publicDir: '/custom-root-directory/.vercel/output/static',
},
})
);
});

it('should use the .vercel output paths when preset is vercel-edge', async () => {
// Arrange
vi.mock('process');
process.cwd = vi.fn().mockReturnValue('/custom-root-directory');
const { buildServerImportSpy } = await mockBuildFunctions();

const plugin = nitro({}, { preset: 'vercel-edge' });

// Act
await runConfigAndCloseBundle(plugin);

// Assert
expect(buildServerImportSpy).toHaveBeenCalledWith(
{},
expect.objectContaining({
output: {
dir: '/custom-root-directory/.vercel/output',
publicDir: '/custom-root-directory/.vercel/output/static',
},
})
);
});

it('should use the .vercel output paths when preset is VERCEL environment variable is set', async () => {
// Arrange
vi.stubEnv('VERCEL', '1');
vi.mock('process');
process.cwd = vi.fn().mockReturnValue('/custom-root-directory');
const { buildServerImportSpy } = await mockBuildFunctions();

const plugin = nitro({}, {});

// Act
await runConfigAndCloseBundle(plugin);

// Assert
expect(buildServerImportSpy).toHaveBeenCalledWith(
{},
expect.objectContaining({
output: {
dir: '/custom-root-directory/.vercel/output',
publicDir: '/custom-root-directory/.vercel/output/static',
},
})
);
});
});
});
28 changes: 27 additions & 1 deletion packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin {
ssrBuild = _config.build?.ssr === true;
config = _config;
const rootDir = config.root || '.';
const buildPreset =
process.env['BUILD_PRESET'] ??
(nitroOptions?.preset as string | undefined);

let pageHandlers = getPageHandlers({ workspaceRoot, rootDir });
const pageHandlers = getPageHandlers({ workspaceRoot, rootDir });

nitroConfig = {
rootDir,
preset: buildPreset,
logLevel: nitroOptions?.logLevel || 0,
srcDir: normalizePath(`${rootDir}/src/server`),
scanDirs: [normalizePath(`${rootDir}/src/server`)],
Expand Down Expand Up @@ -77,6 +81,10 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin {
],
};

if (isVercelPreset(buildPreset)) {
nitroConfig = withVercelOutputAPI(nitroConfig, workspaceRoot);
}

if (!ssrBuild && !isTest) {
// store the client output path for the SSR build config
clientOutputPath = path.resolve(rootDir, config.build?.outDir!);
Expand Down Expand Up @@ -192,3 +200,21 @@ function isEmptyPrerenderRoutes(options?: Options): boolean {
function isArrayWithElements<T>(arr: unknown): arr is [T, ...T[]] {
return !!(Array.isArray(arr) && arr.length);
}

const isVercelPreset = (buildPreset: string | undefined) =>
process.env['VERCEL'] ||
(buildPreset && buildPreset.toLowerCase().includes('vercel'));

const withVercelOutputAPI = (
nitroConfig: NitroConfig | undefined,
workspaceRoot: string
) => ({
...nitroConfig,
output: {
...nitroConfig?.output,
dir: normalizePath(path.resolve(workspaceRoot, '.vercel', 'output')),
publicDir: normalizePath(
path.resolve(workspaceRoot, '.vercel', 'output/static')
),
},
});