Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid poisoning global scope #457

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/shiki/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
declare var __BROWSER__: boolean
declare global {
var __BROWSER__: boolean

// These will not be defined for Node which don't have "DOM" in their lib.
// So declare the minimal interface we need.
// They are defined here and not in `loader.ts` to avoid getting injected in the built code.
interface Window {
WorkerGlobalScope: any
}

var self: Window & typeof globalThis
function fetch(url: string): Promise<Response>
interface Response {
json(): Promise<any>
text(): Promise<any>
}
}

// This export is here so that all variables above would be added to the global scope
export {}
16 changes: 2 additions & 14 deletions packages/shiki/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
/// <reference path="./global.d.ts" />

import { join, dirpathparts } from './utils'
import type { IGrammar, IOnigLib, IRawTheme } from 'vscode-textmate'
import { loadWASM, createOnigScanner, createOnigString } from 'vscode-oniguruma'
import { parse, ParseError } from 'jsonc-parser'
import type { IShikiTheme } from './types'

// These will not be defined for Node which don't have "DOM" in their lib.
// So declare the minimal interface we need.
declare global {
interface Window {
WorkerGlobalScope: any
}
var self: Window & typeof globalThis
function fetch(url: string): Promise<Response>
interface Response {
json(): Promise<any>
text(): Promise<any>
}
}

export const isWebWorker =
typeof self !== 'undefined' && typeof self.WorkerGlobalScope !== 'undefined'
export const isNode =
Expand Down