• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// We polyfill `globalThis` here so re can reliably patch the global scope
2// in the contexts we want to in the same way across script and module formats
3
4// https://mathiasbynens.be/notes/globalthis
5
6// #region The polyfill starts here.
7/* eslint-disable no-var */
8/* @internal */
9declare var window: {};
10/* eslint-enable no-var */
11((() => {
12    if (typeof globalThis === "object") return;
13    try {
14        Object.defineProperty(Object.prototype, "__magic__", {
15            get() {
16                return this;
17            },
18            configurable: true
19        });
20        //@ts-ignore
21        __magic__.globalThis = __magic__;
22        // The previous line should have made `globalThis` globally
23        // available, but it fails in Internet Explorer 10 and older.
24        // Detect this failure and fall back.
25        if (typeof globalThis === "undefined") {
26            // Assume `window` exists.
27            //@ts-ignore
28            window.globalThis = window;
29        }
30        //@ts-ignore
31        delete Object.prototype.__magic__;
32    }
33    catch (error) {
34        // In IE8, Object.defineProperty only works on DOM objects.
35        // If we hit this code path, assume `window` exists.
36        //@ts-ignore
37        window.globalThis = window;
38    }
39})());
40// #endregion The polyfill ends here.
41
42// if `process` is undefined, we're probably not running in node - patch legacy members onto the global scope
43// @ts-ignore
44if (typeof process === "undefined" || process.browser) {
45    /// TODO: this is used by VS, clean this up on both sides of the interface
46
47    //@ts-ignore
48    globalThis.TypeScript = globalThis.TypeScript || {};
49    //@ts-ignore
50    globalThis.TypeScript.Services = globalThis.TypeScript.Services || {};
51    //@ts-ignore
52    globalThis.TypeScript.Services.TypeScriptServicesFactory = ts.TypeScriptServicesFactory;
53
54    // 'toolsVersion' gets consumed by the managed side, so it's not unused.
55    // TODO: it should be moved into a namespace though.
56
57    //@ts-ignore
58    globalThis.toolsVersion = ts.versionMajorMinor;
59}