Skip to content

Commit 307013e

Browse files
committed
Inline DEV_CHROME_EXTENSION_ID at build time via Vite define instead of loading dotenv in websocket server process
- the `apps/editor/src/services/websocket-server-process.ts` should not rely on process env. instead use __X__ convention for placeholder, and replace it with .env-psased value at build time └── apps └── editor ├── .env-sample ├── scripts │ └── build.mjs ├── src │ └── services │ ├── websocket-manager.ts │ └── websocket-server-process.ts └── vite.config.ts
1 parent 9f94ab5 commit 307013e

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎apps/editor/src/services/websocket-server-process.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as crypto from 'crypto'
22
import * as http from 'http'
3-
import * as path from 'path'
43
import * as process from 'process'
5-
import * as dotenv from 'dotenv'
64
const WebSocket = require('ws')
75
import { DEFAULT_PORT, CLIENT_TYPE } from '@shared/constants/websocket'
86

9-
dotenv.config({ path: path.join(__dirname, '..', '.env') })
7+
declare const __DEV_CHROME_EXTENSION_ID__: string
108

119
interface BrowserClient {
1210
ws: WebSocket
@@ -51,9 +49,8 @@ class WebSocketServer {
5149
}
5250

5351
if (
54-
process.env.DEV_CHROME_EXTENSION_ID &&
55-
origin ==
56-
`chrome-extension://${process.env.DEV_CHROME_EXTENSION_ID}`
52+
__DEV_CHROME_EXTENSION_ID__ &&
53+
origin == `chrome-extension://${__DEV_CHROME_EXTENSION_ID__}`
5754
) {
5855
return cb(true)
5956
}

‎apps/editor/vite.config.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as crypto from 'crypto'
22
import * as fs from 'fs'
33
import * as path from 'path'
4+
import * as dotenv from 'dotenv'
45
import { defineConfig, type Plugin, type UserConfig } from 'vite'
56

67
const node_targets: Record<string, string> = {
@@ -15,6 +16,19 @@ const view_targets: Record<string, string> = {
1516
}
1617

1718
const root_dir = __dirname
19+
20+
const get_env_placeholders = (name: string): Record<string, string> => {
21+
if (name != 'websocket-server-process') return {}
22+
const env_path = path.resolve(root_dir, '.env')
23+
const parsed = fs.existsSync(env_path)
24+
? dotenv.parse(fs.readFileSync(env_path))
25+
: {}
26+
const value =
27+
parsed.DEV_CHROME_EXTENSION_ID ?? process.env.DEV_CHROME_EXTENSION_ID ?? ''
28+
return {
29+
__DEV_CHROME_EXTENSION_ID__: JSON.stringify(value)
30+
}
31+
}
1832
const src_dir = path.resolve(root_dir, 'src')
1933
const shared_dir = path.resolve(root_dir, '../../packages/shared/src')
2034
const ui_dir = path.resolve(root_dir, '../../packages/ui/src')
@@ -127,7 +141,8 @@ const create_node_config = (
127141
'process.env.NODE_ENV': JSON.stringify(
128142
is_production ? 'production' : 'development'
129143
),
130-
'require.resolve': 'undefined'
144+
'require.resolve': 'undefined',
145+
...get_env_placeholders(name)
131146
},
132147
resolve: {
133148
alias: {

0 commit comments

Comments
 (0)