gitea, minio: compile the sources beside themselves in the test script, as the old check did (issue 1891)
The resolve hook needed the typescript package, which mesh-build-node keeps out of the workspace (notCopied): tsc is reached only as the bin/tsc command. So the test scripts now do what merge-check-typescript.sh did before the tests: tsc over the module's sources with --outDir ., so the sources' ./x.js imports resolve. The emitted .js is ignored by git.
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# The test script compiles the sources beside themselves before the tests import them (package.json, issue 1891).
|
||||
*.js
|
||||
@@ -6,7 +6,7 @@
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "tsc client.ts token.ts index.ts provisioner/index.ts provisioner/teams.ts tools/index.ts --module NodeNext --moduleResolution NodeNext --target ES2022 --outDir dist",
|
||||
"test": "npm run build && node --import ./test/resolve-ts.mjs --test --experimental-strip-types 'test/*.test.ts'"
|
||||
"test": "npm run build && tsc --module NodeNext --moduleResolution NodeNext --target ES2022 --types node --rootDir . --outDir . $(find . -name '*.ts' ! -name '*.d.ts' ! -path './test/*' ! -path './node_modules/*' ! -path './dist/*') && node --test --experimental-strip-types 'test/*.test.ts'"
|
||||
},
|
||||
"dependencies": {
|
||||
"@novox/mesh-sdk": "^0.1.1"
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
// The tests import the module's TypeScript sources (node --experimental-strip-types), and the sources import each other
|
||||
// by the .js name tsc compiles them to (NodeNext). Nothing compiles them beside themselves before the tests run: the
|
||||
// Node build module runs `tsc -p .` and then `npm test` (novox/hq issue 1891). So this hook resolves a relative .js import
|
||||
// from a .ts file, when no such .js is there, to the .ts beside it, and compiles that file with the toolchain's own
|
||||
// TypeScript (transpileModule: no type-check, which `tsc -p .` did), since a source may use what type stripping refuses
|
||||
// (a parameter property). Every other import resolves and loads as Node does.
|
||||
import { readFileSync } from "node:fs";
|
||||
import { registerHooks } from "node:module";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import ts from "typescript";
|
||||
|
||||
const compiled = new Set();
|
||||
|
||||
registerHooks({
|
||||
resolve(specifier, context, nextResolve) {
|
||||
try {
|
||||
return nextResolve(specifier, context);
|
||||
} catch (err) {
|
||||
const relative = specifier.startsWith("./") || specifier.startsWith("../");
|
||||
if (err?.code !== "ERR_MODULE_NOT_FOUND" || !relative || !specifier.endsWith(".js") || !context.parentURL?.endsWith(".ts")) {
|
||||
throw err;
|
||||
}
|
||||
const resolved = nextResolve(specifier.slice(0, -".js".length) + ".ts", context);
|
||||
compiled.add(resolved.url);
|
||||
return resolved;
|
||||
}
|
||||
},
|
||||
load(url, context, nextLoad) {
|
||||
if (!compiled.has(url)) return nextLoad(url, context);
|
||||
const file = fileURLToPath(url);
|
||||
const out = ts.transpileModule(readFileSync(file, "utf8"), {
|
||||
fileName: file,
|
||||
compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022, sourceMap: false },
|
||||
});
|
||||
return { format: "module", source: out.outputText, shortCircuit: true };
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
# The test script compiles the sources beside themselves before the tests import them (package.json, issue 1891).
|
||||
*.js
|
||||
@@ -5,7 +5,7 @@
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"test": "node --import ./test/resolve-ts.mjs --test --experimental-strip-types 'test/*.test.ts'"
|
||||
"test": "tsc --module NodeNext --moduleResolution NodeNext --target ES2022 --types node --rootDir . --outDir . $(find . -name '*.ts' ! -name '*.d.ts' ! -path './test/*' ! -path './node_modules/*' ! -path './dist/*') && node --test --experimental-strip-types 'test/*.test.ts'"
|
||||
},
|
||||
"dependencies": {
|
||||
"@novox/mesh-sdk": "^0.1.7"
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
// The tests import the module's TypeScript sources (node --experimental-strip-types), and the sources import each other
|
||||
// by the .js name tsc compiles them to (NodeNext). Nothing compiles them beside themselves before the tests run: the
|
||||
// Node build module runs `tsc -p .` and then `npm test` (novox/hq issue 1891). So this hook resolves a relative .js import
|
||||
// from a .ts file, when no such .js is there, to the .ts beside it, and compiles that file with the toolchain's own
|
||||
// TypeScript (transpileModule: no type-check, which `tsc -p .` did), since a source may use what type stripping refuses
|
||||
// (a parameter property). Every other import resolves and loads as Node does.
|
||||
import { readFileSync } from "node:fs";
|
||||
import { registerHooks } from "node:module";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import ts from "typescript";
|
||||
|
||||
const compiled = new Set();
|
||||
|
||||
registerHooks({
|
||||
resolve(specifier, context, nextResolve) {
|
||||
try {
|
||||
return nextResolve(specifier, context);
|
||||
} catch (err) {
|
||||
const relative = specifier.startsWith("./") || specifier.startsWith("../");
|
||||
if (err?.code !== "ERR_MODULE_NOT_FOUND" || !relative || !specifier.endsWith(".js") || !context.parentURL?.endsWith(".ts")) {
|
||||
throw err;
|
||||
}
|
||||
const resolved = nextResolve(specifier.slice(0, -".js".length) + ".ts", context);
|
||||
compiled.add(resolved.url);
|
||||
return resolved;
|
||||
}
|
||||
},
|
||||
load(url, context, nextLoad) {
|
||||
if (!compiled.has(url)) return nextLoad(url, context);
|
||||
const file = fileURLToPath(url);
|
||||
const out = ts.transpileModule(readFileSync(file, "utf8"), {
|
||||
fileName: file,
|
||||
compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022, sourceMap: false },
|
||||
});
|
||||
return { format: "module", source: out.outputText, shortCircuit: true };
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user