npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@asnc/ts_hook

v0.2.1

Published

Run ts file in node

Downloads

36

Readme

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/register | ts-node | | -------------------------------------------------------- | ----------------------- | ------------------------ | ---------- | | Dependencies | typescript or @swc/core | typescript and @swc/core | typescript | | Type check | No | No | Yes | | Source map | Yes | Yes | Yes | | [Cross-package import](#Cross-package import) ts project | Yes | No | No | | Mix esm and commonjs | Yes | | Yes | | [Path alias](#Path alias) | Yes | Yes in some cases | No |

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}"
        }
    ]
}