0.2.1 • Published 8 months ago

@asnc/ts_hook v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

English | 中文

ts_hook

This hook is primarily used to run typescript directly in the development environment, eliminating the compilation step

Required node version >= 16

Global install: npm install @asnc/ts_hook -g\ Project install: npm install @asnc/ts_hook -D

Usage: node --loader MODULE_PATH/@asnc/ts_hook/hook.mjs xx.ts\ Usage: node --loader MODULE_PATH/@asnc/ts_hook/hook.mjs xx.mts\ Usage: node --loader MODULE_PATH/@asnc/ts_hook/hook.mjs xx.cts\ Usage: node --loader @asnc/ts_hook xx.cts (For project-level use, there is no need to specify an entry file "hook.mjs")\

Note: MODULE_PATH is the absolute or relative path to the directory where you installed @asnc/ts_hook. When used globally, the absolute path must be specified and the entry file to the module must be specified. Windows must start with a /. Example /C:/npm_global/@asnc/ts_hook.mjs

If you use a global installation @asnc/ts_hook, you need to install one of the dependencies of typescript or @swc/core globally. If you use a project installation @asnc/ts_hook, you need to install typescript or @swc/core in your project. ts_hook relies on them. @asnc/ts_hook tries to import @swc/core first. If the import fails, it will try to import typescript

Compare with ts-node and @swc-node/register

@asnc/ts_hook@swc-node/registerts-node
Dependenciestypescript or @swc/coretypescript and @swc/coretypescript
Type checkNoNoYes
Source mapYesYesYes
Cross-package import(#Cross-package import) ts projectYesNoNo
Mix esm and commonjsYesYes
Path alias(#Path alias)YesYes in some casesNo

Cross-package import

In the monorepo project, we develop multiple packages in Typescript at the same time, and they may reference each other When using a Node debugger package in this scenario, we must compile the package with the packages in the project on which it depends while If you use ts-node or @swc-node/register, you only need to compile the package on which it depends, because their hooks don't compile on external dependencies And this is very inconvenient So @asnc/ts_hook solves this problem. @asnc/ts_hook tracks the import of all.ts .mts .cts files

Mix esm and commonjs

The following directories are available:

work
- index.mts
- esm.mts
- cjs.tsc

index.mts:

import "./cjs.cjs";
import "./esm.mjs";
console.log(__dirname);

@swc-node/register cannot resolve this full path import

Path alias

In many cases we set the paths field in tsconfig.json @swc-node/register only looks for the paths field of the tsconfig.json file in the working directory at run time. If the working directory is not in the project at run time, this means that @swc-node/register cannot read the alias configuration. Unless shown setting alias

@asnc/ts_hooks support alias configuration in project-level tsconfig.json (needs to be in the same directory as package.json)

ts-hook features

  • Type checking is not provided
  • Optional tsc and swc compiler, try to import swc compiler first
  • Supports the imports, exports, and main fields of package.json
    • If the file id parsed through these fields ends with the extension .js .mjs .cjs and the file does not exist, the extension will be replaced with .ts .mts .cts and the import attempt will be made The module compilation option for -ts is fixed to NodeNext
  • Supports source mapping
  • ES Module can use the same parsing strategy as commonjs

Configuration

The configuration needs to be injected from environment variables

For boolean type: Set "" to false and all other values to true

SAME_PARSER: Does ES Module use the same parsing strategy as Commonjs

Type: boolean default false When enabled, ES Module will use the same module resolution strategy as Commonjs

TS_COMPILER_OPTIONS: Global ts file compilation configuration

Type: string

Note: Some configurations are overwritten

Example: vscode debugger configuration

{
    "configurations": [
        {
            "type": "node",
            "env": {    //Injected environment variable
                "TS_COMPILER_OPTIONS": "{\"target\":\"esnext\"}",
            },
            ...
        }
    ]
}
TS_CONFIG_PATH: Global ts file compiled configuration file path

Type: string

Note: Some configurations are overwritten

Configuration example

vscode debug configuration, you can inject environment variables

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "ts debugger",
            "runtimeArgs": ["--loader", "./node_modules/@asnc/ts_hook/hook.mjs"], //Use loader
            //Injected environment variable
            "env": {
                "SAME_PARSER": "" //Set "" which is true
            },
            "sourceMaps": true,
            "program": "${file}"
        }
    ]
}
0.2.1

8 months ago

0.1.2

11 months ago

0.2.0

8 months ago

0.1.4

10 months ago

0.1.3

11 months ago

0.1.5

10 months ago

0.1.1

11 months ago

0.1.0

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago