Skip to content

Commit

Permalink
Feature/fix api routes (onejs#16)
Browse files Browse the repository at this point in the history
* feat(scripts): Update setup-zero script to use GitHub repository

- Refactored setup-zero.ts to pull and link zero from a GitHub repository instead of a local install
- Tidied up various scripts for improved organization and maintainability

Implementation reason: This change enables builds to work in CI environments by ensuring consistent access to the zero dependency across different build contexts. It also improves the project's scalability and reduces potential issues related to local environment discrepancies.

Files changed:
- scripts/setup-zero.ts
- scripts/build.ts
- scripts/dev.ts
- scripts/watch.ts

* fix(api): fix prod build of api routes

* Revert "feat(scripts): Update setup-zero script to use GitHub repository"

This reverts commit e83519008b5593d4bdd7f316d43b885a779d00a7.

* fix(dx): remove console.info

* feature(ci): amend tests

* feature(ci): trigger workflow
  • Loading branch information
jonsherrard authored Sep 25, 2024
1 parent f2320a9 commit 5f4bf5c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ name: Checks and Tests

on:
push:
branches: [main] # Adjust this to your main branch name
pull_request:
branches: [main] # Adjust this to your main branch name

jobs:
checks:
Expand Down
22 changes: 15 additions & 7 deletions packages/vxs/src/vite/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function build(args: {

build: {
emptyOutDir: false,
outDir: 'dist/api',
outDir: 'dist',
copyPublicDir: false,
minify: false,
rollupOptions: {
Expand All @@ -123,16 +123,23 @@ export async function build(args: {
}
: {
format: 'cjs',
// Use a function to strip the extension
// Preserve folder structure and use .cjs extension
entryFileNames: (chunkInfo) => {
const name = basename(chunkInfo.name, extname(chunkInfo.name))
return `${name}.cjs`
const name = chunkInfo.name.replace(/\.js$/, '.cjs')
return name
},
chunkFileNames: (chunkInfo) => {
const name = basename(chunkInfo.name, extname(chunkInfo.name))
return `${name}-[hash].cjs`
const dir = Path.dirname(chunkInfo.name)
const name = Path.basename(chunkInfo.name, Path.extname(chunkInfo.name))
return Path.join(dir, `${name}-[hash].cjs`)
},
assetFileNames: (assetInfo) => {
const name = assetInfo.name ?? ''
const dir = Path.dirname(name)
const baseName = Path.basename(name, Path.extname(name))
const ext = Path.extname(name)
return Path.join(dir, `${baseName}-[hash]${ext}`)
},
assetFileNames: '[name]-[hash][extname]',
}),
},
},
Expand All @@ -151,6 +158,7 @@ export async function build(args: {
console.info(`\n 🔨 build static routes\n`)

let render: RenderApp | null = null
// @ts-expect-error This works
const entryServer = vxrnOutput.serverEntry

try {
Expand Down
3 changes: 1 addition & 2 deletions packages/vxs/test/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
import * as path from 'node:path'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'

Expand All @@ -13,7 +13,6 @@ const runTests = (environment: 'dev' | 'prod') => {
// Spawn the server process based on the environment
const command = environment === 'dev' ? 'dev' : 'prod'
serverProcess = spawn('yarn', [command, '--host', 'localhost'], { cwd: fixturePath })

let serverOutput = ''
serverProcess.stdout.on('data', (data) => {
console.info(data.toString())
Expand Down
16 changes: 14 additions & 2 deletions packages/vxs/test/build.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FSExtra from 'fs-extra'
import { readFile, pathExists } from 'fs-extra'
import { spawnSync } from 'node:child_process'
import * as path from 'node:path'
import { beforeAll, describe, expect, it } from 'vitest'
Expand All @@ -11,7 +11,7 @@ describe('Simple Build Tests', () => {
})

it('should build api routes without including side effects', async () => {
const sideEffectFreeApiRoute = await FSExtra.readFile(
const sideEffectFreeApiRoute = await readFile(
path.join(fixturePath, 'dist', 'api', 'react-dep.cjs')
)

Expand All @@ -20,4 +20,16 @@ describe('Simple Build Tests', () => {
// if sideEffects is not set in package.json of vxs it will include all of react etc
expect(!sideEffectFreeApiRoute.includes('useEffect')).toBeTruthy()
})

it('should generate the dynamic endpoint file', async () => {
const dynamicEndpointPath = path.join(
fixturePath,
'dist',
'api',
'test-params',
'_endpointId_.cjs'
)
const fileExists = await pathExists(dynamicEndpointPath)
expect(fileExists).toBeTruthy()
})
})

0 comments on commit 5f4bf5c

Please sign in to comment.