|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | 3 | const {existsSync} = require(`fs`); |
4 | | -const {createRequire} = require(`module`); |
| 4 | +const {createRequire, register} = require(`module`); |
5 | 5 | const {resolve} = require(`path`); |
| 6 | +const {pathToFileURL} = require(`url`); |
6 | 7 |
|
7 | 8 | const relPnpApiPath = "../../../../.pnp.cjs"; |
8 | 9 |
|
9 | 10 | const absPnpApiPath = resolve(__dirname, relPnpApiPath); |
| 11 | +const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`); |
10 | 12 | const absRequire = createRequire(absPnpApiPath); |
11 | 13 |
|
12 | | -const moduleWrapper = tsserver => { |
| 14 | +const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`); |
| 15 | +const isPnpLoaderEnabled = existsSync(absPnpLoaderPath); |
| 16 | + |
| 17 | +if (existsSync(absPnpApiPath)) { |
| 18 | + if (!process.versions.pnp) { |
| 19 | + // Setup the environment to be able to require typescript/lib/tsserverlibrary.js |
| 20 | + require(absPnpApiPath).setup(); |
| 21 | + if (isPnpLoaderEnabled && register) { |
| 22 | + register(pathToFileURL(absPnpLoaderPath)); |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const wrapWithUserWrapper = existsSync(absUserWrapperPath) |
| 28 | + ? exports => absRequire(absUserWrapperPath)(exports) |
| 29 | + : exports => exports; |
| 30 | + |
| 31 | +const moduleWrapper = exports => { |
| 32 | + return wrapWithUserWrapper(moduleWrapperFn(exports)); |
| 33 | +}; |
| 34 | + |
| 35 | +const moduleWrapperFn = tsserver => { |
13 | 36 | if (!process.versions.pnp) { |
14 | 37 | return tsserver; |
15 | 38 | } |
@@ -214,11 +237,11 @@ const moduleWrapper = tsserver => { |
214 | 237 | return tsserver; |
215 | 238 | }; |
216 | 239 |
|
217 | | -if (existsSync(absPnpApiPath)) { |
218 | | - if (!process.versions.pnp) { |
219 | | - // Setup the environment to be able to require typescript/lib/tsserverlibrary.js |
220 | | - require(absPnpApiPath).setup(); |
221 | | - } |
| 240 | +const [major, minor] = absRequire(`typescript/package.json`).version.split(`.`, 2).map(value => parseInt(value, 10)); |
| 241 | +// In TypeScript@>=5.5 the tsserver uses the public TypeScript API so that needs to be patched as well. |
| 242 | +// Ref https://github.com/microsoft/TypeScript/pull/55326 |
| 243 | +if (major > 5 || (major === 5 && minor >= 5)) { |
| 244 | + moduleWrapper(absRequire(`typescript`)); |
222 | 245 | } |
223 | 246 |
|
224 | 247 | // Defer to the real typescript/lib/tsserverlibrary.js your application uses |
|
0 commit comments