Repository navigation
feat(vite-plugin): add build.target to deploy the server build to a function, edge function or server #83
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
Open
hrishikesh-k
wants to merge
10
commits into
main
Choose a base branch
from
hk/vite-server
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
72c739e
initial attempt at migration
hrishikesh-k 67e598f
add missing files
hrishikesh-k f02b020
changes from self code-review
hrishikesh-k 41489ec
coderabbit changes
hrishikesh-k 429cadf
drop edgeSSR
hrishikesh-k 2bcb83d
add footgun documentation
hrishikesh-k 8adf847
Merge branch 'main' into hk/vite-server
hrishikesh-k fd274e1
add table test
hrishikesh-k acf7fbf
restore deleted option
hrishikesh-k d4c3f44
drop includedFiles
hrishikesh-k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' | ||
| import { tmpdir } from 'node:os' | ||
| import { dirname, join } from 'node:path' | ||
| import { pathToFileURL } from 'node:url' | ||
|
|
||
| import { build } from 'vite' | ||
| import { afterEach, describe, expect, test } from 'vitest' | ||
|
|
||
| import { createBuildPlugin, type BuildPluginOptions } from './build.js' | ||
|
|
||
| const cleanupJobs: (() => Promise<void>)[] = [] | ||
|
|
||
| afterEach(async () => { | ||
| await Promise.all(cleanupJobs.splice(0).map((job) => job())) | ||
| }) | ||
|
|
||
| const createProject = async () => { | ||
| const root = await mkdtemp(join(tmpdir(), 'vite-plugin-build-')) | ||
| cleanupJobs.push(() => rm(root, { force: true, recursive: true })) | ||
|
|
||
| await writeFile( | ||
| join(root, 'server.js'), | ||
| `export default { fetch: (request) => new Response("hello from " + new URL(request.url).pathname) }`, | ||
| ) | ||
| await mkdir(join(root, 'public', 'fonts'), { recursive: true }) | ||
| await writeFile(join(root, 'public', 'favicon.ico'), '') | ||
|
|
||
| return root | ||
| } | ||
|
|
||
| const buildProject = async (root: string, options: BuildPluginOptions, base?: string) => { | ||
| await build({ | ||
| root, | ||
| base, | ||
| logLevel: 'silent', | ||
| build: { ssr: 'server.js', outDir: 'dist/server' }, | ||
| environments: { client: { build: { outDir: 'dist/client' } } }, | ||
| plugins: [createBuildPlugin(options)], | ||
| }) | ||
| } | ||
|
|
||
| const writeClientBuild = async (root: string, files: string[]) => { | ||
| for (const file of files) { | ||
| await mkdir(dirname(join(root, 'dist/client', file)), { recursive: true }) | ||
| await writeFile(join(root, 'dist/client', file), '') | ||
| } | ||
| } | ||
|
|
||
| const readExcludedPath = async (root: string) => { | ||
| const contents = await readFile(join(root, '.netlify/v1/edge-functions/server.mjs'), 'utf8') | ||
|
|
||
| return JSON.parse(/excludedPath: (.*),/.exec(contents)?.[1] ?? 'null') as unknown | ||
| } | ||
|
|
||
| type Handler = (request: Request) => Promise<Response> | ||
|
|
||
| const importModule = async <T>(path: string) => (await import(pathToFileURL(path).href)) as T | ||
|
|
||
| const exists = (path: string) => | ||
| readFile(path).then( | ||
| () => true, | ||
| () => false, | ||
| ) | ||
|
|
||
| describe('build targets', () => { | ||
| test('writes a function that serves the SSR entry', async () => { | ||
| const root = await createProject() | ||
| await buildProject(root, {}) | ||
|
|
||
| const file = join(root, '.netlify/v1/functions/server.mjs') | ||
| const { default: handler, config } = await importModule<{ default: Handler; config: unknown }>(file) | ||
|
|
||
| expect(config).toMatchObject({ path: '/*', excludedPath: ['/.netlify/*'], preferStatic: true }) | ||
| expect(await (await handler(new Request('http://localhost/a'))).text()).toBe('hello from /a') | ||
| }) | ||
|
|
||
| test('writes an edge function that leaves every file in the client build to the CDN', async () => { | ||
| const root = await createProject() | ||
| await writeClientBuild(root, ['index.html', 'assets/app.js', 'about/index.html', 'docs.html', 'favicon.ico']) | ||
| await buildProject(root, { target: 'edge' }) | ||
|
|
||
| const contents = await readFile(join(root, '.netlify/v1/edge-functions/server.mjs'), 'utf8') | ||
|
|
||
| expect(contents).toContain('path: "/*"') | ||
| expect(contents).toContain('cache: "manual"') | ||
| expect(await readExcludedPath(root)).toEqual([ | ||
| '/.netlify/*', | ||
| '/', | ||
| '/about', | ||
| '/about/', | ||
| '/about/index.html', | ||
| '/assets/app.js', | ||
| '/docs', | ||
| '/docs.html', | ||
| '/favicon.ico', | ||
| '/index.html', | ||
| ]) | ||
| }) | ||
|
|
||
| // https://vite.dev/config/shared-options#base | ||
| test.for([ | ||
| { base: '', prefix: '/' }, | ||
| { base: './', prefix: '/' }, | ||
| { base: '/', prefix: '/' }, | ||
| { base: '/foo', prefix: '/foo/' }, | ||
| { base: '/foo/', prefix: '/foo/' }, | ||
| { base: 'https://example.com/foo', prefix: '/foo/' }, | ||
| { base: 'https://example.com/foo/', prefix: '/foo/' }, | ||
| ])("prefixes the edge function's excluded paths with the base $base", async ({ base, prefix }) => { | ||
| const root = await createProject() | ||
| await writeClientBuild(root, ['index.html', 'bar/index.html', 'docs.html', 'assets/app.js']) | ||
| await buildProject(root, { target: 'edge' }, base) | ||
|
|
||
| const staticPaths = [ | ||
| prefix, | ||
| `${prefix}assets/app.js`, | ||
| `${prefix}bar`, | ||
| `${prefix}bar/`, | ||
| `${prefix}bar/index.html`, | ||
| `${prefix}docs`, | ||
| `${prefix}docs.html`, | ||
| `${prefix}index.html`, | ||
| // The root's path without the trailing slash, which only exists under a base. | ||
| ...(prefix === '/' ? [] : [prefix.slice(0, -1)]), | ||
| ] | ||
|
|
||
| expect(await readExcludedPath(root)).toEqual(['/.netlify/*', ...staticPaths.sort()]) | ||
| }) | ||
|
|
||
| test('falls back to the assets and public directories before the client build has run', async () => { | ||
| const root = await createProject() | ||
| await mkdir(join(root, 'public/about'), { recursive: true }) | ||
| await writeFile(join(root, 'public/about/index.html'), '') | ||
| await writeFile(join(root, 'public/docs.html'), '') | ||
| await buildProject(root, { target: 'edge' }) | ||
|
|
||
| expect(await readExcludedPath(root)).toEqual([ | ||
| '/.netlify/*', | ||
| '/about', | ||
| '/about/', | ||
| '/about/index.html', | ||
| '/assets/*', | ||
| '/docs', | ||
| '/docs.html', | ||
| '/favicon.ico', | ||
| ]) | ||
| }) | ||
|
|
||
| test('writes a server that serves the SSR entry', async () => { | ||
| const root = await createProject() | ||
| await buildProject(root, { target: 'server' }) | ||
|
|
||
| const file = join(root, '.netlify/v1/server/index.mjs') | ||
| const { default: server } = await importModule<{ default: { fetch: Handler } }>(file) | ||
| expect(await (await server.fetch(new Request('http://localhost/b'))).text()).toBe('hello from /b') | ||
| }) | ||
|
|
||
| test('removes a function generated by an earlier version of the plugin', async () => { | ||
| const root = await createProject() | ||
| await mkdir(join(root, '.netlify/v1/functions'), { recursive: true }) | ||
| await writeFile( | ||
| join(root, '.netlify/v1/functions/server.mjs'), | ||
| 'export default () => {}\nexport const config = { generator: "@netlify/vite-plugin@3.0.1", path: "/*" };', | ||
| ) | ||
|
|
||
| await buildProject(root, { target: 'server' }) | ||
|
|
||
| expect(await exists(join(root, '.netlify/v1/functions/server.mjs'))).toBe(false) | ||
| }) | ||
|
|
||
| test('removes the files generated for the previous target', async () => { | ||
| const root = await createProject() | ||
| await buildProject(root, { target: 'function' }) | ||
| await buildProject(root, { target: 'server' }) | ||
|
|
||
| expect(await exists(join(root, '.netlify/v1/functions/server.mjs'))).toBe(false) | ||
| expect(await exists(join(root, '.netlify/v1/server/index.mjs'))).toBe(true) | ||
| }) | ||
|
|
||
| test('fails when the project already has its own server', async () => { | ||
| const root = await createProject() | ||
| await mkdir(join(root, 'netlify/server'), { recursive: true }) | ||
| await writeFile(join(root, 'netlify/server/index.ts'), 'export default {}') | ||
|
|
||
| await expect(buildProject(root, { target: 'server' })).rejects.toThrow( | ||
| "build.target is set to 'server', but this project already has a Netlify Server in netlify/server/index.ts", | ||
| ) | ||
| expect(await exists(join(root, '.netlify/v1/server/index.mjs'))).toBe(false) | ||
|
|
||
| await buildProject(root, { target: 'function' }) | ||
| }) | ||
|
|
||
| test('fails when another tool already wrote a server', async () => { | ||
| const root = await createProject() | ||
| await mkdir(join(root, '.netlify/v1/server'), { recursive: true }) | ||
| await writeFile(join(root, '.netlify/v1/server/index.js'), 'export default {}') | ||
|
|
||
| await expect(buildProject(root, { target: 'server' })).rejects.toThrow( | ||
| 'already has a Netlify Server in .netlify/v1/server/index.js', | ||
| ) | ||
| }) | ||
|
|
||
| test('replaces a server it generated in an earlier build', async () => { | ||
| const root = await createProject() | ||
| await buildProject(root, { target: 'server' }) | ||
| await buildProject(root, { target: 'server' }) | ||
|
|
||
| expect(await exists(join(root, '.netlify/v1/server/index.mjs'))).toBe(true) | ||
| }) | ||
|
|
||
| test('keeps files at those paths that the plugin did not generate', async () => { | ||
| const root = await createProject() | ||
| await mkdir(join(root, '.netlify/v1/server'), { recursive: true }) | ||
| await writeFile(join(root, '.netlify/v1/server/index.mjs'), 'export default {}') | ||
|
|
||
| await buildProject(root, { target: 'function' }) | ||
|
|
||
| expect(await exists(join(root, '.netlify/v1/server/index.mjs'))).toBe(true) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.