|
1 | 1 | // Only this file is allowed to import VSCode modules
|
2 | 2 | // tslint:disable: import-blacklist
|
3 | 3 |
|
4 |
| -import * as path from "path"; |
5 |
| -import * as vscode from "vscode"; |
| 4 | +import { env, window } from "vscode"; |
6 | 5 |
|
7 |
| -const appRoot = vscode.env.appRoot; |
| 6 | +declare const __webpack_require__: typeof require; |
| 7 | +declare const __non_webpack_require__: typeof require; |
8 | 8 |
|
9 |
| -function loadVSCodeModule(id: string) { |
| 9 | +function getNodeModule<T>(moduleName: string, showError = true): T | undefined { |
| 10 | + const r = |
| 11 | + typeof __webpack_require__ === "function" |
| 12 | + ? __non_webpack_require__ |
| 13 | + : require; |
10 | 14 | try {
|
11 |
| - return require(`${appRoot}/node_modules.asar/${id}`); |
12 |
| - } catch (ea) { |
13 |
| - // Ignore |
| 15 | + return r(`${env.appRoot}/node_modules.asar/${moduleName}`); |
| 16 | + } catch (err) { |
| 17 | + // Not in ASAR. |
14 | 18 | }
|
15 |
| - |
16 |
| - const baseDir = path.dirname(process.execPath); |
17 |
| - try { |
18 |
| - module.paths.unshift(`${baseDir}/node_modules`); |
19 |
| - return require(id); |
20 |
| - } catch (eb) { |
21 |
| - vscode.window.showErrorMessage( |
22 |
| - `Missing dependency, go to "${baseDir}" and run: npm install ${id}` |
23 |
| - ); |
24 |
| - } |
25 |
| -} |
26 |
| - |
27 |
| -function getNodeModule(moduleName: string) { |
28 | 19 | try {
|
29 |
| - return require(`${appRoot}/node_modules.asar/${moduleName}`); |
30 |
| - } catch (error) { |
31 |
| - //Ignore |
| 20 | + return r(`${env.appRoot}/node_modules/${moduleName}`); |
| 21 | + } catch (err) { |
| 22 | + if (showError) { |
| 23 | + window.showErrorMessage(`Missing dependency: ${moduleName}`); |
| 24 | + } |
32 | 25 | }
|
33 |
| - |
34 |
| - const baseDir = path.dirname(process.execPath); |
35 |
| - |
36 |
| - try { |
37 |
| - module.paths.unshift(`${baseDir}/node_modules`); |
38 |
| - return require(moduleName); |
39 |
| - } catch (error) {} |
40 |
| - |
41 | 26 | return undefined;
|
42 | 27 | }
|
43 | 28 |
|
| 29 | +export const keytar = getNodeModule("keytar") as typeof import("keytar"); |
| 30 | + |
44 | 31 | let iconv_lite = getNodeModule(
|
45 |
| - "iconv-lite-umd" |
| 32 | + "iconv-lite-umd", |
| 33 | + false |
46 | 34 | ) as typeof import("iconv-lite-umd");
|
47 | 35 | if (!iconv_lite) {
|
48 |
| - iconv_lite = loadVSCodeModule("iconv-lite") as typeof import("iconv-lite"); |
| 36 | + iconv_lite = getNodeModule("iconv-lite") as typeof import("iconv-lite"); |
49 | 37 | }
|
50 |
| - |
51 | 38 | export const iconv = iconv_lite;
|
52 |
| -export const jschardet = loadVSCodeModule( |
| 39 | + |
| 40 | +export const jschardet = getNodeModule( |
53 | 41 | "jschardet"
|
54 | 42 | ) as typeof import("jschardet");
|
55 |
| -export const keytar = loadVSCodeModule("keytar") as typeof import("keytar"); |
0 commit comments