Skip to content

Commit

Permalink
chore: small fixes
Browse files Browse the repository at this point in the history
- fixed nodemon restart on .env
- fixed basename
  • Loading branch information
denchiklut committed Oct 13, 2024
1 parent 52ccd16 commit 6f4d6b1
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 56 deletions.
18 changes: 6 additions & 12 deletions @types/env/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import type { Env } from "src/common"

declare global {
declare const IS_DEV: boolean
declare const IS_PROD: boolean
declare const IS_SERVER: boolean
declare const IS_SPA: boolean
declare const clientPrefix: string
declare var IS_DEV: boolean
declare var IS_PROD: boolean
declare var IS_SERVER: boolean
declare var IS_SPA: boolean
declare var clientPrefix: string

namespace NodeJS {
interface ProcessEnv extends Partial<Collection<keyof Env, string>> {}
}

interface Window {
IS_SPA: boolean
IS_SERVER: boolean
IS_DEV: boolean
IS_PROD: boolean

env_vars: Partial<Collection<keyof Env, string>>
clientPrefix: string
nonce: string
env_vars: Partial<Collection<keyof Env, string>>
}
}
8 changes: 4 additions & 4 deletions config/spec/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import '@testing-library/jest-dom'

window.IS_SERVER = false
window.IS_DEV = false
window.IS_SPA = true
window.clientPrefix = 'PUBLIC_'
globalThis.IS_SERVER = false
globalThis.IS_DEV = false
globalThis.IS_SPA = true
globalThis.clientPrefix = 'CLIENT_'

jest.mock('src/common/env/env.util', () => ({
...jest.requireActual('src/common/env/env.util'),
Expand Down
3 changes: 3 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"watch": ["dist/server", ".env"]
}
22 changes: 12 additions & 10 deletions src/client/components/@shared/app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type FC, lazy } from 'react'
import { type FC, lazy, StrictMode } from 'react'
import { Route, Routes } from 'react-router-dom'
import { type AppProps, getENV } from 'src/common'
import { Layout } from '@shared/layout'
Expand All @@ -13,14 +13,16 @@ export const App: FC<AppProps> = ({ nonce }) => {
__webpack_public_path__ = getENV('CLIENT_PUBLIC_PATH')

return (
<Html nonce={nonce}>
<Routes>
<Route path='/' element={<Layout />}>
<Route index element={<Home />} />
<Route path='about' element={<About />} />
<Route path='*' element={<NotFound />} />
</Route>
</Routes>
</Html>
<StrictMode>
<Html nonce={nonce}>
<Routes>
<Route path='/' element={<Layout />}>
<Route index element={<Home />} />
<Route path='about' element={<About />} />
<Route path='*' element={<NotFound />} />
</Route>
</Routes>
</Html>
</StrictMode>
)
}
3 changes: 2 additions & 1 deletion src/client/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { BrowserRouter } from 'react-router-dom'
import { bootstrap } from 'client/utils'
import { basename } from 'src/common'
import { App } from '@shared/app'

const AppContainer = () => (
<BrowserRouter>
<BrowserRouter basename={basename}>
<App nonce={window.nonce} />
</BrowserRouter>
)
Expand Down
2 changes: 1 addition & 1 deletion src/common/env/env.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function createEnv<S extends AnyObject>({
const { data, error } = parse((IS_SERVER ? schema : client) as ObjectSchema<S>, envs)

if (error) {
logger.error('❌ Invalid environment variables:', error.errors)
logger.error('❌ Invalid environment variables: %o', error.errors)
throw new Error('Invalid environment variables')
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/middleware/render/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { renderToPipeableStream } from 'react-dom/server'
import type { NextFunction, Request, Response } from 'express'
import { StaticRouter } from 'react-router-dom/server'
import { logger, setEnvVars } from 'src/common'
import { logger, setEnvVars, basename } from 'src/common'
import { ChunkExtractor } from 'server/utils'
import { getApp, getStats } from './render.util'

Expand All @@ -14,7 +14,7 @@ export const render = (req: Request, res: Response, next: NextFunction) => {
const { url, nonce } = req

const { pipe } = renderToPipeableStream(
<StaticRouter location={url}>
<StaticRouter location={url} basename={basename}>
<App nonce={nonce} />
</StaticRouter>,
{
Expand Down
2 changes: 1 addition & 1 deletion webpack/configs/client.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ export default {
plugins.definePlugin(),
...plugins.htmlWebpackPlugin()
].filter(Boolean),
optimization: optimization
optimization
} satisfies Configuration
37 changes: 17 additions & 20 deletions webpack/plugins/define.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@ interface Props {
server?: boolean
spa?: boolean
}

const config = (IS_SERVER: boolean, IS_SPA: boolean) => ({
IS_SERVER,
IS_DEV,
IS_PROD,
IS_SPA,
clientPrefix: JSON.stringify('CLIENT_'),
...(!IS_SERVER && {
'process.env': JSON.stringify(
Object.entries(process.env)
.filter(([k]) => k.startsWith('CLIENT_'))
.reduce<Collection<string, unknown>>((res, [k, v]) => {
res[k] = v
return res
}, {})
)
})
})

export const definePlugin = ({ server = false, spa = false }: Props = {}) =>
new DefinePlugin(config(server, spa))
new DefinePlugin({
IS_PROD,
IS_DEV,
IS_SPA: spa,
IS_SERVER: server,
clientPrefix: JSON.stringify('CLIENT_'),
...(spa && {
'process.env': JSON.stringify(
Object.entries(process.env)
.filter(([k]) => k.startsWith('CLIENT_'))
.reduce<Collection<string, unknown>>((res, [k, v]) => {
res[k] = v
return res
}, {})
)
})
})
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { join } from 'path'
import { ROOT_DIR } from './env'
import { existsSync, readFileSync } from 'fs'
import { ROOT_DIR } from '../env'
import { safeUrl } from './url'

const sslKeyPath = join(ROOT_DIR, 'certs/key.pem')
const sslCertPath = join(ROOT_DIR, 'certs/cert.pem')
const sslIsExist = existsSync(sslKeyPath) && existsSync(sslCertPath)

const { port, hostname } = safeUrl()

/**
* @see https://webpack.js.org/configuration/dev-server/
*/
Expand All @@ -19,8 +22,8 @@ export const devServerConfig = {
})
}
},
port: 3000,
host: 'localhost',
port,
host: hostname,
historyApiFallback: true,
static: { directory: join(ROOT_DIR, 'assets') }
}
9 changes: 9 additions & 0 deletions webpack/utils/dev-server/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'dotenv/config'

export function safeUrl() {
try {
return new URL(process.env.CLIENT_HOST ?? 'http://localhost:3000')
} catch (err) {
return new URL('http://localhost:3000')
}
}
2 changes: 1 addition & 1 deletion webpack/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './env'
export * from './devServer'
export * from './dev-server'
export * from './optimization'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Configuration } from 'webpack'
import { IS_PROD } from './env'
import { IS_PROD } from '../env'

export const optimization: Configuration['optimization'] = IS_PROD
? { splitChunks: { chunks: 'all' } }
Expand Down

0 comments on commit 6f4d6b1

Please sign in to comment.