简体   繁体   English

如何使用 Yarn 将 TypeScript 转换为 JavaScript?

[英]How do I transpile TypeScript to JavaScript with Yarn?

How do I transpile TypeScript code in JavaScript in a project managed by Yarn?如何在 Yarn 管理的项目中编译 JavaScript 中的 TypeScript 代码?

I need Javascript code in order to run it with other compilers (such as GraalVM) and investigate performance.我需要 Javascript 代码才能与其他编译器(例如 GraalVM)一起运行并调查性能。

You can do it with by adding a build rule to your package.json.您可以通过向 package.json 添加构建规则来实现。

{
  "name": "my-package",
  "scripts": {
    "build": "tsc -p .",
  }
}

Replace .更换 with the path to your tsconfig.json file which, for example, contains the following lines.带有 tsconfig.json 文件的路径,例如,该文件包含以下行。

{
    "compilerOptions": {
      "target": "ES2019",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
      "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
       "outDir": "./dist",                      /* Redirect output structure to the directory. */
      "strict": true,                           /* Enable all strict type-checking options. */
      "strictNullChecks": true,                 /* Enable strict null checks. */
      "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
    }
}

Run yarn run build .运行yarn run build The transpiled JavaScript files will be in the./dist directory.编译后的 JavaScript 文件将位于 ./dist 目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM