@native-typescript/loader-of-typescript-for-node-js 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.
Files changed (28) hide show
  1. package/README.md +64 -0
  2. package/package.json +56 -0
  3. package/source/entrypoint-of-source.js +3 -0
  4. package/source/options/index.js +1 -0
  5. package/source/options/loading/TypeScript/base/index.js +1 -0
  6. package/source/options/loading/TypeScript/base/loadTypescript.js +46 -0
  7. package/source/options/loading/TypeScript/commonjs/index.js +1 -0
  8. package/source/options/loading/TypeScript/commonjs/loadCommonjsTypescript.js +24 -0
  9. package/source/options/loading/TypeScript/index.js +2 -0
  10. package/source/options/loading/TypeScript/module/index.js +1 -0
  11. package/source/options/loading/TypeScript/module/loadModuleTypescript.js +24 -0
  12. package/source/options/loading/index.js +1 -0
  13. package/source/options/loading/load.js +27 -0
  14. package/source/options/loading/with-known-format/index.js +1 -0
  15. package/source/options/loading/with-known-format/loadWithKnownFormat.js +38 -0
  16. package/source/options/loading/with-unknown-format/extracting-extension-from-url/extractExtensionFromUrl.js +13 -0
  17. package/source/options/loading/with-unknown-format/extracting-extension-from-url/index.js +1 -0
  18. package/source/options/loading/with-unknown-format/index.js +1 -0
  19. package/source/options/loading/with-unknown-format/loadWithUnknownFormat.js +28 -0
  20. package/source/options/loading/with-unknown-format/with-extension/index.js +1 -0
  21. package/source/options/loading/with-unknown-format/with-extension/loadWithExtension.js +43 -0
  22. package/source/options/loading/with-unknown-format/with-extension/unknown-TypeScript/determining-type-of-module/determineTypeOfModule.js +46 -0
  23. package/source/options/loading/with-unknown-format/with-extension/unknown-TypeScript/determining-type-of-module/index.js +1 -0
  24. package/source/options/loading/with-unknown-format/with-extension/unknown-TypeScript/determining-type-of-module/using-readFileSync-safely/index.js +1 -0
  25. package/source/options/loading/with-unknown-format/with-extension/unknown-TypeScript/determining-type-of-module/using-readFileSync-safely/useReadFileSyncSafely.js +22 -0
  26. package/source/options/loading/with-unknown-format/with-extension/unknown-TypeScript/index.js +1 -0
  27. package/source/options/loading/with-unknown-format/with-extension/unknown-TypeScript/loadUnknownTypescript.js +34 -0
  28. package/source/options/optionsOfLoadingTypescriptHook.js +6 -0
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Loader of TypeScript for Node.js
2
+
3
+ Load `.ts`, `.mts`, and `.cts` files in Node.js by registering a custom `load` hook.
4
+
5
+ This package wires into Node's module loader (`node:module`) and strips TypeScript
6
+ types at load time, so files can run directly without a build step.
7
+
8
+ ## What this project does
9
+
10
+ - Registers a Node.js module hook via `registerHooks(...)`.
11
+ - Intercepts TypeScript files based on extension:
12
+ - `.cts` → loads as CommonJS
13
+ - `.mts` → loads as ESM
14
+ - `.ts` → auto-detects module type from nearest `package.json`
15
+ - Uses `stripTypeScriptTypes(...)` from `node:module` to remove TypeScript-only syntax.
16
+ - Passes non-TypeScript files through untouched.
17
+
18
+ ## How module type is chosen for `.ts`
19
+
20
+ For plain `.ts` files, the loader walks up directories from the file location:
21
+
22
+ - If it finds a `package.json` with `"type": "module"`, the file is treated as ESM.
23
+ - Otherwise, it is treated as CommonJS.
24
+ - If no `package.json` is found, CommonJS is used.
25
+
26
+ ## Requirements
27
+
28
+ - Node.js `>=24.14.0`
29
+ - npm `>=11.9.0`
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ npm install loader-of-typescript-for-node-js
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ### 1. Register through Node's import flag (recommended)
40
+
41
+ ```bash
42
+ node --import loader-of-typescript-for-node-js ./path/to/file.ts
43
+ ```
44
+
45
+ You can also keep it enabled globally for a session:
46
+
47
+ ```bash
48
+ export NODE_OPTIONS="--import=loader-of-typescript-for-node-js"
49
+ node ./path/to/file.ts
50
+ ```
51
+
52
+ ### 2. Register programmatically
53
+
54
+ Importing the package runs its side-effect entrypoint and registers the hook:
55
+
56
+ ```ts
57
+ import "loader-of-typescript-for-node-js";
58
+ ```
59
+
60
+ ## Notes and limitations
61
+
62
+ - This loader strips types; it does not perform full TypeScript transpilation.
63
+ - Prefer erasable TypeScript syntax compatible with `stripTypeScriptTypes(...)`.
64
+ - Keep using `tsc --noEmit` (or equivalent) for type checking in CI.
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "author": {
3
+ "email": "goodheropl@gmail.com",
4
+ "name": "Rafał Majewski",
5
+ "url": "https://github.com/rafal-majewski"
6
+ },
7
+ "bugs": {
8
+ "url": "https://github.com/native-typescript/loader-of-typescript-for-node-js/issues"
9
+ },
10
+ "description": "A loader of TypeScript for Node.js.",
11
+ "devDependencies": {
12
+ "@commitlint/cli": "20.5.0",
13
+ "@commitlint/types": "20.5.0",
14
+ "@release-it/conventional-changelog": "10.0.6",
15
+ "@stylistic/eslint-plugin": "5.10.0",
16
+ "@types/node": "25.5.0",
17
+ "@typescript-eslint/parser": "8.57.2",
18
+ "eslint": "10.2.0",
19
+ "eslint-plugin-import-x": "4.16.2",
20
+ "eslint-plugin-perfectionist": "5.8.0",
21
+ "eslint-plugin-unused-imports": "4.4.1",
22
+ "expect": "30.3.0",
23
+ "jiti": "2.6.1",
24
+ "prettier": "3.8.1",
25
+ "release-it": "19.2.4",
26
+ "testcontainers": "11.14.0",
27
+ "typescript": "5.9.3",
28
+ "typescript-eslint": "8.57.2"
29
+ },
30
+ "engines": {
31
+ "node": ">=24.14.0",
32
+ "npm": ">=11.9.0"
33
+ },
34
+ "exports": "./source/entrypoint-of-source.js",
35
+ "homepage": "https://github.com/native-typescript/loader-of-typescript-for-node-js#readme",
36
+ "name": "@native-typescript/loader-of-typescript-for-node-js",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/native-typescript/loader-of-typescript-for-node-js.git"
40
+ },
41
+ "scripts": {
42
+ "commitlint:check:last": "node --env-file-if-exists='./.env' ./node_modules/.bin/commitlint --last --strict",
43
+ "commitlint:check:range": "node --env-file-if-exists='./.env' ./node_modules/.bin/commitlint --strict",
44
+ "eslint:check": "node --env-file-if-exists='./.env' ./node_modules/.bin/eslint --max-warnings=0 .",
45
+ "eslint:fix": "node --env-file-if-exists='./.env' ./node_modules/.bin/eslint --fix --max-warnings=0 .",
46
+ "prettier:check": "node --env-file-if-exists='./.env' ./node_modules/.bin/prettier --check .",
47
+ "prettier:fix": "node --env-file-if-exists='./.env' ./node_modules/.bin/prettier --write .",
48
+ "release-it": "node --env-file-if-exists='./.env' ./node_modules/.bin/release-it --config='./release-it.config.ts'",
49
+ "test:check": "node --env-file-if-exists='./.env' --experimental-test-coverage --test --test-concurrency=1 --test-coverage-exclude='**/*.test.*' --test-coverage-exclude='node_modules/**/*' --test-coverage-exclude='testing/**/*' --test-coverage-include='source/**/*' --test-global-setup='./testing/entrypoint-of-testing.ts' '**/*.test.ts'",
50
+ "test:check:integration": "node --env-file-if-exists='./.env' --test --test-concurrency=1 --test-global-setup='./testing/entrypoint-of-testing.ts' '**/*.integration.test.ts'",
51
+ "test:check:unit": "node --env-file-if-exists='./.env' --test --test-global-setup='./testing/entrypoint-of-testing.ts' '**/*.unit.test.ts'",
52
+ "typescript:check": "node --env-file-if-exists='./.env' ./node_modules/.bin/tsc"
53
+ },
54
+ "type": "module",
55
+ "version": "1.0.0"
56
+ }
@@ -0,0 +1,3 @@
1
+ import {optionsOfLoadingTypescriptHook} from "./options/index.js";
2
+ import {registerHooks} from "node:module";
3
+ registerHooks(optionsOfLoadingTypescriptHook);
@@ -0,0 +1 @@
1
+ export {optionsOfLoadingTypescriptHook} from "./optionsOfLoadingTypescriptHook.js";
@@ -0,0 +1 @@
1
+ export {loadTypescript} from "./loadTypescript.js";
@@ -0,0 +1,46 @@
1
+ import {stripTypeScriptTypes} from "node:module";
2
+ /** @typedef {import("node:module").LoadFnOutput} LoadFnOutput */
3
+ /** @typedef {import("node:module").LoadHookContext} LoadHookContext */
4
+ /**
5
+ * @typedef {(
6
+ * urlInNextLoad: string,
7
+ * contextInNextLoad?: Partial<LoadHookContext>,
8
+ * ) => LoadFnOutput} NextLoad
9
+ */
10
+ /**
11
+ * @typedef {{
12
+ * readonly output: "commonjs",
13
+ * readonly reformattedContext: "commonjs-typescript",
14
+ * } | {
15
+ * readonly output: "module",
16
+ * readonly reformattedContext: "module-typescript",
17
+ * }} FormatsOfTypescriptLoading
18
+ */
19
+ /** @returns {LoadFnOutput} */
20
+ export function loadTypescript(
21
+ /** @type {FormatsOfTypescriptLoading} */
22
+ formats,
23
+ /** @type {string} */
24
+ url,
25
+ /** @type {LoadHookContext} */
26
+ context,
27
+ /** @type {NextLoad} */
28
+ nextLoad,
29
+ ) {
30
+ /** @type {LoadHookContext} */
31
+ const reformattedContext = {...context, format: formats.reformattedContext};
32
+ const output = nextLoad(url, reformattedContext);
33
+ if (output.source === undefined) {
34
+ return output;
35
+ } else {
36
+ const sourceOfOutputWithoutTypes = stripTypeScriptTypes(
37
+ /* eslint-disable-next-line @typescript-eslint/no-base-to-string */
38
+ output.source.toString(),
39
+ );
40
+ return {
41
+ ...output,
42
+ format: formats.output,
43
+ source: sourceOfOutputWithoutTypes,
44
+ };
45
+ }
46
+ }
@@ -0,0 +1 @@
1
+ export {loadCommonjsTypescript} from "./loadCommonjsTypescript.js";
@@ -0,0 +1,24 @@
1
+ import {loadTypescript} from "../base/index.js";
2
+ /** @typedef {import("node:module").LoadFnOutput} LoadFnOutput */
3
+ /** @typedef {import("node:module").LoadHookContext} LoadHookContext */
4
+ /**
5
+ * @typedef {(
6
+ * urlInNextLoad: string,
7
+ * contextInNextLoad?: Partial<LoadHookContext>,
8
+ * ) => LoadFnOutput} NextLoad
9
+ */
10
+ /**
11
+ * @param {string} url
12
+ * @param {LoadHookContext} context
13
+ * @param {NextLoad} nextLoad
14
+ * @returns {LoadFnOutput}
15
+ */
16
+ export function loadCommonjsTypescript(url, context, nextLoad) {
17
+ const output = loadTypescript(
18
+ {output: `commonjs`, reformattedContext: `commonjs-typescript`},
19
+ url,
20
+ context,
21
+ nextLoad,
22
+ );
23
+ return output;
24
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./commonjs/index.js";
2
+ export * from "./module/index.js";
@@ -0,0 +1 @@
1
+ export {loadModuleTypescript} from "./loadModuleTypescript.js";
@@ -0,0 +1,24 @@
1
+ import {loadTypescript} from "../base/index.js";
2
+ /** @typedef {import("node:module").LoadFnOutput} LoadFnOutput */
3
+ /** @typedef {import("node:module").LoadHookContext} LoadHookContext */
4
+ /**
5
+ * @typedef {(
6
+ * urlInNextLoad: string,
7
+ * contextInNextLoad?: Partial<LoadHookContext>,
8
+ * ) => LoadFnOutput} NextLoad
9
+ */
10
+ /**
11
+ * @param {string} url
12
+ * @param {LoadHookContext} context
13
+ * @param {NextLoad} nextLoad
14
+ * @returns {LoadFnOutput}
15
+ */
16
+ export function loadModuleTypescript(url, context, nextLoad) {
17
+ const output = loadTypescript(
18
+ {output: `module`, reformattedContext: `module-typescript`},
19
+ url,
20
+ context,
21
+ nextLoad,
22
+ );
23
+ return output;
24
+ }
@@ -0,0 +1 @@
1
+ export {load} from "./load.js";
@@ -0,0 +1,27 @@
1
+ import {loadWithKnownFormat} from "./with-known-format/index.js";
2
+ import {loadWithUnknownFormat} from "./with-unknown-format/index.js";
3
+ /** @typedef {import("node:module").LoadFnOutput} LoadFnOutput */
4
+ /** @typedef {import("node:module").LoadHookContext} LoadHookContext */
5
+ /**
6
+ * @typedef {(
7
+ * urlInNextLoad: string,
8
+ * contextInNextLoad?: Partial<LoadHookContext>,
9
+ * ) => LoadFnOutput} NextLoad
10
+ */
11
+ /** @returns {LoadFnOutput} */
12
+ export function load(
13
+ /** @type {string} */
14
+ url,
15
+ /** @type {LoadHookContext} */
16
+ context,
17
+ /** @type {NextLoad} */
18
+ nextLoad,
19
+ ) {
20
+ if (typeof context.format === `string`) {
21
+ const output = loadWithKnownFormat(url, context, nextLoad, context.format);
22
+ return output;
23
+ } else {
24
+ const output = loadWithUnknownFormat(url, context, nextLoad);
25
+ return output;
26
+ }
27
+ }
@@ -0,0 +1 @@
1
+ export {loadWithKnownFormat} from "./loadWithKnownFormat.js";
@@ -0,0 +1,38 @@
1
+ import {
2
+ loadCommonjsTypescript,
3
+ loadModuleTypescript,
4
+ } from "../TypeScript/index.js";
5
+ /** @typedef {import("node:module").LoadFnOutput} LoadFnOutput */
6
+ /** @typedef {import("node:module").LoadHookContext} LoadHookContext */
7
+ /**
8
+ * @typedef {(
9
+ * urlInNextLoad: string,
10
+ * contextInNextLoad?: Partial<LoadHookContext>,
11
+ * ) => LoadFnOutput} NextLoad
12
+ */
13
+ /** @returns {LoadFnOutput} */
14
+ export function loadWithKnownFormat(
15
+ /** @type {string} */
16
+ url,
17
+ /** @type {LoadHookContext} */
18
+ context,
19
+ /** @type {NextLoad} */
20
+ nextLoad,
21
+ /** @type {string} */
22
+ formatOfContext,
23
+ ) {
24
+ switch (formatOfContext) {
25
+ case `commonjs-typescript`: {
26
+ const output = loadCommonjsTypescript(url, context, nextLoad);
27
+ return output;
28
+ }
29
+ case `module-typescript`: {
30
+ const output = loadModuleTypescript(url, context, nextLoad);
31
+ return output;
32
+ }
33
+ default: {
34
+ const output = nextLoad(url, context);
35
+ return output;
36
+ }
37
+ }
38
+ }
@@ -0,0 +1,13 @@
1
+ /** @returns {null | string} */
2
+ export function extractExtensionFromUrl(
3
+ /** @type {string} */
4
+ url,
5
+ ) {
6
+ const indexOfLastDot = url.lastIndexOf(`.`);
7
+ if (indexOfLastDot === -1) {
8
+ return null;
9
+ } else {
10
+ const extension = url.substring(indexOfLastDot + 1);
11
+ return extension;
12
+ }
13
+ }
@@ -0,0 +1 @@
1
+ export {extractExtensionFromUrl} from "./extractExtensionFromUrl.js";
@@ -0,0 +1 @@
1
+ export {loadWithUnknownFormat} from "./loadWithUnknownFormat.js";
@@ -0,0 +1,28 @@
1
+ import {extractExtensionFromUrl} from "./extracting-extension-from-url/index.js";
2
+ import {loadWithExtension} from "./with-extension/index.js";
3
+ /** @typedef {import("node:module").LoadFnOutput} LoadFnOutput */
4
+ /** @typedef {import("node:module").LoadHookContext} LoadHookContext */
5
+ /**
6
+ * @typedef {(
7
+ * urlInNextLoad: string,
8
+ * contextInNextLoad?: Partial<LoadHookContext>,
9
+ * ) => LoadFnOutput} NextLoad
10
+ */
11
+ /** @returns {LoadFnOutput} */
12
+ export function loadWithUnknownFormat(
13
+ /** @type {string} */
14
+ url,
15
+ /** @type {LoadHookContext} */
16
+ context,
17
+ /** @type {NextLoad} */
18
+ nextLoad,
19
+ ) {
20
+ const extensionOfUrl = extractExtensionFromUrl(url);
21
+ if (extensionOfUrl === null) {
22
+ const output = nextLoad(url, context);
23
+ return output;
24
+ } else {
25
+ const output = loadWithExtension(url, context, nextLoad, extensionOfUrl);
26
+ return output;
27
+ }
28
+ }
@@ -0,0 +1 @@
1
+ export {loadWithExtension} from "./loadWithExtension.js";
@@ -0,0 +1,43 @@
1
+ import {
2
+ loadCommonjsTypescript,
3
+ loadModuleTypescript,
4
+ } from "../../TypeScript/index.js";
5
+ import {loadUnknownTypescript} from "./unknown-TypeScript/index.js";
6
+ /** @typedef {import("node:module").LoadFnOutput} LoadFnOutput */
7
+ /** @typedef {import("node:module").LoadHookContext} LoadHookContext */
8
+ /**
9
+ * @typedef {(
10
+ * urlInNextLoad: string,
11
+ * contextInNextLoad?: Partial<LoadHookContext>,
12
+ * ) => LoadFnOutput} NextLoad
13
+ */
14
+ /** @returns {LoadFnOutput} */
15
+ export function loadWithExtension(
16
+ /** @type {string} */
17
+ url,
18
+ /** @type {LoadHookContext} */
19
+ context,
20
+ /** @type {NextLoad} */
21
+ nextLoad,
22
+ /** @type {string} */
23
+ extensionOfUrl,
24
+ ) {
25
+ switch (extensionOfUrl) {
26
+ case `cts`: {
27
+ const output = loadCommonjsTypescript(url, context, nextLoad);
28
+ return output;
29
+ }
30
+ case `mts`: {
31
+ const output = loadModuleTypescript(url, context, nextLoad);
32
+ return output;
33
+ }
34
+ case `ts`: {
35
+ const output = loadUnknownTypescript(url, context, nextLoad);
36
+ return output;
37
+ }
38
+ default: {
39
+ const output = nextLoad(url, context);
40
+ return output;
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,46 @@
1
+ import {useReadFileSyncSafely} from "./using-readFileSync-safely/index.js";
2
+ import {dirname, resolve} from "node:path";
3
+ import {fileURLToPath} from "node:url";
4
+ /** @returns {"commonjs" | "module"} */
5
+ export function determineTypeOfModule(
6
+ /** @type {string} */
7
+ url,
8
+ ) {
9
+ let pathOfCurrentDirectory = dirname(fileURLToPath(url));
10
+ for (;;) {
11
+ const pathOfPackageJson = resolve(pathOfCurrentDirectory, `package.json`);
12
+ const contentOfPackageJson = useReadFileSyncSafely(pathOfPackageJson);
13
+ if (contentOfPackageJson === null) {
14
+ const pathOfParentDirectory = dirname(pathOfCurrentDirectory);
15
+ if (pathOfParentDirectory === pathOfCurrentDirectory) {
16
+ return `commonjs`;
17
+ } else {
18
+ pathOfCurrentDirectory = pathOfParentDirectory;
19
+ continue;
20
+ }
21
+ } else {
22
+ const stringOfContentOfPackageJson = new TextDecoder(`utf-8`).decode(
23
+ contentOfPackageJson,
24
+ );
25
+ const packageJson = /** @type {unknown} */ (
26
+ JSON.parse(stringOfContentOfPackageJson)
27
+ );
28
+ if (typeof packageJson === `object` && packageJson !== null) {
29
+ if (
30
+ `type` in packageJson
31
+ && typeof packageJson[`type`] === `string`
32
+ && packageJson[`type`] === `module`
33
+ ) {
34
+ return `module`;
35
+ } else {
36
+ return `commonjs`;
37
+ }
38
+ } else {
39
+ const error = new Error(
40
+ `Expected package.json to contain an object, but got ${typeof packageJson}`,
41
+ );
42
+ throw error;
43
+ }
44
+ }
45
+ }
46
+ }
@@ -0,0 +1 @@
1
+ export {determineTypeOfModule} from "./determineTypeOfModule.js";
@@ -0,0 +1,22 @@
1
+ import {readFileSync} from "node:fs";
2
+ /** @returns {null | Uint8Array<ArrayBuffer>} */
3
+ export function useReadFileSyncSafely(
4
+ /** @type {string} */
5
+ path,
6
+ ) {
7
+ try {
8
+ const content = readFileSync(path);
9
+ return content;
10
+ } catch (error) {
11
+ if (error instanceof Error) {
12
+ if (`code` in error && error.code === `ENOENT`) {
13
+ return null;
14
+ } else {
15
+ throw error;
16
+ }
17
+ } else {
18
+ const actualError = new Error(`Unknown error.`, {cause: error});
19
+ throw actualError;
20
+ }
21
+ }
22
+ }
@@ -0,0 +1 @@
1
+ export {loadUnknownTypescript} from "./loadUnknownTypescript.js";
@@ -0,0 +1,34 @@
1
+ import {
2
+ loadCommonjsTypescript,
3
+ loadModuleTypescript,
4
+ } from "../../../TypeScript/index.js";
5
+ import {determineTypeOfModule} from "./determining-type-of-module/index.js";
6
+ /** @typedef {import("node:module").LoadFnOutput} LoadFnOutput */
7
+ /** @typedef {import("node:module").LoadHookContext} LoadHookContext */
8
+ /**
9
+ * @typedef {(
10
+ * urlInNextLoad: string,
11
+ * contextInNextLoad?: Partial<LoadHookContext>,
12
+ * ) => LoadFnOutput} NextLoad
13
+ */
14
+ /** @returns {LoadFnOutput} */
15
+ export function loadUnknownTypescript(
16
+ /** @type {string} */
17
+ url,
18
+ /** @type {LoadHookContext} */
19
+ context,
20
+ /** @type {NextLoad} */
21
+ nextLoad,
22
+ ) {
23
+ const typeOfModule = determineTypeOfModule(url);
24
+ switch (typeOfModule) {
25
+ case `commonjs`: {
26
+ const output = loadCommonjsTypescript(url, context, nextLoad);
27
+ return output;
28
+ }
29
+ case `module`: {
30
+ const output = loadModuleTypescript(url, context, nextLoad);
31
+ return output;
32
+ }
33
+ }
34
+ }
@@ -0,0 +1,6 @@
1
+ import {load} from "./loading/index.js";
2
+ /** @typedef {import("node:module").RegisterHooksOptions} RegisterHooksOptions */
3
+ /** @satisfies {RegisterHooksOptions} */
4
+ export const optionsOfLoadingTypescriptHook = /** @type {const} */ ({
5
+ load: load,
6
+ });