|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const {existsSync} = require(`fs`); |
| 4 | +const {createRequire, createRequireFromPath} = require(`module`); |
| 5 | +const {resolve} = require(`path`); |
| 6 | + |
| 7 | +const relPnpApiPath = "../../../../.pnp.js"; |
| 8 | + |
| 9 | +const absPnpApiPath = resolve(__dirname, relPnpApiPath); |
| 10 | +const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath); |
| 11 | + |
| 12 | +const moduleWrapper = tsserver => { |
| 13 | + const {isAbsolute} = require(`path`); |
| 14 | + const pnpApi = require(`pnpapi`); |
| 15 | + |
| 16 | + const dependencyTreeRoots = new Set(pnpApi.getDependencyTreeRoots().map(locator => { |
| 17 | + return `${locator.name}@${locator.reference}`; |
| 18 | + })); |
| 19 | + |
| 20 | + // VSCode sends the zip paths to TS using the "zip://" prefix, that TS |
| 21 | + // doesn't understand. This layer makes sure to remove the protocol |
| 22 | + // before forwarding it to TS, and to add it back on all returned paths. |
| 23 | + |
| 24 | + function toEditorPath(str) { |
| 25 | + // We add the `zip:` prefix to both `.zip/` paths and virtual paths |
| 26 | + if (isAbsolute(str) && !str.match(/^\^zip:/) && (str.match(/\.zip\//) || str.match(/\$\$virtual\//))) { |
| 27 | + // We also take the opportunity to turn virtual paths into physical ones; |
| 28 | + // this makes is much easier to work with workspaces that list peer |
| 29 | + // dependencies, since otherwise Ctrl+Click would bring us to the virtual |
| 30 | + // file instances instead of the real ones. |
| 31 | + // |
| 32 | + // We only do this to modules owned by the the dependency tree roots. |
| 33 | + // This avoids breaking the resolution when jumping inside a vendor |
| 34 | + // with peer dep (otherwise jumping into react-dom would show resolution |
| 35 | + // errors on react). |
| 36 | + // |
| 37 | + const resolved = pnpApi.resolveVirtual(str); |
| 38 | + if (resolved) { |
| 39 | + const locator = pnpApi.findPackageLocator(resolved); |
| 40 | + if (locator && dependencyTreeRoots.has(`${locator.name}@${locator.reference}`)) { |
| 41 | + str = resolved; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + str = str.replace(/\\/g, `/`) |
| 46 | + str = str.replace(/^\/?/, `/`); |
| 47 | + |
| 48 | + // Absolute VSCode `Uri.fsPath`s need to start with a slash. |
| 49 | + // VSCode only adds it automatically for supported schemes, |
| 50 | + // so we have to do it manually for the `zip` scheme. |
| 51 | + // The path needs to start with a caret otherwise VSCode doesn't handle the protocol |
| 52 | + // |
| 53 | + // Ref: https://github.com/microsoft/vscode/issues/105014#issuecomment-686760910 |
| 54 | + // |
| 55 | + if (str.match(/\.zip\//)) { |
| 56 | + str = `${isVSCode ? `^` : ``}zip:${str}`; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return str; |
| 61 | + } |
| 62 | + |
| 63 | + function fromEditorPath(str) { |
| 64 | + return process.platform === `win32` |
| 65 | + ? str.replace(/^\^?zip:\//, ``) |
| 66 | + : str.replace(/^\^?zip:/, ``); |
| 67 | + } |
| 68 | + |
| 69 | + // And here is the point where we hijack the VSCode <-> TS communications |
| 70 | + // by adding ourselves in the middle. We locate everything that looks |
| 71 | + // like an absolute path of ours and normalize it. |
| 72 | + |
| 73 | + const Session = tsserver.server.Session; |
| 74 | + const {onMessage: originalOnMessage, send: originalSend} = Session.prototype; |
| 75 | + let isVSCode = false; |
| 76 | + |
| 77 | + return Object.assign(Session.prototype, { |
| 78 | + onMessage(/** @type {string} */ message) { |
| 79 | + const parsedMessage = JSON.parse(message) |
| 80 | + |
| 81 | + if ( |
| 82 | + parsedMessage != null && |
| 83 | + typeof parsedMessage === `object` && |
| 84 | + parsedMessage.arguments && |
| 85 | + parsedMessage.arguments.hostInfo === `vscode` |
| 86 | + ) { |
| 87 | + isVSCode = true; |
| 88 | + } |
| 89 | + |
| 90 | + return originalOnMessage.call(this, JSON.stringify(parsedMessage, (key, value) => { |
| 91 | + return typeof value === `string` ? fromEditorPath(value) : value; |
| 92 | + })); |
| 93 | + }, |
| 94 | + |
| 95 | + send(/** @type {any} */ msg) { |
| 96 | + return originalSend.call(this, JSON.parse(JSON.stringify(msg, (key, value) => { |
| 97 | + return typeof value === `string` ? toEditorPath(value) : value; |
| 98 | + }))); |
| 99 | + } |
| 100 | + }); |
| 101 | +}; |
| 102 | + |
| 103 | +if (existsSync(absPnpApiPath)) { |
| 104 | + if (!process.versions.pnp) { |
| 105 | + // Setup the environment to be able to require typescript/lib/tsserver.js |
| 106 | + require(absPnpApiPath).setup(); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Defer to the real typescript/lib/tsserver.js your application uses |
| 111 | +module.exports = moduleWrapper(absRequire(`typescript/lib/tsserver.js`)); |
0 commit comments