Skip to content

Commit

Permalink
feat: support node runtime arguments (#2196)
Browse files Browse the repository at this point in the history
The primary purpose of this is to be able to add a Yarn loader for mjs.
  • Loading branch information
DavidArchibald authored Nov 14, 2023
1 parent 66e6d97 commit d7457f4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
7 changes: 6 additions & 1 deletion packages/svelte-vscode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ This setting can only be changed in user settings for security reasons.
You normally don't set this. Path to the language server executable. If you installed the `svelte-language-server` npm package, it's within there at `bin/server.js`. Path can be either relative to your workspace root or absolute. Set this only if you want to use a custom version of the language server. This will then also use the workspace version of TypeScript.
This setting can only be changed in user settings for security reasons.

#### `svelte.language-server.runtime-args`

You normally don't set this. Additional arguments to pass to Node when spawning the language server.
This is useful when you use something like Yarn PnP and need its loader arguments `["--loader", ".pnp.loader.mjs"]`.

##### `svelte.language-server.port`

You normally don't set this. At which port to spawn the language server.
Expand All @@ -86,7 +91,7 @@ Settings to toggle specific features of the extension. The full list of all sett
### Usage with Yarn 2 PnP

1. Run `yarn add -D svelte-language-server` to install svelte-language-server as a dev dependency
2. Run `yarn dlx @yarnpkg/sdks vscode` to generate or update the VSCode/Yarn integration SDKs. This also sets the `svelte.language-server.ls-path` setting for the workspace, pointing it to the workspace-installed language server. Note that this requires workspace trust - else set the `svelte.language-server.ls-path` setting in your user configuration.
2. Run `yarn dlx @yarnpkg/sdks vscode` to generate or update the VSCode/Yarn integration SDKs. This also sets the `svelte.language-server.ls-path` and `svelte.language-server.runtime-args` setting for the workspace, pointing it to the workspace-installed language server. Note that this requires workspace trust - else set the `svelte.language-server.ls-path` and `svelte.language-server.runtime-args` setting in your user configuration.
3. Restart VSCode.
4. Commit the changes to `.yarn/sdks`

Expand Down
8 changes: 7 additions & 1 deletion packages/svelte-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"supported": "limited",
"restrictedConfigurations": [
"svelte.language-server.runtime",
"svelte.language-server.ls-path"
"svelte.language-server.ls-path",
"svelte.language-server.runtime-args"
],
"description": "The extension requires workspace trust because it executes code specified by the workspace. Loading the user's node_modules and loading svelte config files is disabled when untrusted"
}
Expand Down Expand Up @@ -88,6 +89,11 @@
"title": "Language Server Path",
"description": "- You normally don't set this - Path to the language server executable. If you installed the \"svelte-language-server\" npm package, it's within there at \"bin/server.js\". Path can be either relative to your workspace root or absolute. Set this only if you want to use a custom version of the language server. This will then also use the workspace version of TypeScript. This setting can only be changed in user settings for security reasons."
},
"svelte.language-server.runtime-args": {
"type": "array",
"title": "Language Server Runtime Args",
"description": "You normally don't set this. Additional arguments to pass to the node executable when spawning the language server. This is useful when you use something like Yarn PnP and need its loader arguments `[\"--loader\", \".pnp.loader.mjs\"]`."
},
"svelte.language-server.port": {
"type": "number",
"title": "Language Server Port",
Expand Down
21 changes: 17 additions & 4 deletions packages/svelte-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,35 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
// Add --experimental-modules flag for people using node 12 < version < 12.17
// Remove this in mid 2022 and bump vs code minimum required version to 1.55
const runExecArgv: string[] = ['--experimental-modules'];
let port = runtimeConfig.get<number>('port') ?? -1;

const runtimeArgs = runtimeConfig.get<string[]>('runtime-args');
if (runtimeArgs !== undefined) {
runExecArgv.push(...runtimeArgs);
}

const debugArgs = ['--nolazy'];

const port = runtimeConfig.get<number>('port') ?? -1;
if (port < 0) {
port = 6009;
debugArgs.push('--inspect=6009');
} else {
console.log('setting port to', port);
runExecArgv.push(`--inspect=${port}`);
}
const debugOptions = { execArgv: ['--nolazy', '--experimental-modules', `--inspect=${port}`] };

debugArgs.push(...runExecArgv);

const serverOptions: ServerOptions = {
run: {
module: serverModule,
transport: TransportKind.ipc,
options: { execArgv: runExecArgv }
},
debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions }
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: { execArgv: debugArgs }
}
};

const serverRuntime = runtimeConfig.get<string>('runtime');
Expand Down

0 comments on commit d7457f4

Please sign in to comment.