vue-go-tsc 0.2.4 → 0.3.0
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/README.md +23 -2
- package/bin/vue-go-tsc +118 -6
- package/bin/vue-go-tsc.test.js +57 -0
- package/package.json +2 -1
- package/volar/plugin.mjs +105 -0
- package/volar/plugin.mjs.LEGAL.txt +25 -0
package/README.md
CHANGED
|
@@ -39,14 +39,35 @@ nuxt prepare && vue-go-tsc -p .nuxt/tsconfig.json --noEmit
|
|
|
39
39
|
|
|
40
40
|
See the full [CI integration guide](https://github.com/aidalinfo/vue-tsgo/blob/main/docs/integration.md).
|
|
41
41
|
|
|
42
|
+
### Codegen modes
|
|
43
|
+
|
|
44
|
+
The `.vue` files are turned into TypeScript ("virtual code") before tsgo
|
|
45
|
+
type-checks them. Two codegens are available:
|
|
46
|
+
|
|
47
|
+
| Mode | How | When |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| `volar` (**default**) | `@vue/language-core` — the codegen `vue-tsc` runs — taken from your project's `vue-tsc` (bundled copy if you have none) | Same results as `vue-tsc`, for your Volar version |
|
|
50
|
+
| `go` | Built-in Go port of the codegen | Fastest; may differ from `vue-tsc` where the port lags behind Volar |
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
vue-go-tsc --noEmit -p tsconfig.json # Volar codegen (default)
|
|
54
|
+
vue-go-tsc --codegen=go --noEmit -p tsconfig.json # Go codegen
|
|
55
|
+
VUE_GO_TSC_CODEGEN=go vue-go-tsc --noEmit # same, via env
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The Volar codegen runs in a few Node worker threads next to tsgo and caches
|
|
59
|
+
its output in `~/.cache/vue-go-tsc/volar-codegen` (`GOLAR_VUE_CACHE=0` to
|
|
60
|
+
disable). It needs `typescript` installed in the project. The type-checking
|
|
61
|
+
itself is always tsgo.
|
|
62
|
+
|
|
42
63
|
## Why vue-go-tsc?
|
|
43
64
|
|
|
44
65
|
- **~5–25× faster** than vue-tsc (project-dependent) — e.g. on the Pulse ERP
|
|
45
66
|
monorepo: app ~232s → **~49s** (~4.7×), docs site ~28s → **~1.5s** (~18×)
|
|
46
67
|
- **Lower memory usage** — native Go instead of Node.js
|
|
47
68
|
- **Drop-in replacement** for vue-tsc
|
|
48
|
-
- **
|
|
49
|
-
|
|
69
|
+
- **Same errors as vue-tsc** — the default Volar codegen is vue-tsc's own,
|
|
70
|
+
checked against vue-tsc in CI (`tests/parity`)
|
|
50
71
|
|
|
51
72
|
## Documentation
|
|
52
73
|
|
package/bin/vue-go-tsc
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { spawn } = require('child_process');
|
|
4
4
|
const fs = require('fs');
|
|
5
|
-
const { join } = require('path');
|
|
5
|
+
const { join, resolve } = require('path');
|
|
6
6
|
const os = require('os');
|
|
7
7
|
|
|
8
8
|
const GiB = 1024 ** 3;
|
|
@@ -45,6 +45,21 @@ const STALE_ENTRY_MS = 24 * 60 * 60 * 1000;
|
|
|
45
45
|
|
|
46
46
|
const REGISTRY_DIR = join(os.tmpdir(), 'vue-go-tsc-instances');
|
|
47
47
|
|
|
48
|
+
// Codegen modes. `volar` (default) generates the .vue virtual code with
|
|
49
|
+
// `@vue/language-core` — the codegen vue-tsc itself runs, taken from the
|
|
50
|
+
// project's vue-tsc — so results match vue-tsc; type-checking stays in tsgo.
|
|
51
|
+
// `go` uses the built-in Go port of the codegen (faster, but it can only match
|
|
52
|
+
// one Volar version and may lag behind it).
|
|
53
|
+
const CODEGEN_MODES = ['volar', 'go'];
|
|
54
|
+
const DEFAULT_CODEGEN = 'volar';
|
|
55
|
+
|
|
56
|
+
// Approximate resident memory of the Volar codegen Node processes (measured
|
|
57
|
+
// ~180 MB per worker thread on a Nuxt app, plus the main thread). Reserved
|
|
58
|
+
// out of the Go heap ceiling so both fit in the machine's budget.
|
|
59
|
+
const VOLAR_BASE_MEM = 200 * 1024 ** 2;
|
|
60
|
+
const VOLAR_WORKER_MEM = 200 * 1024 ** 2;
|
|
61
|
+
const MAX_CODEGEN_WORKERS = 4;
|
|
62
|
+
|
|
48
63
|
// Compute GOMAXPROCS for one of `instances` concurrent runs on a machine with
|
|
49
64
|
// `cpus` cores of which `idleCpus` are currently idle.
|
|
50
65
|
//
|
|
@@ -118,10 +133,71 @@ function computeGoMemLimit(totalBytes, instances = 1, availableBytes) {
|
|
|
118
133
|
return Math.floor(Math.max(Math.min(MIN_MEM_LIMIT, totalBytes * DEFAULT_MEM_FRACTION), share));
|
|
119
134
|
}
|
|
120
135
|
|
|
136
|
+
// Split `--codegen=<mode>` / `--codegen <mode>` out of the tsgo arguments.
|
|
137
|
+
// The VUE_GO_TSC_CODEGEN env var sets the default; the flag wins.
|
|
138
|
+
function parseCodegenArgs(argv, env = {}) {
|
|
139
|
+
let codegen = env.VUE_GO_TSC_CODEGEN || DEFAULT_CODEGEN;
|
|
140
|
+
const args = [];
|
|
141
|
+
for (let i = 0; i < argv.length; i++) {
|
|
142
|
+
const arg = argv[i];
|
|
143
|
+
if (arg === '--codegen') {
|
|
144
|
+
codegen = argv[++i];
|
|
145
|
+
} else if (arg.startsWith('--codegen=')) {
|
|
146
|
+
codegen = arg.slice('--codegen='.length);
|
|
147
|
+
} else {
|
|
148
|
+
args.push(arg);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (!CODEGEN_MODES.includes(codegen)) {
|
|
152
|
+
return { args, error: `invalid codegen mode ${JSON.stringify(codegen)} (expected ${CODEGEN_MODES.join(' or ')})` };
|
|
153
|
+
}
|
|
154
|
+
return { codegen, args };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// The tsconfig tsgo will check, for the Volar codegen to read its
|
|
158
|
+
// vueCompilerOptions: `-p/--project <file|dir>`, the first `-b/--build`
|
|
159
|
+
// project, or ./tsconfig.json.
|
|
160
|
+
function resolveProjectConfig(args, cwd, isDirectory = (p) => fs.statSync(p).isDirectory()) {
|
|
161
|
+
let target;
|
|
162
|
+
for (let i = 0; i < args.length; i++) {
|
|
163
|
+
const arg = args[i];
|
|
164
|
+
if (arg === '-p' || arg === '--project') {
|
|
165
|
+
target = args[i + 1];
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
if (arg.startsWith('--project=')) {
|
|
169
|
+
target = arg.slice('--project='.length);
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
if ((arg === '-b' || arg === '--build') && args[i + 1] && !args[i + 1].startsWith('-')) {
|
|
173
|
+
target = args[i + 1];
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const resolved = resolve(cwd, target ?? '.');
|
|
178
|
+
let dir = false;
|
|
179
|
+
try {
|
|
180
|
+
dir = isDirectory(resolved);
|
|
181
|
+
} catch {}
|
|
182
|
+
return dir ? join(resolved, 'tsconfig.json') : resolved;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Number of Volar codegen worker threads: half the Go cores (codegen overlaps
|
|
186
|
+
// with tsgo's own parsing), at most MAX_CODEGEN_WORKERS, fewer when free RAM is
|
|
187
|
+
// short (a worker must not take more than 1/8 of the available memory).
|
|
188
|
+
function computeCodegenWorkers(procs, availableMem) {
|
|
189
|
+
let workers = Math.min(MAX_CODEGEN_WORKERS, Math.max(1, Math.floor((procs || 2) / 2)));
|
|
190
|
+
if (Number.isFinite(availableMem) && availableMem > 0) {
|
|
191
|
+
workers = Math.min(workers, Math.max(1, Math.floor(availableMem / 8 / VOLAR_WORKER_MEM)));
|
|
192
|
+
}
|
|
193
|
+
return workers;
|
|
194
|
+
}
|
|
195
|
+
|
|
121
196
|
// Return a copy of `env` with GOMAXPROCS and GOMEMLIMIT defaulted from the
|
|
122
197
|
// machine resources, what is currently free, and the number of concurrent
|
|
123
|
-
// runs. Explicit values are always respected (never overridden).
|
|
124
|
-
|
|
198
|
+
// runs. Explicit values are always respected (never overridden). With the
|
|
199
|
+
// Volar codegen, its Node processes get a share of the memory budget.
|
|
200
|
+
function resolveEnv(env, { cpus, totalMem, instances = 1, idleCpus, availableMem, codegen = 'go', volar }) {
|
|
125
201
|
const out = { ...env };
|
|
126
202
|
if (!out.GOMAXPROCS) {
|
|
127
203
|
const procs = computeGoMaxProcs(cpus, instances, idleCpus);
|
|
@@ -129,8 +205,24 @@ function resolveEnv(env, { cpus, totalMem, instances = 1, idleCpus, availableMem
|
|
|
129
205
|
out.GOMAXPROCS = String(procs);
|
|
130
206
|
}
|
|
131
207
|
}
|
|
208
|
+
let workers = 0;
|
|
209
|
+
if (codegen === 'volar') {
|
|
210
|
+
workers = Number(out.GOLAR_PLUGIN_WORKERS) || computeCodegenWorkers(Number(out.GOMAXPROCS), availableMem);
|
|
211
|
+
out.GOLAR_PLUGIN = 'vue';
|
|
212
|
+
out.GOLAR_PLUGIN_WORKERS = String(workers);
|
|
213
|
+
if (volar) {
|
|
214
|
+
out.GOLAR_VUE_PLUGIN_ENTRY = volar.pluginEntry;
|
|
215
|
+
out.GOLAR_NODE = volar.nodePath;
|
|
216
|
+
out.GOLAR_VUE_TSCONFIG = volar.tsconfig;
|
|
217
|
+
}
|
|
218
|
+
} else {
|
|
219
|
+
delete out.GOLAR_PLUGIN;
|
|
220
|
+
}
|
|
132
221
|
if (!out.GOMEMLIMIT) {
|
|
133
|
-
|
|
222
|
+
let limit = computeGoMemLimit(totalMem, instances, availableMem);
|
|
223
|
+
if (limit && workers > 0) {
|
|
224
|
+
limit = Math.floor(Math.max(Math.min(MIN_MEM_LIMIT, limit), limit - VOLAR_BASE_MEM - workers * VOLAR_WORKER_MEM));
|
|
225
|
+
}
|
|
134
226
|
if (limit) {
|
|
135
227
|
// A bare integer is interpreted by the Go runtime as a byte count.
|
|
136
228
|
out.GOMEMLIMIT = String(limit);
|
|
@@ -229,6 +321,17 @@ function sampleIdleCpus(cpus) {
|
|
|
229
321
|
async function main() {
|
|
230
322
|
const BINARY_NAME = process.platform === 'win32' ? 'tsgo.exe' : 'tsgo';
|
|
231
323
|
const BIN_PATH = join(__dirname, BINARY_NAME);
|
|
324
|
+
const VOLAR_PLUGIN = join(__dirname, '..', 'volar', 'plugin.mjs');
|
|
325
|
+
|
|
326
|
+
const { codegen, args, error } = parseCodegenArgs(process.argv.slice(2), process.env);
|
|
327
|
+
if (error) {
|
|
328
|
+
console.error(`vue-go-tsc: ${error}`);
|
|
329
|
+
process.exit(1);
|
|
330
|
+
}
|
|
331
|
+
if (codegen === 'volar' && !fs.existsSync(VOLAR_PLUGIN)) {
|
|
332
|
+
console.error(`vue-go-tsc: the Volar codegen plugin is missing (${VOLAR_PLUGIN}); reinstall vue-go-tsc or use --codegen=go`);
|
|
333
|
+
process.exit(1);
|
|
334
|
+
}
|
|
232
335
|
|
|
233
336
|
const { instances, unregister } = registerInstance();
|
|
234
337
|
process.on('exit', unregister);
|
|
@@ -243,10 +346,16 @@ async function main() {
|
|
|
243
346
|
instances,
|
|
244
347
|
idleCpus,
|
|
245
348
|
availableMem: freeMemory(),
|
|
349
|
+
codegen,
|
|
350
|
+
volar: {
|
|
351
|
+
pluginEntry: VOLAR_PLUGIN,
|
|
352
|
+
nodePath: process.execPath,
|
|
353
|
+
tsconfig: resolveProjectConfig(args, process.cwd()),
|
|
354
|
+
},
|
|
246
355
|
});
|
|
247
356
|
|
|
248
|
-
// Forward
|
|
249
|
-
const child = spawn(BIN_PATH,
|
|
357
|
+
// Forward the remaining arguments to the binary
|
|
358
|
+
const child = spawn(BIN_PATH, args, {
|
|
250
359
|
stdio: 'inherit',
|
|
251
360
|
shell: false,
|
|
252
361
|
env,
|
|
@@ -289,6 +398,9 @@ if (require.main === module) {
|
|
|
289
398
|
}
|
|
290
399
|
|
|
291
400
|
module.exports = {
|
|
401
|
+
parseCodegenArgs,
|
|
402
|
+
resolveProjectConfig,
|
|
403
|
+
computeCodegenWorkers,
|
|
292
404
|
computeGoMaxProcs,
|
|
293
405
|
computeGoMemLimit,
|
|
294
406
|
measureIdleCpus,
|
package/bin/vue-go-tsc.test.js
CHANGED
|
@@ -6,6 +6,9 @@ const os = require('node:os');
|
|
|
6
6
|
const path = require('node:path');
|
|
7
7
|
|
|
8
8
|
const {
|
|
9
|
+
parseCodegenArgs,
|
|
10
|
+
resolveProjectConfig,
|
|
11
|
+
computeCodegenWorkers,
|
|
9
12
|
computeGoMaxProcs,
|
|
10
13
|
computeGoMemLimit,
|
|
11
14
|
measureIdleCpus,
|
|
@@ -186,3 +189,57 @@ test('registerInstance degrades to alone when the registry is unusable', () => {
|
|
|
186
189
|
assert.strictEqual(instances, 1);
|
|
187
190
|
unregister();
|
|
188
191
|
});
|
|
192
|
+
|
|
193
|
+
test('parseCodegenArgs defaults to the Volar codegen and strips the flag', () => {
|
|
194
|
+
assert.deepStrictEqual(parseCodegenArgs(['--noEmit', '-p', 'tsconfig.json']), {
|
|
195
|
+
codegen: 'volar',
|
|
196
|
+
args: ['--noEmit', '-p', 'tsconfig.json'],
|
|
197
|
+
});
|
|
198
|
+
assert.deepStrictEqual(parseCodegenArgs(['--codegen=go', '--noEmit']), { codegen: 'go', args: ['--noEmit'] });
|
|
199
|
+
assert.deepStrictEqual(parseCodegenArgs(['--noEmit', '--codegen', 'go']), { codegen: 'go', args: ['--noEmit'] });
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test('parseCodegenArgs honors VUE_GO_TSC_CODEGEN, the flag wins', () => {
|
|
203
|
+
assert.strictEqual(parseCodegenArgs([], { VUE_GO_TSC_CODEGEN: 'go' }).codegen, 'go');
|
|
204
|
+
assert.strictEqual(parseCodegenArgs(['--codegen=volar'], { VUE_GO_TSC_CODEGEN: 'go' }).codegen, 'volar');
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test('parseCodegenArgs rejects unknown modes', () => {
|
|
208
|
+
assert.match(parseCodegenArgs(['--codegen=fast']).error, /invalid codegen mode "fast"/);
|
|
209
|
+
assert.match(parseCodegenArgs(['--codegen']).error, /invalid codegen mode/);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test('resolveProjectConfig finds the checked tsconfig', () => {
|
|
213
|
+
const isDir = (p) => !p.endsWith('.json');
|
|
214
|
+
assert.strictEqual(resolveProjectConfig(['--noEmit'], '/app', isDir), '/app/tsconfig.json');
|
|
215
|
+
assert.strictEqual(resolveProjectConfig(['-p', '.nuxt/tsconfig.shared.json'], '/app', isDir), '/app/.nuxt/tsconfig.shared.json');
|
|
216
|
+
assert.strictEqual(resolveProjectConfig(['--project', 'sub'], '/app', isDir), '/app/sub/tsconfig.json');
|
|
217
|
+
assert.strictEqual(resolveProjectConfig(['--project=/abs/tsconfig.json'], '/app', isDir), '/abs/tsconfig.json');
|
|
218
|
+
assert.strictEqual(resolveProjectConfig(['-b', 'packages/a', '--noEmit'], '/app', isDir), '/app/packages/a/tsconfig.json');
|
|
219
|
+
assert.strictEqual(resolveProjectConfig(['-b', '--noEmit'], '/app', isDir), '/app/tsconfig.json');
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('computeCodegenWorkers uses half the cores, capped, and fits the free memory', () => {
|
|
223
|
+
assert.strictEqual(computeCodegenWorkers(8, 32 * GiB), 4);
|
|
224
|
+
assert.strictEqual(computeCodegenWorkers(4, 32 * GiB), 2);
|
|
225
|
+
assert.strictEqual(computeCodegenWorkers(1, 32 * GiB), 1);
|
|
226
|
+
assert.strictEqual(computeCodegenWorkers(8, 3 * GiB), 1, '1/8 of 3 GiB fits one 200 MB worker');
|
|
227
|
+
assert.strictEqual(computeCodegenWorkers(8, 0), 4, 'unknown free memory: cores only');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test('resolveEnv wires the Volar codegen and reserves its memory', () => {
|
|
231
|
+
const volar = { pluginEntry: '/pkg/volar/plugin.mjs', nodePath: '/usr/bin/node', tsconfig: '/app/tsconfig.json' };
|
|
232
|
+
const env = resolveEnv({}, { ...machine, codegen: 'volar', volar, availableMem: 8 * GiB });
|
|
233
|
+
assert.strictEqual(env.GOLAR_PLUGIN, 'vue');
|
|
234
|
+
assert.strictEqual(env.GOLAR_PLUGIN_WORKERS, '4');
|
|
235
|
+
assert.strictEqual(env.GOLAR_VUE_PLUGIN_ENTRY, volar.pluginEntry);
|
|
236
|
+
assert.strictEqual(env.GOLAR_NODE, volar.nodePath);
|
|
237
|
+
assert.strictEqual(env.GOLAR_VUE_TSCONFIG, volar.tsconfig);
|
|
238
|
+
const goLimit = Number(resolveEnv({}, { ...machine, availableMem: 8 * GiB }).GOMEMLIMIT);
|
|
239
|
+
assert.strictEqual(Number(env.GOMEMLIMIT), goLimit - 200 * 1024 ** 2 * 5, 'base + 4 workers reserved');
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test('resolveEnv with the Go codegen never enables the plugin', () => {
|
|
243
|
+
const env = resolveEnv({ GOLAR_PLUGIN: 'vue' }, { ...machine, codegen: 'go' });
|
|
244
|
+
assert.strictEqual('GOLAR_PLUGIN' in env, false);
|
|
245
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-go-tsc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Blazingly fast Vue 3 type checker powered by typescript-go — a Go-native alternative to vue-tsc",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vue",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"bin/",
|
|
34
|
+
"volar/",
|
|
34
35
|
"install.js",
|
|
35
36
|
"README.md"
|
|
36
37
|
],
|