ms-types 0.11.4 → 0.11.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -3
- package/ts-plugin/idea.cjs +77 -0
- package/ts-plugin/index.cjs +33 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ms-types",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"access": "public"
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
|
-
"types"
|
|
16
|
+
"types",
|
|
17
|
+
"ts-plugin"
|
|
17
18
|
],
|
|
18
19
|
"scripts": {
|
|
19
20
|
"docs:dev": "vitepress dev docs --host",
|
|
@@ -24,6 +25,6 @@
|
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"vitepress": "^1.6.4",
|
|
26
27
|
"vitepress-theme-teek": "^1.6.2",
|
|
27
|
-
"wrangler": "^4.
|
|
28
|
+
"wrangler": "^4.147.0"
|
|
28
29
|
}
|
|
29
30
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// IDEA 没有 typescriptServerPlugins,用 `-r` 预加载:在 JetBrains 的 tsc-ide-plugin 创建项目时,
|
|
4
|
+
// 对快点JS项目调用 index.cjs 的 inject。tsserver 进程是所有工程共用的,所以要先判断是不是快点JS项目:
|
|
5
|
+
// 从项目目录往上找带 `ms build` / `ms package` 脚本的 package.json。
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const Module = require('module');
|
|
9
|
+
const { inject } = require('./index.cjs');
|
|
10
|
+
|
|
11
|
+
const MS_SCRIPT = /\bms\s+(build|package)\b/;
|
|
12
|
+
const MAX_ASCEND = 4;
|
|
13
|
+
const projectCache = new Map();
|
|
14
|
+
|
|
15
|
+
function hasKuaiJsPackage(dir) {
|
|
16
|
+
try {
|
|
17
|
+
const scripts = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')).scripts || {};
|
|
18
|
+
return ['dev', 'build', 'package'].some((key) => MS_SCRIPT.test(String(scripts[key] || '')));
|
|
19
|
+
} catch (ignored) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isKuaiJsProject(info) {
|
|
25
|
+
let dir;
|
|
26
|
+
try {
|
|
27
|
+
dir = path.resolve(info.project.getCurrentDirectory());
|
|
28
|
+
} catch (ignored) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
const start = dir;
|
|
32
|
+
if (projectCache.has(start)) return projectCache.get(start);
|
|
33
|
+
let found = false;
|
|
34
|
+
for (let i = 0; i < MAX_ASCEND && dir; i += 1) {
|
|
35
|
+
if (hasKuaiJsPackage(dir)) {
|
|
36
|
+
found = true;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
const parent = path.dirname(dir);
|
|
40
|
+
if (parent === dir) break;
|
|
41
|
+
dir = parent;
|
|
42
|
+
}
|
|
43
|
+
projectCache.set(start, found);
|
|
44
|
+
return found;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function wrapFactory(officialFactory) {
|
|
48
|
+
if (officialFactory.__kuaijsWrapped) return officialFactory;
|
|
49
|
+
const wrapped = function init(modules) {
|
|
50
|
+
const official = officialFactory(modules);
|
|
51
|
+
return {
|
|
52
|
+
...official,
|
|
53
|
+
create(info) {
|
|
54
|
+
const languageService = official.create(info);
|
|
55
|
+
try {
|
|
56
|
+
if (isKuaiJsProject(info)) inject(modules.typescript, info);
|
|
57
|
+
} catch (ignored) {
|
|
58
|
+
// 注入失败不能影响 IDE 自己的语言服务。
|
|
59
|
+
}
|
|
60
|
+
return languageService;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
wrapped.__kuaijsWrapped = true;
|
|
65
|
+
return wrapped;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const originalLoad = Module._load;
|
|
69
|
+
Module._load = function (request) {
|
|
70
|
+
const exported = originalLoad.apply(this, arguments);
|
|
71
|
+
const name = String(request || '').replace(/\\/g, '/');
|
|
72
|
+
if ((name === 'tsc-ide-plugin' || name.endsWith('/tsc-ide-plugin') || name.includes('/tsc-ide-plugin/')) &&
|
|
73
|
+
typeof exported === 'function') {
|
|
74
|
+
return wrapFactory(exported);
|
|
75
|
+
}
|
|
76
|
+
return exported;
|
|
77
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// tsserver 插件:把本包的 types/index.d.ts 作为根文件加进项目,不用改项目的 tsconfig.json。
|
|
4
|
+
// VS Code 的 kuaijs-ts-plugin 和 IDEA 的预加载(idea.cjs)共用这一份。
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
const typesIndex = path.join(__dirname, '..', 'types', 'index.d.ts');
|
|
8
|
+
|
|
9
|
+
function inject(ts, info) {
|
|
10
|
+
const extra = ts.server.toNormalizedPath(typesIndex);
|
|
11
|
+
const host = info.languageServiceHost;
|
|
12
|
+
const getScriptFileNames = host.getScriptFileNames.bind(host);
|
|
13
|
+
host.getScriptFileNames = () => {
|
|
14
|
+
const files = getScriptFileNames();
|
|
15
|
+
return files.includes(extra) ? files : files.concat(extra);
|
|
16
|
+
};
|
|
17
|
+
const scriptInfo = info.project.projectService.getOrCreateScriptInfoForNormalizedPath(extra, false);
|
|
18
|
+
if (scriptInfo) info.project.addRoot(scriptInfo);
|
|
19
|
+
info.project.markAsDirty();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function init(modules) {
|
|
23
|
+
const ts = modules.typescript;
|
|
24
|
+
return {
|
|
25
|
+
create(info) {
|
|
26
|
+
inject(ts, info);
|
|
27
|
+
return info.languageService;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
init.inject = inject;
|
|
33
|
+
module.exports = init;
|