Skip to content

Commit

Permalink
fix: capturer not work in web worker
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Aug 13, 2024
1 parent c8d7192 commit 0488e24
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 34 deletions.
11 changes: 8 additions & 3 deletions packages/connection/src/common/capturer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ export interface ICapturedMessage {
source?: string;
}

const _global = (typeof window !== 'undefined' ? window : global) || {
__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__: undefined,
};
const _global: any =
typeof global === 'undefined'
? typeof window === 'undefined'
? {
__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__: undefined,
}
: window
: global;

export function getCapturer() {
const hook = _global.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
11 changes: 11 additions & 0 deletions packages/extension/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 对 extension-host 使用 webpack bundle 后,require 方法会被覆盖为 webpack 内部的 require
* 这里是一个 webpack 提供的 workaround,用于获取原始的 require
*/
declare let __webpack_require__: any;
declare let __non_webpack_require__: any;

// https://github.com/webpack/webpack/issues/4175#issuecomment-342931035
export function getNodeRequire() {
return typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;
}
13 changes: 1 addition & 12 deletions packages/extension/src/hosted/ext.host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { join } from '@opensumi/ide-utils/lib/path';

import { EXTENSION_EXTEND_SERVICE_PREFIX, IExtendProxy, IExtensionHostService, getExtensionId } from '../common';
import { ActivatedExtension, ActivatedExtensionJSON, ExtensionsActivator } from '../common/activator';
import { getNodeRequire } from '../common/utils';
import {
ExtHostAPIIdentifier,
ExtensionIdentifier,
Expand All @@ -37,18 +38,6 @@ import { KTExtension } from './vscode.extension';

const { enumValueToArray } = arrays;

/**
* 对 extension-host 使用 webpack bundle 后,require 方法会被覆盖为 webpack 内部的 require
* 这里是一个 webpack 提供的 workaround,用于获取原始的 require
*/
declare let __webpack_require__: any;
declare let __non_webpack_require__: any;

// https://github.com/webpack/webpack/issues/4175#issuecomment-342931035
export function getNodeRequire() {
return typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;
}

enum EInternalModule {
VSCODE = 'vscode',
KAITIAN = 'kaitian',
Expand Down
10 changes: 3 additions & 7 deletions packages/extension/src/hosted/extension-log2.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Injector } from '@opensumi/di';
import {
IExtensionLogger,
ILogService,
LogLevel,
SupportLogNamespace,
getNodeRequire,
} from '@opensumi/ide-core-common';
import { IExtensionLogger, ILogService, LogLevel, SupportLogNamespace } from '@opensumi/ide-core-common';
import { AppConfig } from '@opensumi/ide-core-node/lib/types';
import { LogServiceManager } from '@opensumi/ide-logs/lib/node/log-manager';

import { getNodeRequire } from '../common/utils';

export class ExtensionLogger2 implements IExtensionLogger {
private injector: Injector;
private loggerManager: LogServiceManager;
Expand Down
3 changes: 2 additions & 1 deletion packages/extension/src/node/extension.scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import path from 'path';
import * as fs from 'fs-extra';
import semver from 'semver';

import { Uri, getDebugLogger, getNodeRequire } from '@opensumi/ide-core-node';
import { Uri, getDebugLogger } from '@opensumi/ide-core-node';

import { IExtensionMetaData, IExtraMetaData } from '../common';
import { getNodeRequire } from '../common/utils';

import { mergeContributes } from './merge-contributes';

Expand Down
11 changes: 0 additions & 11 deletions packages/utils/src/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,3 @@ export function isElectronNode() {
export function isDevelopment() {
return safeGlobal.isDev || (typeof process !== 'undefined' && process.env.IS_DEV);
}

/**
* 在 Electron 中,会将 opensumi 中的 extension-host 使用 webpack 打成一个,所以需要其他方法来获取原始的 require
*/
declare let __webpack_require__: any;
declare let __non_webpack_require__: any;

// https://github.com/webpack/webpack/issues/4175#issuecomment-342931035
export function getNodeRequire() {
return typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;
}

0 comments on commit 0488e24

Please sign in to comment.