Skip to content

Commit

Permalink
Read host & port directly from dev server (fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan-Shields committed Nov 28, 2023
1 parent 2b91002 commit 3a70f1e
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
})

let config: ResolvedConfig
let dSrvProtocol: string
let dSrvHost: string
let assetManifest: Manifest
let protocol: string
let socketAddr: string

let resolvedInputOptions: InputOptions

Expand All @@ -88,14 +88,14 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {

if (config.mode === 'development') {
tags.push(
`<script type="module" src="${protocol}://${path.posix.join(
socketAddr,
`<script type="module" src="${dSrvProtocol}://${path.posix.join(
dSrvHost,
'@vite/client'
)}"></script>`
)
tags.push(
`<script type="module" src="${protocol}://${path.posix.join(
socketAddr,
`<script type="module" src="${dSrvProtocol}://${path.posix.join(
dSrvHost,
'bundles',
bundleName,
entry
Expand Down Expand Up @@ -199,7 +199,7 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
fs.mkdirSync(dir, { recursive: true })
} catch (e) {
console.error(
`Could not create directory ${dir} for input ${filePath}. Skipping...`
`vite-plugin-nodecg: Could not create directory ${dir} for input ${filePath}. Skipping...`
)
continue
}
Expand All @@ -217,13 +217,6 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {

// validate and setup defaults in user's vite config
config: (_config, { mode }): UserConfig => {
protocol = _config?.server?.https ? 'https' : 'http'
socketAddr = `${
typeof _config?.server?.host === 'string'
? _config?.server?.host
: 'localhost'
}:${_config?.server?.port?.toString() ?? '5173'}`

return {
build: {
manifest: true,
Expand All @@ -232,9 +225,6 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
input: inputs,
},
},
server: {
origin: `${protocol}://${socketAddr}`,
},
base: `/bundles/${bundleName}/${
mode === 'development' ? '' : 'shared/dist/'
}`,
Expand All @@ -249,12 +239,6 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
buildStart(options: InputOptions) {
// capture inputOptions for use in generateHtmlFiles in both dev & prod
resolvedInputOptions = options

if (!resolvedInputOptions?.input || config.mode !== 'development')
return

// dev inject
generateHTMLFiles()
},

writeBundle() {
Expand Down Expand Up @@ -285,5 +269,16 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
// prod inject
generateHTMLFiles()
},

configureServer(server) {
server.httpServer.on('listening', () => {
dSrvProtocol = server.config.server.https ? 'https' : 'http'
dSrvHost = `${server.config.server.host ?? 'localhost'}:${
server.config.server.port ?? '5173'
}`
// dev inject
generateHTMLFiles()
})
},
}
}

0 comments on commit 3a70f1e

Please sign in to comment.