此软件包已移动到 自动。这是一个遗留版本.
自动
自动 是一个"工作出盒"库,用于执行Typescript (以及现代Javascript并带有装饰器等)而无需使用捆绑器. 用于快速演示或启动您用 Typescript 写的测试 .
这个图书馆是薄薄的包装 兴建 它几乎立即编译出字典.
运行打字机的难度在于处理依赖性. 例如,您可能需要导入其他 Typescript 文件,但也需要导入用 Javascript 书写的库,并使用 CJS 或 ESM 格式. 所有这些使用案例都应予以考虑.
自动 能够处理所有烦人的东西 使事情如你所期望的那样运转.
使用量
全球安装
用您最喜欢的软件包管理器在全球安装库 :
npm i -g esrun
然后,您可以同样的方式执行任何 Typescript文件 :
esrun foo.ts
您可以像任何进程一样通过参数 :
esrun foo.ts --option=bar --verbose -S
所有文件的依赖性也会被捆绑和执行.
外部模块的依赖性不会被捆绑起来,这取决于 node 解决依赖关系的引擎.
当地安装
在本地安装您最喜欢的软件包管理器 .
npm i -D esrun
那你可以用在你的体内 package.json 脚本 :
{
"scripts": {
"test": "esrun test"
}
}
运行 npm run test 将运行以下列表中的第一个文件 :
/test.ts/test/index.ts/test/test.ts/test/main.ts/test.js/test/index.js/test/test.js/test/main.js
CLI 参数语法
有两种参数可以传递给esrun cli:
- 无效参数,这将影响如何执行您的 Typescript 文件,
- 财务报告和审定财务报表 程序参数,将转成 进程.argv 到要执行的文件.
无效参数 遵循与节点相同的语法惯例 :
- 它必须从双连字符" -"开始
- 之后是参数的名称
- 如果一个值是必需的,则使用一个等号"=",然后是该值,没有空格.
示例
esrun --tsconfig=/path/to/tsconfig.json myFileToExecute.ts
所有参数 之后 要执行的文件是 程序参数 并将通过 process.argv。 。 。 .
在这个例子中, "foo" 财务报告和审定财务报表 "bar" 将发送至 myFileToExecute数字 :
esrun myFileToExecute.ts foo bar
自定义 tsconfig.json
您可以通过自定义路径到您的 tsconfig.json 来自 CLI 的文件 :
esrun --tsconfig=/custom/path/to/tsconfig.json foo.ts
顶级等待
埃斯伦 与顶级等待兼容.
要启用顶级等待, 您需要设置选项 "module": "esnext" 在你的tsconfig.json中.
监视模式
您也可以执行 自动 在监视模式中.
在监视模式下,您的文件会在每次更新自身或其中一个依赖时自动被重新执行.
esrun --watch foo.ts
这个
--watch(或-w) 选项必须放在要执行的文件路径之前. 如果您将其放置在文件路径后, 它将被传递为参数foo.ts换句话说.
这个功能在您进行测试驱动开发时非常有用. 你们可以跑 esrun --watch test.ts 并享受您在控制台上的 变化的现场输出.
您可能想要观看除代码文件之外的其他文件 。 例如,如果从配置文件中装入数据。 在这种情况下,您可以指定一个需要观看的光子(或光子列表):
esrun --watch=src/*.json foo.ts
随便 json 文档中 src/文件夹将重新触发运行 .
您可以使用由逗号分隔的多个缩写( 但无空格 ) :
esrun --watch=src/*.json,test/*.json foo.ts
防止控制台清理
在监视模式下,每个文件更改都会触发控制台清除. 您可以禁用此行为 --preserveConsole 选项 :
esrun --watch --preserveConsole foo.ts
检查模式
您也可以执行 自动 检查模式.
在检查模式下运行时,您的代码会被连接到Webkit DevTools,以受益浏览器控制台的功率,而不是终端控制台.
首先,以检查模式运行您的程序 :
esrun --inspect foo.ts
然后打开 about:inspect 在一个 颜色 编号 : 勇敢点 编号 : 边缘 浏览器. 你应该看看你的节目在 远程目标 节.
点击 Open dedicated DevTools for Node 并享受您的后端程序的浏览器控制台.
如果遇到麻烦,请读一下 节点文档。 。 。 .
检查和监视模式尚未兼容 .
Sudo 模式
您可以以 sudo 模式运行脚本 :
esrun --sudo myScript.ts
其他节点 cli 选项
esrun 使用 esbuilt 将 Typescript 转换为 Javascript, 然后由节点执行 .
您可以通过将选项名称前缀为"-node"来将自定义选项传递给节点 cli, 像这样:
esrun --node-max-old-space-size=4096 foo.ts
esrun --node-no-warnings foo.ts
导入 CJS 模块
如果导入 CJS 模块( 如 typescript 库本身),很可能需要设置 eModule Interop 服务器 在您的旗帜 tsconfig.json 文件 :
{
"compilerOptions": {
"esModuleInterop": true
}
}
这将抑制 Typescript 编译器的导入错误, 并允许您写入 import ts from "typescript" 改为 import * as ts from "typescript" - 最新的语法不是标准的无害环境管理.
使用目录作为切入点
如果给定的出入口是一个目录,为了找到正确的出入口文件,将执行以下动作:
- 检查一个 package.json 文件是否存在
main字段。 条目文件将是main字段,相对于 package.json 目录. - 检查是否为
index.ts文件存在于给定目录中. - 检查给定目录中是否存在一个eponym文件.
- 检查是否带有该词的缩写文件
.ts扩展存在于给定的目录中. - 检查是否为
main.ts文件存在于给定目录中. - 检查是否为
index.js文件存在于给定目录中. - 检查是否带有该词的缩写文件
.js扩展存在于给定的目录中. - 检查是否为
main.js文件存在于给定目录中.
API 密码
库导出一个函数,用于程序上执行 Typescript 文件.
import esrun from 'esrun'
export async function esrun(filePath: string, options?: Options): Promise<void>
export type Options = {
// arguments to pass to the script
args?: string[] = []
// if true, will reload the script on file changes
// you can also pass an additional array of globs to watch
watch?: boolean | string[] = false
// if true, prevent console clearing on watch mode
preserveConsole?: boolean
// if true, turn on inspect mode to use browser's console
inspect?: boolean = false
// if false, do not transform __dirname and __filename
// (the CLI option to disable file constants is --noFileConstants)
fileConstants?: boolean = true
// if false, external packages will be bundled
makeAllPackagesExternal?: boolean = true
// if false, process.exit() won't be called after execution
exitAfterExecution?: boolean = true
// enable use of process.send() from the children
interProcessCommunication?: boolean = false
// additional options to pass to node's cli
nodeOptions?: Record<string, Parameter> = {}
// indicate the mode to use to run code
// "cliParameters" means the code is passed to the node program
// as parameters
// "temporaryFile" means a temporary file is created inside
// the node_modules folder, then executed, then deleted
sendCodeMode?: "cliParameters" | "temporaryFile"
// executed before the code is executed (after the build)
beforeRun?: () => unknown
// executed after the code is executed
afterRun?: () => unknown
}
创建新运行程序以获取/ 转换生成的代码
要完全控制,您可以创建自己的脚本运行例 :
import { Runner } from 'esrun'
const runner = new Runner(inputFile: string, options?: Options)
// build the given file and all its dependencies
await runner.build(buildOptions?: BuildOptions)
// you can see what the generated code is
console.log("Generated javascript code:", runner.outputCode)
// you can apply transformations to the code
await runner.transform(code => `console.log('Hello world!');\n` + code)
// then execute the build and return the given status
const status = await runner.execute()
接收数据
您可以打开选项从执行的脚本接收数据 interProcessCommunication。 。 。 .
当选项打开时,脚本可以调用 process.send(消息:字符串)。 。 。 .
执行结束时,发送的信息将通过 runner.output。 。 。 .
假设你有以下文件 helloWorld.ts数字 :
// helloWorld.ts
process.send('Hello world')
然后,您可以这样接收数据fro这个脚本:
const runner = new Runner('helloWorld.ts', { interProcessCommunication: true })
await runner.execute({ exitAfterExecution: false })
console.log("Received data:", runner.output)
// should log 'Received data: Hello world'
更改日志
已有更改日志 这里。 。 。 .