Skip to content

Commit

Permalink
Merge pull request #45 from fastify/dev
Browse files Browse the repository at this point in the history
v2.3.0
  • Loading branch information
galvez authored Nov 20, 2021
2 parents d466f8e + 49ea4b4 commit f1e9b7a
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 36 deletions.
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- Fix code splitting for routes
- Revisit data fetching idioms, maybe provide `usePayload` and `useRouteData`

- React
- Upgrade to React Router v6

- Documentation
- Implement select box and click-to-copy like in https://stripe.com/docs/js/initializing
- Add Undici fetch() examples
Expand Down
17 changes: 8 additions & 9 deletions devinstall.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { readdirSync, existsSync, writeFile, readFile } from 'fs/promises'
import { join } from 'path'
import { dirname } from 'desm'

import { fileURLToPath } from 'url'

$.quote = s => s

const { entries } = Object

let root = dirname(import.meta.url)
let root = fileURLToPath(path.dirname(import.meta.url))
let [example, exRoot, deps] = await parseArgv(root)

let pkgInfos = {}
Expand Down Expand Up @@ -37,7 +36,7 @@ for (let pkg of deps.local) {

async function getDeps (example, exRoot) {
const examplePackage = await readJSON(`${root}/${exRoot}/package.dist.json`)
const pkgInfo = readdirSync(join(root, 'packages'))
const pkgInfo = await fs.readdir(path.join(root, 'packages'))
return {
local: Object.keys(examplePackage.dependencies).filter(dep => pkgInfo.includes(dep)),
external: Object.keys(examplePackage.dependencies).filter(dep => !pkgInfo.includes(dep))
Expand All @@ -47,23 +46,23 @@ async function getDeps (example, exRoot) {
async function parseArgv () {
const example = process.argv[3]
const exRoot = `examples/${example}`
const deps = await getDeps(join(exRoot, 'package.dist.json'), exRoot)
const deps = await getDeps(path.join(exRoot, 'package.dist.json'), exRoot)
if (!example) {
console.error('Usage: npm run devinstall -- <dir>')
process.exit(1)
}
if (!existsSync(join(root, exRoot))) {
if (!await fs.stat(path.join(root, exRoot)).catch(() => false)) {
console.error(`Directory ${join(root, exRoot)} does not exist.`)
process.exit(1)
}
return [example, exRoot, deps]
}

async function readJSON(path) {
const json = await readFile(path, 'utf8')
const json = await fs.readFile(path, 'utf8')
return JSON.parse(json)
}

async function writeJSON(path, contents) {
await writeFile(path, contents)
await fs.writeFile(path, contents)
}
4 changes: 2 additions & 2 deletions examples/react-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"fastify": "^3.17.0",
"fastify-vite": "^2.2.4",
"fastify-vite-react": "^2.2.4"
"fastify-vite": "^2.3.0",
"fastify-vite-react": "^2.3.0"
}
}
19 changes: 10 additions & 9 deletions examples/react-base/server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const fastify = require('fastify')()
const fastifyVite = require('fastify-vite')
const fastifyViteReact = require('fastify-vite-react')
const Fastify = require('fastify')
const FastifyVite = require('fastify-vite')
const FastifyViteReact = require('fastify-vite-react')

async function main () {
await fastify.register(fastifyVite, {
const app = Fastify()
await app.register(FastifyVite, {
root: __dirname,
renderer: fastifyViteReact,
renderer: FastifyViteReact,
})
await fastify.vite.ready()
return fastify
await app.vite.commands()
return app
}

if (require.main === module) {
main().then((fastify) => {
fastify.listen(3000, (err, address) => {
main().then((app) => {
app.listen(3000, (err, address) => {
if (err) {
console.error(err)
process.exit(1)
Expand Down
4 changes: 2 additions & 2 deletions examples/react-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"fastify": "^3.17.0",
"fastify-api": "^0.2.0",
"fastify-vite": "^2.2.4",
"fastify-vite-react": "^2.2.4"
"fastify-vite": "^2.3.0",
"fastify-vite-react": "^2.3.0"
}
}
2 changes: 1 addition & 1 deletion examples/react-data/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function main () {

app.vite.global = { foobar: 123 }

await app.vite.ready()
await app.vite.commands()
return app
}

Expand Down
4 changes: 2 additions & 2 deletions examples/vue-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"fastify": "^3.17.0",
"fastify-vite": "^2.2.4",
"fastify-vite-vue": "^2.2.4"
"fastify-vite": "^2.3.0",
"fastify-vite-vue": "^2.3.0"
}
}
2 changes: 1 addition & 1 deletion examples/vue-base/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function main () {
const root = import.meta.url

await app.register(FastifyVite, { root, renderer })
await app.vite.ready()
await app.vite.commands()

return app
}
Expand Down
4 changes: 2 additions & 2 deletions examples/vue-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"fastify": "^3.17.0",
"fastify-api": "^0.2.0",
"fastify-vite": "^2.2.4",
"fastify-vite-vue": "^2.2.4"
"fastify-vite": "^2.3.0",
"fastify-vite-vue": "^2.3.0"
}
}
2 changes: 1 addition & 1 deletion examples/vue-data/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function main () {

app.vite.global = { foobar: 123 }

await app.vite.ready()
await app.vite.commands()
return app
}

Expand Down
2 changes: 1 addition & 1 deletion packages/fastify-vite-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"type": "git",
"url": "git+https://github.com/terixjs/fastify-vite.git"
},
"version": "2.2.4",
"version": "2.3.0",
"dependencies": {
"@vitejs/plugin-react-refresh": "1.3.1",
"devalue": "2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/fastify-vite-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"type": "git",
"url": "git+https://github.com/terixjs/fastify-vite.git"
},
"version": "2.2.4",
"version": "2.3.0",
"dependencies": {
"@vitejs/plugin-vue": "1.9.3",
"@vitejs/plugin-vue-jsx": "1.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/fastify-vite/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function flattenPaths (viewPath) {
return [viewPath]
}
if (Array.isArray(viewPath)) {
return viewPath;
return viewPath
}
return []
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fastify-vite/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function build (options) {
await Promise.all([viteBuild(client), viteBuild(server)])
await move(join(serverOutDir, 'server.js'), join(serverOutDir, 'server.cjs'))
console.log(`Generated ${outDir}/client and ${outDir}/server.`)
process.exit(0);
process.exit(0)
}

module.exports = { build }
17 changes: 15 additions & 2 deletions packages/fastify-vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ const { generateRoute } = require('./static')
const { processOptions } = require('./options')

async function fastifyVite (fastify, options) {
let viteReady
const isViteReady = new Promise((resolve) => {
viteReady = resolve
})

fastify.decorate('vite', {
ready () {
return isViteReady
},
})

// Run options through Vite to get all Vite defaults taking vite.config.js
// into account and ensuring options.root and options.vite.root are the same
try {
Expand Down Expand Up @@ -99,9 +110,11 @@ async function fastifyVite (fastify, options) {
}
}

viteReady()

// Sets fastify.vite.get() helper which uses
// a wrapper for setting a route with a data() handler
fastify.decorate('vite', {
Object.assign(fastify.vite, {
handler,
options,
global: undefined,
Expand Down Expand Up @@ -186,7 +199,7 @@ async function fastifyVite (fastify, options) {
})
}

fastify.vite.ready = async () => {
fastify.vite.commands = async () => {
await fastify.ready()
if (fastify.vite.options.eject) {
const force = process.argv.includes('--force')
Expand Down
2 changes: 1 addition & 1 deletion packages/fastify-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"type": "git",
"url": "git+https://github.com/terixjs/fastify-vite.git"
},
"version": "2.2.4",
"version": "2.3.0",
"dependencies": {
"degit": "2.8.4",
"fastify": "^3.17.0",
Expand Down

0 comments on commit f1e9b7a

Please sign in to comment.