Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a7991b6

Browse files
committed
v2
1 parent 652bec7 commit a7991b6

File tree

17 files changed

+1152
-144
lines changed

17 files changed

+1152
-144
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 2.0.0
4+
### ⚠️ **Breaking Changes!** ⚠️
5+
- update vite to 3.x
6+
7+
- add apollo example
8+
39
## 1.0.0
410
- lazy load swc
511

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ This plugin leverages Vite SSR mode to build your app. All you need to do is add
224224
```
225225
For more build config please check [vite doc](https://vitejs.dev/config/#build-target)
226226

227+
> Note: By default, starting from vite v3, the ssr buildle will be in esm format. if you want to build cjs, add `ssr: { format: 'cjs' }` to your vite config.
228+
227229
## Examples
228230

229231
See the examples folder.

examples/apollo/app.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// npm install @apollo/server express graphql cors body-parser
2+
import http from 'http';
3+
import { ApolloServer } from '@apollo/server';
4+
import { expressMiddleware } from '@apollo/server/express4';
5+
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
6+
import express from 'express';
7+
import cors from 'cors';
8+
import bodyParser from 'body-parser';
9+
10+
interface MyContext {
11+
token?: String
12+
}
13+
// Construct a schema, using GraphQL schema language
14+
const typeDefs = `
15+
type Query {
16+
hello: String
17+
}
18+
`;
19+
20+
// Provide resolver functions for your schema fields
21+
const resolvers = {
22+
Query: {
23+
hello: () => 'Hello world!~!',
24+
},
25+
};
26+
27+
async function bootstrap() {
28+
// Required logic for integrating with Express
29+
const app = express();
30+
// Our httpServer handles incoming requests to our Express app.
31+
// Below, we tell Apollo Server to "drain" this httpServer,
32+
// enabling our servers to shut down gracefully.
33+
const httpServer = http.createServer(app);
34+
35+
// Same ApolloServer initialization as before, plus the drain plugin
36+
// for our httpServer.
37+
const server = new ApolloServer<MyContext>({
38+
typeDefs,
39+
resolvers,
40+
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
41+
});
42+
// Ensure we wait for our server to start
43+
await server.start();
44+
45+
// Set up our Express middleware to handle CORS, body parsing,
46+
// and our expressMiddleware function.
47+
app.use(
48+
'/',
49+
cors<cors.CorsRequest>(),
50+
bodyParser.json(),
51+
// expressMiddleware accepts the same arguments:
52+
// an Apollo Server instance and optional configuration options
53+
expressMiddleware(server, {
54+
context: async ({ req }) => ({ token: req.headers.token }),
55+
}),
56+
);
57+
58+
if (import.meta.env.PROD) {
59+
// Modified server startup
60+
httpServer.listen(3000);
61+
}
62+
63+
return app;
64+
}
65+
const app = bootstrap();
66+
export const viteNodeApp = app;

examples/apollo/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "apollo-example",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"dev": "vite",
8+
"dev-flag": "NODE_OPTIONS='--experimental-fetch' vite",
9+
"debug": "vite --debug --inspect-brk",
10+
"build": "vite build"
11+
},
12+
"dependencies": {
13+
"@apollo/server": "^4.3.0",
14+
"cors": "^2.8.5",
15+
"graphql": "^16.6.0"
16+
},
17+
"devDependencies": {
18+
"@types/cors": "^2.8.13",
19+
"vite": "^3",
20+
"vite-plugin-node": "workspace:*"
21+
}
22+
}

examples/apollo/vite.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from 'vite';
2+
import { VitePluginNode } from 'vite-plugin-node';
3+
4+
export default defineConfig({
5+
server: {
6+
port: 3699,
7+
},
8+
plugins: [
9+
...VitePluginNode({
10+
adapter: 'express',
11+
appPath: './app.ts',
12+
}),
13+
],
14+
});

examples/cloudfunction/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"devDependencies": {
1616
"@swc/core": "^1.2.155",
17-
"vite": "^2.8.6",
17+
"vite": "^3",
1818
"vite-plugin-node": "workspace:*"
1919
}
2020
}

examples/express/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"express": "^4.17.1"
1414
},
1515
"devDependencies": {
16-
"vite": "^2.9.0",
16+
"vite": "^3",
1717
"vite-plugin-node": "workspace:*"
1818
}
1919
}

examples/express/vite.config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import { defineConfig } from 'vite';
22
import { VitePluginNode } from 'vite-plugin-node';
33

44
export default defineConfig({
5+
ssr: {
6+
format: 'cjs',
7+
},
58
server: {
6-
port: 3699
9+
port: 3699,
710
},
811
plugins: [
912
...VitePluginNode({
1013
adapter: 'express',
11-
appPath: './app.ts'
12-
})
13-
]
14+
appPath: './app.ts',
15+
}),
16+
],
1417
});

examples/fastify/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"fastify": "^3.27.1"
1212
},
1313
"devDependencies": {
14-
"vite": "^2.9.0",
14+
"vite": "3",
1515
"vite-plugin-node": "workspace:*"
1616
}
1717
}

examples/koa/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"koa": "^2.13.3"
1212
},
1313
"devDependencies": {
14-
"vite": "^2.9.0",
14+
"vite": "3",
1515
"vite-plugin-node": "workspace:*"
1616
}
1717
}

0 commit comments

Comments
 (0)