Skip to content

Commit 373c948

Browse files
committed
Add playground
1 parent 62ccded commit 373c948

16 files changed

+250
-44
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ coverage
5454
Network Trash Folder
5555
Temporary Items
5656
.apdisk
57+
58+
*.db

playground/app.vue

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<template>
22
<div>
3-
Nuxt module playground!
3+
<NuxtPage />
44
</div>
55
</template>
6-
7-
<script setup>
8-
</script>

playground/lib/prisma.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { PrismaClient } from '@prisma/client'
2+
3+
const prisma = new PrismaClient()
4+
5+
export {
6+
prisma
7+
}

playground/lib/todo.server.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { prisma } from './prisma'
2+
3+
export async function getTodos () {
4+
const todos = await prisma.todo.findMany()
5+
return todos
6+
}
7+
8+
export async function getTodo (id: number) {
9+
const todo = await prisma.todo.findFirstOrThrow({
10+
where: {
11+
id
12+
}
13+
})
14+
return todo
15+
}

playground/nuxt.config.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ import MyModule from '..'
44
export default defineNuxtConfig({
55
modules: [
66
MyModule
7-
],
8-
myModule: {
9-
addPlugin: true
10-
}
7+
]
118
})

playground/package.json

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
{
2-
"private": true,
3-
"name": "my-module-playground"
2+
"name": "playground",
3+
"devDependencies": {
4+
"esno": "^0.16.3",
5+
"prisma": "^4.6.1"
6+
},
7+
"prisma": {
8+
"seed": "esno prisma/seed.ts",
9+
"schema": "./prisma/schema.prisma"
10+
},
11+
"dependencies": {
12+
"@prisma/client": "^4.6.1"
13+
}
414
}

playground/pages/index.vue

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script setup lang="ts">
2+
import { getTodos } from '~~/lib/todo.server'
3+
4+
const todos = await getTodos()
5+
</script>
6+
7+
<template>
8+
<div>
9+
<ul>
10+
<li>
11+
<NuxtLink v-for="todo in todos" :key="todo.id" :to="`/todos/${todo.id}`">
12+
{{ todo.title }}
13+
</NuxtLink>
14+
</li>
15+
</ul>
16+
</div>
17+
</template>

playground/pages/todos/[id].vue

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script setup lang="ts">
2+
import { getTodo } from '~~/lib/todo.server'
3+
import { useRoute } from '#imports'
4+
5+
const route = useRoute()
6+
const todo = await getTodo(Number(route.params.id))
7+
</script>
8+
9+
<template>
10+
<div>
11+
Todo: {{ todo }}
12+
</div>
13+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- CreateTable
2+
CREATE TABLE "Todo" (
3+
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
4+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
5+
"updatedAt" DATETIME NOT NULL,
6+
"title" TEXT NOT NULL,
7+
"completed" BOOLEAN NOT NULL,
8+
"content" TEXT
9+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "sqlite"

playground/prisma/schema.prisma

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
generator client {
5+
provider = "prisma-client-js"
6+
}
7+
8+
datasource db {
9+
provider = "sqlite"
10+
url = env("DATABASE_URL")
11+
}
12+
13+
model Todo {
14+
id Int @id @default(autoincrement())
15+
createdAt DateTime @default(now())
16+
updatedAt DateTime @updatedAt
17+
title String
18+
completed Boolean
19+
content String?
20+
}

playground/prisma/seed.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @ts-ignore
2+
import { PrismaClient, Prisma } from '@prisma/client'
3+
4+
const prisma = new PrismaClient()
5+
6+
const todoData: Prisma.TodoCreateInput[] = [
7+
{ title: 'Milk', content: 'Buy milk', completed: false },
8+
{ title: 'Bananas', content: 'Buy bananas', completed: false }
9+
]
10+
11+
async function main() {
12+
console.log(`Start seeding ...`)
13+
for (const t of todoData) {
14+
const todo = await prisma.todo.create({
15+
data: t
16+
})
17+
console.log(`Created todo with id: ${todo.id}`)
18+
}
19+
console.log(`Seeding finished.`)
20+
}
21+
22+
main()
23+
.catch((e) => {
24+
console.error(e)
25+
process.exit(1)
26+
})
27+
.finally(async () => {
28+
await prisma.$disconnect()
29+
})

pnpm-lock.yaml

+108-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
packages:
2+
- playground

src/module.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { addImports, addImportsDir, addServerHandler, addTemplate, addVitePlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
21
import { fileURLToPath } from 'url'
2+
import { addImportsDir, addServerHandler, addTemplate, addVitePlugin, defineNuxtModule } from '@nuxt/kit'
33
import fg from 'fast-glob'
44
import { join } from 'pathe'
55
import { transformRemoteFunctions } from '../dist/runtime/plugin'
@@ -10,8 +10,6 @@ export default defineNuxtModule({
1010
configKey: 'remote'
1111
},
1212
async setup (options, nuxt) {
13-
const { resolve } = createResolver(import.meta.url)
14-
1513
const extGlob = '**/*.server.{ts,js}'
1614

1715
const files: string[] = []
@@ -20,7 +18,7 @@ export default defineNuxtModule({
2018

2119
// Transpile runtime
2220
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
23-
nuxt.options.build.transpile.push(runtimeDir)
21+
nuxt.options.build.transpile.push(runtimeDir, join(runtimeDir, 'composables'))
2422

2523
addServerHandler({
2624
route: '/api/__server_fn__',

src/runtime/plugin.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@ async function transformExports (src: string) {
2828

2929
const [, exports] = parse(src)
3030

31-
return exports.map((e) => {
31+
const exportList = exports.map((e) => {
3232
if (e.n === 'default') {
3333
throw new Error('Default exports are not allowed!')
3434
}
3535

36-
return `export const ${e.n} = (...args) => useServerFunctions().${e.n}(...args)`
37-
}).join('\n')
36+
return `export const ${e.n} = (...args) => serverFn.${e.n}(...args)`
37+
})
38+
39+
return `
40+
import { useServerFunctions } from '#imports'
41+
const serverFn = useServerFunctions()
42+
${exportList.join('\n')}
43+
`
3844
}

0 commit comments

Comments
 (0)