Skip to content

[1.x] Add remote IDE support #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ interface PluginConfig {
/**
* Transform the code while serving.
*/
transformOnServe?: (code: string, url: DevServerUrl) => string,
transformOnServe?: (code: string, url: string) => string,
}

interface RefreshConfig {
Expand All @@ -85,8 +85,6 @@ interface LaravelPlugin extends Plugin {
config: (config: UserConfig, env: ConfigEnv) => UserConfig
}

type DevServerUrl = `${'http'|'https'}://${string}:${number}`

let exitHandlersBound = false

export const refreshPaths = [
Expand Down Expand Up @@ -116,7 +114,8 @@ export default function laravel(config: string|string[]|PluginConfig): [LaravelP
* Resolve the Laravel Plugin configuration.
*/
function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlugin {
let viteDevServerUrl: DevServerUrl
const exposedDevServerFormat = process.env.LARAVEL_VITE_EXPOSED_DEV_SERVER_URL ? process.env.LARAVEL_VITE_EXPOSED_DEV_SERVER_URL : '{protocol}://{host}:{port}'
let exposedDevServerUrl: string
let resolvedConfig: ResolvedConfig
let userConfig: UserConfig

Expand Down Expand Up @@ -152,6 +151,9 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
},
server: {
origin: userConfig.server?.origin ?? '__laravel_vite_placeholder__',
...(process.env.VITE_HOST ? {
host: process.env.VITE_HOST
} : undefined),
...(process.env.LARAVEL_SAIL ? {
host: userConfig.server?.host ?? '0.0.0.0',
port: userConfig.server?.port ?? (env.VITE_PORT ? parseInt(env.VITE_PORT) : 5173),
Expand Down Expand Up @@ -190,9 +192,9 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
},
transform(code) {
if (resolvedConfig.command === 'serve') {
code = code.replace(/__laravel_vite_placeholder__/g, viteDevServerUrl)
code = code.replace(/__laravel_vite_placeholder__/g, exposedDevServerUrl)

return pluginConfig.transformOnServe(code, viteDevServerUrl)
return pluginConfig.transformOnServe(code, exposedDevServerUrl)
}
},
configureServer(server) {
Expand All @@ -204,9 +206,9 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug

const isAddressInfo = (x: string|AddressInfo|null|undefined): x is AddressInfo => typeof x === 'object'
if (isAddressInfo(address)) {
viteDevServerUrl = userConfig.server?.origin ? userConfig.server.origin as DevServerUrl : resolveDevServerUrl(address, server.config, userConfig)
exposedDevServerUrl = userConfig.server?.origin ? userConfig.server.origin : resolveDevServerUrl(address, server.config, userConfig, exposedDevServerFormat)

fs.writeFileSync(pluginConfig.hotFile, `${viteDevServerUrl}${server.config.base.replace(/\/$/, '')}`)
fs.writeFileSync(pluginConfig.hotFile, `${exposedDevServerUrl}${server.config.base.replace(/\/$/, '')}`)

setTimeout(() => {
server.config.logger.info(`\n ${colors.red(`${colors.bold('LARAVEL')} ${laravelVersion()}`)} ${colors.dim('plugin')} ${colors.bold(`v${pluginVersion()}`)}`)
Expand Down Expand Up @@ -419,7 +421,7 @@ function resolveFullReloadConfig({ refresh: config }: Required<PluginConfig>): P
/**
* Resolve the dev server URL from the server address and configuration.
*/
function resolveDevServerUrl(address: AddressInfo, config: ResolvedConfig, userConfig: UserConfig): DevServerUrl {
function resolveDevServerUrl(address: AddressInfo, config: ResolvedConfig, userConfig: UserConfig, format: string): string {
const configHmrProtocol = typeof config.server.hmr === 'object' ? config.server.hmr.protocol : null
const clientProtocol = configHmrProtocol ? (configHmrProtocol === 'wss' ? 'https' : 'http') : null
const serverProtocol = config.server.https ? 'https' : 'http'
Expand All @@ -434,7 +436,9 @@ function resolveDevServerUrl(address: AddressInfo, config: ResolvedConfig, userC
const configHmrClientPort = typeof config.server.hmr === 'object' ? config.server.hmr.clientPort : null
const port = configHmrClientPort ?? address.port

return `${protocol}://${host}:${port}`
return format.replace('{protocol}', protocol)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh actually another typo: your original description used {scheme}

.replace('{host}', host)
.replace('{port}', port.toString())
}

function isIpv6(address: AddressInfo): boolean {
Expand Down
Loading