From 787a4c940c97a8bc60d5de5ba1721ab252be0419 Mon Sep 17 00:00:00 2001 From: EnochGao Date: Thu, 20 Oct 2022 15:31:22 +0800 Subject: [PATCH 1/2] feat: translate Integrating with Build Tools.md in zh-CN --- .../Integrating with Build Tools.md | 286 ++++++++++++++++++ 1 file changed, 286 insertions(+) create mode 100644 docs/documentation/zh/project-config/Integrating with Build Tools.md diff --git a/docs/documentation/zh/project-config/Integrating with Build Tools.md b/docs/documentation/zh/project-config/Integrating with Build Tools.md new file mode 100644 index 00000000..a9b21292 --- /dev/null +++ b/docs/documentation/zh/project-config/Integrating with Build Tools.md @@ -0,0 +1,286 @@ +--- +title: 与构建工具集成 +layout: docs +permalink: /zh/docs/handbook/integrating-with-build-tools.html +oneline: 如何将TypeScript与其他构建工具一起使用 +translatable: true +--- + +## Babel + +### 安装 + +```sh +npm install @babel/cli @babel/core @babel/preset-typescript --save-dev +``` + +### .babelrc + +```js +{ + "presets": ["@babel/preset-typescript"] +} +``` + +### 命令行执行 + +```sh +./node_modules/.bin/babel --out-file bundle.js src/index.ts +``` + +### package.json + +```js +{ + "scripts": { + "build": "babel --out-file bundle.js main.ts" + }, +} +``` + +### 命令行执行 Babel + +```sh +npm run build +``` + +## Browserify + +### 安装 + +```sh +npm install tsify +``` + +### 命令行执行 + +```sh +browserify main.ts -p [ tsify --noImplicitAny ] > bundle.js +``` + +### 使用 API + +```js +var browserify = require("browserify"); +var tsify = require("tsify"); + +browserify() + .add("main.ts") + .plugin("tsify", { noImplicitAny: true }) + .bundle() + .pipe(process.stdout); +``` + +更多细节: [smrq/tsify](https://github.com/smrq/tsify) + +## Grunt + +### 安装 + +```sh +npm install grunt-ts +``` + +### 基础 Gruntfile.js + +```js +module.exports = function (grunt) { + grunt.initConfig({ + ts: { + default: { + src: ["**/*.ts", "!node_modules/**/*.ts"], + }, + }, + }); + grunt.loadNpmTasks("grunt-ts"); + grunt.registerTask("default", ["ts"]); +}; +``` + +更多细节: [TypeStrong/grunt-ts](https://github.com/TypeStrong/grunt-ts) + +## Gulp + +### 安装 + +```sh +npm install gulp-typescript +``` + +### 基础 gulpfile.js + +```js +var gulp = require("gulp"); +var ts = require("gulp-typescript"); + +gulp.task("default", function () { + var tsResult = gulp.src("src/*.ts").pipe( + ts({ + noImplicitAny: true, + out: "output.js", + }) + ); + return tsResult.js.pipe(gulp.dest("built/local")); +}); +``` + +更多细节: [ivogabe/gulp-typescript](https://github.com/ivogabe/gulp-typescript) + +## Jspm + +### 安装 + +```sh +npm install -g jspm@beta +``` + +注意:目前 jspm 中支持TypeScript版本是 0.16beta + +更多细节: [TypeScriptSamples/jspm](https://github.com/Microsoft/TypeScriptSamples/tree/master/jspm) + +## MSBuild + +更新项目文件,包括本地安装`Microsoft.TypeScript.Default.props` (顶部) 和 `Microsoft.TypeScript.targets` (底部): + +```xml + + + + + + + + false + true + + + true + false + + + + + +``` + +有关定义 MSBuild 编译器选项的更多详细信息: [在 MSBuild 项目中设置编译器选项](/docs/handbook/compiler-options-in-msbuild.html) + +## NuGet + +- 右键单击 -> 管理 NuGet 包 +- 搜索 `Microsoft.TypeScript.MSBuild` +- 点击 `Install` +- 安装完成后重新构建! + +更多详细信息可以查看 [Package Manager Dialog](http://docs.nuget.org/Consume/Package-Manager-Dialog) 和 [using nightly builds with NuGet](https://github.com/Microsoft/TypeScript/wiki/Nightly-drops#using-nuget-with-msbuild) + +## Rollup + +### 安装 + +``` +npm install @rollup/plugin-typescript --save-dev +``` + +请注意,`typescript`和`tslib`都是此插件的对等依赖项(peerDependencies),需要单独安装。 + +### 使用 + +创建一个 `rollup.config.js` [配置文件](https://www.rollupjs.org/guide/en/#configuration-files) 并且导入插件: + +```js +// rollup.config.js +import typescript from '@rollup/plugin-typescript'; + +export default { + input: 'src/index.ts', + output: { + dir: 'output', + format: 'cjs' + }, + plugins: [typescript()] +}; +``` + +## Svelte Compiler + +### 安装 + +``` +npm install --save-dev svelte-preprocess +``` + +请注意,`typescript` 是此插件的 peerDependencies, 需要单独安装。 `tslib` 也没有提供。 + +你也可以考虑使用 [`svelte-check`](https://www.npmjs.com/package/svelte-check) 进行 CLI 类型检查。 + +### 使用 + +创建一个 `svelte.config.js` 配置文件并且导入插件: + +```js +// svelte.config.js +import preprocess from 'svelte-preprocess'; + +const config = { + // Consult https://github.com/sveltejs/svelte-preprocess + // for more information about preprocessors + preprocess: preprocess() +}; + +export default config; +``` + +现在你可以指定脚本块中用TypeScript编写: + +``` +