diff --git a/package-lock.json b/package-lock.json index 7d8c18d..3cd1057 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2225,7 +2225,8 @@ "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", "hasInstallScript": true, "os": [ - "darwin" + "darwin", + "linux" ], "engines": { "node": "^8.16.0 || ^10.6.0 || >=11.0.0" diff --git a/package.json b/package.json index 2fd131a..63a0dd5 100644 --- a/package.json +++ b/package.json @@ -151,6 +151,11 @@ "default": "", "markdownDescription": "Absolute path to php.ini file. `string`" }, + "fiveServer.baseURL": { + "type": "string", + "default": "/", + "markdownDescription": "Manually set the baseURL of the server. (Useful if the server is behind a reverse proxy) `string`\n\r*If set to 'AUTO' server root will automatically be set to \"/\" if running on desktop and \"/proxy/{PORT}/\" if on web*" + }, "fiveServer.host": { "type": "string", "default": "0.0.0.0", diff --git a/src/extension.ts b/src/extension.ts index 754fc94..725b5c4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -20,6 +20,7 @@ let pty: PTY; let activeFileName = ""; let root: string = ""; let _root: string | null = null; +let baseURL: string = "NULL"; let workspace: string | undefined; let rootAbsolute: string; let config: LiveServerParams = {}; @@ -111,6 +112,21 @@ const shouldInjectBody = () => { return false; }; +function autoSetBaseURL() { + // Auto set baseURL based on appHost + if (config.baseURL !== "AUTO") { + if (config.baseURL) baseURL = config.baseURL; + return; + } + + if (vscode.env.appHost !== "desktop") { + baseURL = "/proxy/" + config.port ?? "5500" + "/"; + return; + } + + baseURL = "/"; +} + export function activate(context: vscode.ExtensionContext) { context.workspaceState.update(state, "off"); @@ -268,6 +284,8 @@ export function activate(context: vscode.ExtensionContext) { // get configFile for "root, injectBody and highlight" config = { ...config, ...(await getConfigFile(true, workspace)) }; + autoSetBaseURL(); + if (_root) root = _root; else if (config && config.root) root = config.root; else root = ""; @@ -303,6 +321,7 @@ export function activate(context: vscode.ExtensionContext) { injectBody: shouldInjectBody(), open: config.open !== undefined ? config.open : file, root, + baseURL, workspace, _cli: true, }); @@ -322,6 +341,7 @@ export function activate(context: vscode.ExtensionContext) { injectBody: shouldInjectBody(), open: config.open !== undefined ? config.open : file, root, + baseURL, workspace, _cli: true, }); @@ -348,11 +368,14 @@ export function activate(context: vscode.ExtensionContext) { const file = basename(fileName); const root = fileName.replace(file, ""); + autoSetBaseURL(); + // start a simple server await fiveServer.start({ ...config, injectBody: shouldInjectBody(), root, + baseURL, open: file, }); } diff --git a/src/helpers.ts b/src/helpers.ts index 63e9f3b..5208926 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -33,6 +33,7 @@ export const assignVSCodeConfiguration = () => { const navigate = getConfig("navigate"); const php = getConfig("php.executable"); const phpIni = getConfig("php.ini"); + const baseURL = getConfig("baseURL"); const host = getConfig("host"); const port = getConfig("port"); const injectBody = getConfig("injectBody"); @@ -44,6 +45,7 @@ export const assignVSCodeConfiguration = () => { navigate, php, phpIni, + baseURL, host, port, injectBody, diff --git a/src/test/runTest.ts b/src/test/runTest.ts index 300c9d5..6ec5744 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -38,7 +38,7 @@ async function main() { { encoding: "utf-8", stdio: "inherit", - } + }, ); // Run the extension test