Skip to content

Commit

Permalink
Merge pull request #12 from ieavid/develop
Browse files Browse the repository at this point in the history
fix: Tow bugs
  • Loading branch information
Eavid authored Sep 17, 2023
2 parents ae89197 + 36d2f95 commit b259d87
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 26 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ This hook is primarily used to run typescript directly in the development enviro
Global install: `npm install @asnc/ts_hook -g`\
Project install: `npm install @asnc/ts_hook -D`

Usage: `node --loader MODULE_PATH/@asnc/ts_hook xx.ts`\
Usage: `node --loader MODULE_PATH/@asnc/ts_hook xx.mts`\
Usage: `node --loader MODULE_PATH/@asnc/ts_hook xx.cts`\
Usage: `node --loader @asnc/ts_hook xx.cts` (Project level use)\
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 using an absolute path, Windows must start with a `/`
> Example `/C:/npm_global/@asnc/ts_hook.mjs`, when using a relative path, relative to the node's startup directory, and must start with `./`
> 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
Expand Down Expand Up @@ -128,7 +127,7 @@ vscode debug configuration, you can inject environment variables
"type": "node",
"request": "launch",
"name": "ts debugger",
"runtimeArgs": ["--loader", "./node_modules/@asnc/ts_hook"], //Use loader
"runtimeArgs": ["--loader", "./node_modules/@asnc/ts_hook/hook.mjs"], //Use loader
//Injected environment variable
"env": {
"SAME_PARSER": "" //Set "" which is true
Expand Down
13 changes: 6 additions & 7 deletions docs/zh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
全局安装: `npm install @asnc/ts_hook -g`\
项目安装: `npm install @asnc/ts_hook -D`

用法: `node --loader MODULE_PATH/@asnc/ts_hook xx.ts`\
用法: `node --loader MODULE_PATH/@asnc/ts_hook xx.mts`\
用法: `node --loader MODULE_PATH/@asnc/ts_hook xx.cts`\
用法: `node --loader @asnc/ts_hook xx.cts` (项目级使用\
用法: `node --loader MODULE_PATH/@asnc/ts_hook/hook.mjs xx.ts`\
用法: `node --loader MODULE_PATH/@asnc/ts_hook/hook.mjs xx.mts`\
用法: `node --loader MODULE_PATH/@asnc/ts_hook/hook.mjs xx.cts`\
用法: `node --loader @asnc/ts_hook xx.cts` (项目级使用, 不需要指定入口文件"hook.mjs")

> 注意: `MODULE_PATH`是你安装`@asnc/ts_hook`的所在目录的绝对或相对路径, 使用绝对路径时, Windows 系统下必须以`/`开头
> `/C:/npm_global/@asnc/ts_hook`, 使用相对路径时, 相对于 node 的启动目录, 且必须以`./`开头
> 注意: `MODULE_PATH`是你安装`@asnc/ts_hook`的所在目录的绝对或相对路径. 全局使用时, 必须是指定绝对路径, 并且是指定到模块的入口文件。Windows 系统下必须以`/`开头,如 `/C:/npm_global/@asnc/ts_hook/hook.mjs`
> 如果使用全局安装`ts_hook`, 你需要全局安装 typescript 或 @swc/core 其中的一个依赖, 如果是项目安装`ts_hook`, 你需要在项目中安装 typescript 或 @swc/core, ts_hook 依赖他们, ts_hook 优先尝试导入 @swc/core, 如果导入失败, 才会尝试导入 typescript, 你可以在通过配置进行选择
Expand Down Expand Up @@ -126,7 +125,7 @@ vscode 调试配置, 可以注入环境变量
"type": "node",
"request": "launch",
"name": "ts debugger",
"runtimeArgs": ["--loader", "./node_modules/@asnc/ts_hook"], //使用loader
"runtimeArgs": ["--loader", "./node_modules/@asnc/ts_hook/hook.mjs"], //使用loader
//注入环境变量
"env": {
"SAME_PARSER": "" //设置"" 即false
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asnc/ts_hook",
"version": "0.2.0",
"version": "0.2.1",
"description": "Run ts file in node",
"scripts": {
"build": "tsc -p ./tsconfig.build.json",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/hook_config.cts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ interface ProcessEnv {
TS_COMPILER_OPTIONS?: string;
TS_CONFIG_PATH?: string;
COMPILER?: "swc" | "tsc";
DISABLE_SOURCE_MAP?: string;
}
const env = process.env as ProcessEnv;
if (env.COMPILER !== "swc" && env.COMPILER !== "tsc") delete env.COMPILER;
export const hookConfig = {
sameParsing: !!env["SAME_PARSER"],
compiler: env.COMPILER,
enableSourceMap: !env["DISABLE_SOURCE_MAP"],
};

export type HookConfig = typeof hookConfig;
Expand Down Expand Up @@ -41,7 +43,7 @@ const DEFAULT_COMPILER_OPTIONS = getDefaultCompilerOptions();
const TOP_OPTIONS: ts.CompilerOptions = {
removeComments: true,
sourceMap: false,
inlineSourceMap: true,
inlineSourceMap: hookConfig.enableSourceMap,
noEmit: false,
allowSyntheticDefaultImports: true,
declaration: false,
Expand Down
4 changes: 4 additions & 0 deletions src/internal/common_loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ function parseAlias(instance: TsCompilerConfig, alias: string) {
}
}
describe("别名解析", function () {
it("全匹配", function () {
let tsConfig = createInstance({ paths: { "@/abc": ["./src/abc"] } });
expect(parseAlias(tsConfig, "@/abc")).toBe(Path.resolve(dir, "./src/abc"));
});
it("无base", function () {
let tsConfig = createInstance({ paths: { "@/*": ["./src/*"] } });
expect(parseAlias(tsConfig, "@/y")).toBe(Path.resolve(dir, "./src/y"));
Expand Down
3 changes: 1 addition & 2 deletions src/internal/common_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Module from "node:module";
import { upSearch } from "../util/file_tool";
import type * as ts from "typescript";
import * as Path from "node:path";
import { pickObjectAttr } from "../lib/object";
import type { PackageConfig } from "./pkg";
import { readTsConfigFile, readTsConfigFileSync } from "../util/tslib";

Expand Down Expand Up @@ -120,7 +119,7 @@ export class TsCompilerConfig {
if (!vab) continue;
yield Path.resolve(this.pkgPath, map.replace(/\*/g, vab));
} else {
return maps;
yield map;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/internal/esm_loader.mts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ export async function tryResolvePathMod(
}
if (!resolvedAbsPath) return;

let format: NodeLoader.Format =
ExtraModule._readPackage(resolvedAbsPath!)?.type === "module" ? "module" : "commonjs";
const url = pathToFileURL(resolvedAbsPath).toString();
let format: NodeLoader.Format = (await getTsModuleKind(url)) ?? "commonjs";
return {
url: pathToFileURL(resolvedAbsPath).toString(),
url,
format,
shortCircuit: true,
};
Expand Down
1 change: 1 addition & 0 deletions test/tests/基础导入/exports/mod_esm.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
process.send?.("imported mts");
console.log(import.meta as any);
export const fn = () => {};
export {};
2 changes: 2 additions & 0 deletions test/tests/基础导入/完整扩展名导入.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
async function imports() {
await import("./exports/mod_ts.js");
await import("./exports/mod_cts.cts");
await import("./exports/mod_cts.cjs");
await import("./exports/mod_esm.mts");
await import("./exports/mod_esm.mjs");

console.log(import.meta as any);
Expand Down

0 comments on commit b259d87

Please sign in to comment.