问题描述:

I have a simple TypeScript project which is made up of two files:

helloworld.ts

export class HelloWorld {

static printHelloWorld(): void {

console.log('Hello World');

}

}

and main.ts

import { HelloWorld } from "./helloworld";

HelloWorld.printHelloWorld();

I'm using gulp to build the project, with gulp-typescript as TypeScript compiler. Everything works fine since I decide to bundle both compiled files into a single one with gulp-concat. This is my build task:

gulpfile.js

var paths = {

tscripts: {

src: ['app/src/**/*.ts'],

dest: 'app/build'

}

};

gulp.task('build', function () {

return gulp

.src(paths.tscripts.src)

.pipe(sourcemaps.init())

.pipe(ts())

.pipe(concat('main.js'))

.pipe(sourcemaps.write())

.pipe(gulp.dest(paths.tscripts.dest));

});

The build process ends up with no errors, but when I run the program with node main.js this is what I got:

Error: Cannot find module './helloworld'

In fact the compiled .js tries to resolve a dependency that, after gulp-concat, is in the same file:

"use strict";

var HelloWorld = (function () {

function HelloWorld() {

}

HelloWorld.printHelloWorld = function () {

console.log('Hello World');

};

return HelloWorld;

}());

exports.HelloWorld = HelloWorld;

var helloworld_1 = require("./helloworld");

helloworld_1.HelloWorld.printHelloWorld();

How can I tell gulp that all the classes I'm importing in the source should be in a single file in the build?

Should I have to use any other gulp plugin? Or just have to setup correctly the TypeScript compiler?

相关阅读:
Top