This commit is contained in:
gnu4cn 2019-10-30 14:58:14 +08:00
parent a3e7b3692d
commit 866cc29c1d
8 changed files with 40 additions and 19 deletions

View File

@ -1,28 +1,40 @@
// 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');
var tsProject = ts.createProject('tsconfig.json');
let paths = {
pages: ["src/*.html"]
};
gulp.task("copy-html", ()=>{
return gulp.src(paths.pages)
.pipe(gulp.dest("dist"))
gulp.task('clean', () => {
return gulp
.src([
'./dist/'
], { read: false, allowEmpty: true })
.pipe(clean());
});
gulp.task('tsc', () => {
return gulp.src('src/*.ts')
.pipe(tsProject())
.pipe(gulp.dest('dist'));
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', () => {
gulp.watch('src/*.ts', gulp.series('tsc'));
gulp.watch('./src/namespace/*.ts', gulp.series('clean', 'transpile-ts'));
});
// 这里必须要有一个 default 任务
gulp.task('default', gulp.series('copy-html', 'tsc', 'watch'));
gulp.task('default', gulp.series('clean', 'copy-html', 'transpile-ts', 'watch'));

View File

@ -4,7 +4,7 @@
"description": "TypeScript Learning stuffs.",
"main": "/dist/main.js",
"scripts": {
"gulp": "gulp &",
"gulp": "gulp",
"start": "live-server dist/",
"test": "echo \"Error: no test specified\" && exit 1"
},

View File

@ -1,4 +1,5 @@
/// <reference path="IShape.ts" />
namespace Drawing {
export class Circle implements IShape {
public draw(): void {

8
src/namespace/Circle.ts Normal file
View File

@ -0,0 +1,8 @@
/// <reference path="IShape.ts" />
namespace Drawing {
export class Circle implements IShape {
public draw(): void {
console.log("Circle is drawn.");
}
}
}

View File

@ -1,6 +1,6 @@
/// <reference path="IShape.ts" />
/// <reference path="Circle.ts" />
/// <reference path="Triangle.ts" />
/// <reference path="Circle.ts" />
function drawAllShapes(shape: Drawing.IShape) {
shape.draw();

View File

@ -1,4 +1,5 @@
/// <reference path="IShape.ts" />
namespace Drawing {
export class Triangle implements IShape {
public draw(): void{

View File

@ -1,11 +1,10 @@
{
"include": [
"src/*.ts"
"src/namespace/*.ts"
],
"compilerOptions": {
"noImplicitAny": true,
"target": "es2015",
"outDir": "dist/",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types": [