Skip to content

Commit

Permalink
can build and bundle yjs-server
Browse files Browse the repository at this point in the history
  • Loading branch information
hxhxhx88 committed Feb 27, 2024
1 parent 927b1e8 commit 2e7c50c
Show file tree
Hide file tree
Showing 7 changed files with 2,368 additions and 236 deletions.
4 changes: 3 additions & 1 deletion app/yjs-server/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
dist
bin
2,536 changes: 2,309 additions & 227 deletions app/yjs-server/package-lock.json

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions app/yjs-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
"version": "0.0.0",
"scripts": {
"dev": "nodemon src/index.ts",
"lint": "eslint src/"
"lint": "eslint src/",
"build": "cross-env TS_NODE_PROJECT=\"tsconfig.webpack.json\" webpack",
"bundle": "pkg --output bin/yjs-server dist/yjs-server.js"
},
"devDependencies": {
"@types/node": "^20.11.20",
"@types/webpack-node-externals": "^3.0.4",
"@types/ws": "^8.5.10",
"@vercel/style-guide": "^5.2.0",
"@yao-pkg/pkg": "^5.11.4",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0"
"tsconfig-paths": "^4.2.0",
"webpack": "^5.90.3",
"webpack-cli": "^5.1.4"
},
"dependencies": {
"bufferutil": "^4.0.8",
"nodemon": "^3.1.0",
"utf-8-validate": "^6.0.3",
"ws": "^8.16.0",
"y-websocket": "^1.5.4",
"yjs": "^13.6.12"
Expand Down
6 changes: 4 additions & 2 deletions app/yjs-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from "node:http";
import fs from "node:fs";
import * as Y from "yjs";
import { WebSocket } from "ws";
import { WebSocketServer } from "ws";
import { setupWSConnection, setPersistence } from "y-websocket/bin/utils";

const dataDir = process.env.DATA_DIR;
Expand All @@ -14,7 +14,9 @@ const server = http.createServer((_, response) => {
response.end("ok");
});

const wss = new WebSocket.Server({ noServer: true });
// Do not use `new WebSocket.Server` but `new WebSocketServer`. Otherwise, it will panic at runtime after bundling.
// https://github.com/websockets/ws/issues/1538#issuecomment-944859160
const wss = new WebSocketServer({ noServer: true });

const urlPtn = /^\/video\/(?<id>[0-9a-zA-Z]+)$/;
wss.on("connection", (ws, req) => {
Expand Down
6 changes: 2 additions & 4 deletions app/yjs-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
"target": "esnext",
"moduleResolution": "bundler",
"composite": false,
"declaration": true,
"declarationMap": true,
"declaration": false,
"declarationMap": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"allowImportingTsExtensions": true,
"noEmit": true,
"inlineSources": false,
"isolatedModules": true,
"noUnusedLocals": true,
Expand Down
8 changes: 8 additions & 0 deletions app/yjs-server/tsconfig.webpack.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"esModuleInterop": true
},
"include": ["webpack.config.ts"]
}
30 changes: 30 additions & 0 deletions app/yjs-server/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import path from "node:path";
import type { Configuration } from "webpack";
// import nodeExternals from "webpack-node-externals";

const config: Configuration = {
target: "node",
mode: "production",
entry: path.resolve(__dirname, "src", "index.ts"),
output: {
path: path.join(__dirname, "dist"),
filename: "yjs-server.js",
clean: true,
},
resolve: {
extensions: [".ts", ".js"],
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
exclude: /node_modules/,
},
],
},
// externals: [nodeExternals()],
};

// eslint-disable-next-line import/no-default-export -- fine
export default config;

0 comments on commit 2e7c50c

Please sign in to comment.