Skip to content

Commit

Permalink
Support node json module using script during node build (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
byn9826 authored Sep 23, 2024
1 parent 13eb011 commit ed93771
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 22 deletions.
12 changes: 1 addition & 11 deletions docs/auth-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,7 @@ npm run node:dev
```

### 3. Production Build
To prepare for production, follow these steps:
1. Update server/src/routes/other.tsx file
```
# Comment out the current swagger.json import statement:
// import swaggerSpec from '../scripts/swagger.json';
# Uncomment the other swagger.json import statement which contains with { type: "json" }:
import swaggerSpec from '../scripts/swagger.json' with { type: "json" }
```

2. Run the following commands to build and start the server:
Run the following commands to build and start the server:
```
cd server
npm run node:build
Expand Down
124 changes: 117 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "melody-auth",
"version": "1.0.4",
"version": "1.0.5",
"description": "A turnkey OAuth & authentication system.",
"license": "MIT",
"author": "Baozier",
Expand Down
5 changes: 3 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dev:secret:generate": "node ./src/scripts/generate-secret.cjs",
"dev:secret:clean": "node ./src/scripts/clean-secret.cjs",
"node:dev": "tsx watch src/node.tsx",
"node:build": "tsc && tsc-alias",
"node:build": "rimraf dist && tsc && tsc-alias && node ./src/scripts/node-build-patch.cjs",
"node:start": "node dist/node.js",
"node:migration:apply": "knex --knexfile knexfile.cjs migrate:latest",
"node:secret:generate": "node ./src/scripts/generate-secret.cjs node",
Expand Down Expand Up @@ -48,15 +48,16 @@
"@types/jsdom": "^21.1.7",
"@types/jwk-to-pem": "^2.0.3",
"@types/node-jose": "^1.1.13",
"@types/nodemailer": "^6.4.15",
"@vitest/coverage-v8": "^2.0.5",
"better-sqlite3": "^11.2.1",
"ioredis-mock": "^8.9.0",
"jsdom": "^25.0.0",
"node-jose": "^2.2.0",
"otplib": "^12.0.1",
"pg-mem": "^3.0.2",
"rimraf": "^6.0.1",
"swagger-jsdoc": "^6.2.8",
"@types/nodemailer": "^6.4.15",
"tsc-alias": "^1.8.10",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
Expand Down
1 change: 0 additions & 1 deletion server/src/routes/other.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { swaggerUI } from '@hono/swagger-ui'
import { Hono } from 'hono'
import swaggerSpec from '../scripts/swagger.json'
// import swaggerSpec from '../scripts/swagger.json' with { type: 'json' }
import { otherHandler } from 'handlers'
import { typeConfig } from 'configs'

Expand Down
42 changes: 42 additions & 0 deletions server/src/scripts/node-build-patch.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require('fs')
const path = require('path')

const filePath = path.join(
__dirname,
'../../dist/routes/other.js',
)

fs.readFile(
filePath,
'utf8',
(
err, data,
) => {
if (err) {
console.error(
'Error reading file:',
err,
)
return
}

const result = data.replace(
"import swaggerSpec from '../scripts/swagger.json'",
"import swaggerSpec from '../scripts/swagger.json' with { type: 'json' }",
)

fs.writeFile(
filePath,
result,
'utf8',
(err) => {
if (err) {
console.error(
'Error writing file:',
err,
)
}
},
)
},
)
1 change: 1 addition & 0 deletions server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"strict": true,
"skipLibCheck": true,
"lib": [
Expand Down

0 comments on commit ed93771

Please sign in to comment.