如何为浏览器用户 gulp 和 tsproject 捆绑打字稿文件? [英] How to bundle typescript files for the browser user gulp and tsproject?

查看:33
本文介绍了如何为浏览器用户 gulp 和 tsproject 捆绑打字稿文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是前端开发的新手,是一名长期的后端 C# 和 Java 开发人员,我正在尝试掌握如何构建前端应用程序.我想使用 typescript 构建一个应用程序,并使用 gulp 作为构建管道将我的代码转换、捆绑和缩小到单个 JavaScript 文件.

I'm kinda new to front end development, being a long time backend C# and Java developer, and I'm trying to come to grips with how to build front end applications. I would like to build an application using typescript and use gulp for my build pipeline to transpile, bundle and minify my code to a single JavaScript file.

为此,我一直在查看节点模块 tsproject ,到目前为止据我所知,应该完全按照我的意愿去做.除了,我不能让它做我想做的事:-(.

For this purpose, I have been looking at the node module tsproject which, as far as I can understand, is supposed to do exactly what I want. Except, I can't get it to do what I want :-(.

这是我的 gulpfile.js 的相关部分:

This is the relevant parts my gulpfile.js:

var gulp = require('gulp');
var clean = require('gulp-clean');
var tsproject = require('tsproject');

gulp.task('default', [ 'min-js' ]);

gulp.task('clean', function () {
    return gulp
        .src('./dist/', { read: false })
        .pipe(clean());
});

gulp.task('min-js', [ 'clean' ], function () {
    return tsproject
        .src('./tsconfig.json')
        .pipe(gulp.dest('./dist/js'))
});

还有我的 tsconfig.json:

And my tsconfig.json:

{
    "compilerOptions": {
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es5",
        "jsx": "react",
        "outDir": "./"
    },
    "files": [
        "./src/**/*.tsx",
        "./src/**/*.ts"
    ],
    "bundles": {
        "myApp": {
            "files": [
                "./src/App.tsx"
            ],
            "config": {
                "minify": true,
                "sourcemap": true
            }
        }
    }
}

我曾期望这会输出一个包含转译、捆绑和缩小的 JavaScript 代码的单个 js 文件,但我得到的是一个捆绑的(未转译或缩小的)Typescript 文件 (./dist/js/myApp.min.ts).

I had expected this to output a single js-file containing transpiled, bundled and minified JavaScript code but what I get is a bundled (not transpiled nor minified) Typescript file (./dist/js/myApp.min.ts).

是我做错了什么还是完全误解了 tsproject 的目的和意图?

Am I doing something wrong or have I completely misunderstood the purpose and intent of tsproject?

推荐答案

好吧,我不确定这是否是最好的方法,但我终于可以使用这样的 gulpfile 了:

Well, I'm not sure if this is the best way to go about it but I finally got things working with a gulpfile like this:

var gulp = require('gulp');
var browserify = require('gulp-browserify');
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
var ts = require('gulp-typescript');
var uglify = require('gulp-uglify');

gulp.task('default', [ 'min-js' ]);

gulp.task('clean', function () {
    return gulp
        .src([
            './tmp/',
            './dist/'
        ], { read: false })
        .pipe(clean());
});

gulp.task('transpile-ts', ['clean'], function () {
    var tsproject = ts.createProject('./src/tsconfig.json');
    return tsproject
        .src()
        .pipe(sourcemaps.init())
        .pipe(tsproject()).js
        .pipe(sourcemaps.write('./sourcemaps'))
        .pipe(gulp.dest('./tmp/js'));
});

gulp.task('min-js', [ 'transpile-ts' ], function () {
    return gulp
        .src('./tmp/js/App.js')
        .pipe(sourcemaps.init({ loadMaps: true }))
        .pipe(browserify())
        .pipe(uglify())
        .pipe(concat('App.min.js'))
        .pipe(sourcemaps.write('./sourcemaps'))
        .pipe(gulp.dest('./dist/js'))
});

基本上,我将所有 TypeScript 文件转换为 Javascript,并将 CommonJS 导入到一个临时目录.然后我使用 Browserify 将 JavaScript 文件捆绑(即解析 CommonJS 导入)到单个文件中.最后,我缩小了生成的 JavaScript 文件.瞧!

Basically I transpile all TypeScript files to Javascript with CommonJS imports to a temporary directory. Then I use Browserify to bundle (i.e. resolve the CommonJS imports) the JavaScript files into a single file. Finally I minify the resulting JavaScript file. Voila!

出于某种原因,我必须先将转译文件写入临时目录,然后打包并缩小这些文件.而不是继续将 pipe(tsproject()) 的结果传送到 browserify()...

For some reason I had to first write the transpiled files to a temporary directory and then bundle and minify those files. Instead of just keep on pipeing the result of pipe(tsproject()) to browserify()...

这篇关于如何为浏览器用户 gulp 和 tsproject 捆绑打字稿文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆