mirror of
https://github.com/gnu4cn/ts-learnings.git
synced 2025-01-13 13:50:07 +08:00
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
// https://stackoverflow.com/questions/40823462/how-to-bundle-typescript-files-for-the-browser-user-gulp-and-tsproject
|
|
var gulp = require('gulp');
|
|
var clean = require('gulp-clean');
|
|
var sourcemaps = require('gulp-sourcemaps');
|
|
var ts = require('gulp-typescript');
|
|
|
|
gulp.task('clean', () => {
|
|
return gulp
|
|
.src([
|
|
'./dist/'
|
|
], { read: false, allowEmpty: true })
|
|
.pipe(clean());
|
|
});
|
|
|
|
gulp.task("transpile-ts", () => {
|
|
var tsProject = ts.createProject('tsconfig.json',{
|
|
outFile: "app.js"
|
|
});
|
|
return tsProject
|
|
.src()
|
|
.pipe(sourcemaps.init())
|
|
.pipe(tsProject()).js
|
|
.pipe(sourcemaps.write('./sourcemaps'))
|
|
.pipe(gulp.dest('./dist/js'));
|
|
});
|
|
|
|
gulp.task("copy-html", () => {
|
|
return gulp
|
|
.src(['./src/*.html'])
|
|
.pipe(gulp.dest("./dist/"));
|
|
});
|
|
|
|
// 这里 watch 里必须使用 gulp.series
|
|
gulp.task('watch', () => {
|
|
// 这里监视所有src及其所有子文件夹下的ts文件
|
|
gulp.watch('./src/**/*.ts', gulp.series('clean', 'copy-html', 'transpile-ts'));
|
|
});
|
|
|
|
|
|
// 这里必须要有一个 default 任务
|
|
gulp.task('default', gulp.series('clean', 'copy-html', 'transpile-ts', 'watch'));
|