ts-arc 1.0.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/dist/bin.js +19 -0
- package/dist/loader.js +50 -0
- package/package.json +25 -0
package/dist/bin.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { register } from "node:module";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import * as url from "node:url";
|
|
4
|
+
const __filename = url.fileURLToPath(import.meta.url);
|
|
5
|
+
const __dirname = path.dirname(__filename);
|
|
6
|
+
const loaderPath = path.join(__dirname, "loader.js");
|
|
7
|
+
register(loaderPath, url.pathToFileURL(__dirname).href);
|
|
8
|
+
const script = process.argv[2];
|
|
9
|
+
if (!script) {
|
|
10
|
+
console.error("Usage: node bin.js <script.ts> [args...]");
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
const scriptPath = path.resolve(script);
|
|
14
|
+
const scriptUrl = url.pathToFileURL(scriptPath).href;
|
|
15
|
+
process.argv = [process.argv[0], script, ...process.argv.slice(3)];
|
|
16
|
+
import(scriptUrl).catch((err) => {
|
|
17
|
+
console.error(err);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
});
|
package/dist/loader.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as url from "url";
|
|
3
|
+
import { transformSync } from "esbuild";
|
|
4
|
+
async function resolve(specifier, context, nextResolve) {
|
|
5
|
+
try {
|
|
6
|
+
const resolved = await nextResolve(specifier, context);
|
|
7
|
+
return resolved;
|
|
8
|
+
} catch (error) {
|
|
9
|
+
if (error.code !== "ERR_MODULE_NOT_FOUND") {
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
const resolved = await nextResolve(specifier + ".ts", context);
|
|
15
|
+
return { ...resolved, shortCircuit: true };
|
|
16
|
+
} catch (error) {
|
|
17
|
+
if (error.code !== "ERR_MODULE_NOT_FOUND") {
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const resolved = await nextResolve(specifier + "/index.ts", context);
|
|
23
|
+
return { ...resolved, shortCircuit: true };
|
|
24
|
+
} catch (error) {
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function load(urlStr, context, nextLoad) {
|
|
29
|
+
if (urlStr.endsWith(".ts")) {
|
|
30
|
+
const filePath = url.fileURLToPath(urlStr);
|
|
31
|
+
const rawSource = fs.readFileSync(filePath, "utf8");
|
|
32
|
+
const { code } = transformSync(rawSource, {
|
|
33
|
+
loader: "ts",
|
|
34
|
+
format: "esm",
|
|
35
|
+
target: `node${process.versions.node}`,
|
|
36
|
+
sourcemap: "inline",
|
|
37
|
+
sourcefile: filePath
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
format: "module",
|
|
41
|
+
source: code,
|
|
42
|
+
shortCircuit: true
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return nextLoad(urlStr, context);
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
load,
|
|
49
|
+
resolve
|
|
50
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ts-arc",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "A simple typescript runtime.",
|
|
6
|
+
"main": "dist/bin.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"ts-arc": "./dist/bin.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "npx esbuild ./src/loader.ts ./src/bin.ts --outdir=./dist --platform=node --format=esm"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^24.9.1"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"esbuild": "^0.25.11"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
]
|
|
25
|
+
}
|