gulp在dist中创建src目录结构

时间:2016-06-15 08:22:10

标签: gulp tsconfig

我收到了以下gulp个任务,

const gulp = require('gulp');
const del = require('del');
const typescript = require('gulp-typescript');
const tscConfig = require('./tsconfig.json');
const sourcemaps = require('gulp-sourcemaps');
const print = require('gulp-print');

// clean the contents of the distribution directory
gulp.task('clean', function () {
  return del('dist/**/*');
});

// TypeScript compile
gulp.task('compile', ['clean'], function () {
  return gulp
    .src(tscConfig.files)
    .pipe(print(function (filepath) {
      return 'Processing ' + filepath + '...';
    }))
    .pipe(sourcemaps.init())
    .pipe(typescript(tscConfig.compilerOptions))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('dist/app'));
});

gulp.task('build', ['compile']);
gulp.task('default', ['build']);

和目录结构

|
+- dist
|
+- src 
|   |
|   +- app
|   |   |
|   |   + - example.ts

现在,如果我运行gulp build,我会在app/src内得到另一个dist,即

|
+- dist
|   |
|   + - app
|        |
|        + - src 
|             |
|             + - app
|                  |
|                  + - example.js
|                  + - example.js.map
+- src 
|   |
|   +- app
|   |   |
|   |   + - example.ts   

这不是我想要的。如何告诉gulp而不是创建以下目录结构?

|
+- dist
|   |
|   + - app
|        |
|        + - example.js
|        + - example.js.map 
|
+- src 
|   |
|   +- app
|   |   |
|   |   + - example.ts 

我需要gulp-replace还是有其他选择?

编辑:tsconfig.json如下:

{
    "compilerOptions": {
        "outDir": "dist/app",
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "suppressImplicitAnyIndexErrors": true
    },
    "files": [
        "src/app/**/*.ts"
    ]
}

1 个答案:

答案 0 :(得分:0)

我使用gulp-rename,如果有人能想出更好的解决方案,我很想听到它。

gulp.task('compile', function () {
  return gulp
    .src(tscConfig.files)
    .pipe(print(function (filepath) {
      return 'Processing ' + filepath + '...';
    }))
    .pipe(sourcemaps.init())
    .pipe(typescript(tscConfig.compilerOptions))
    .pipe(rename(function (path) {
      if (path.dirname === 'src\\app') {
        path.dirname = '';
      } else {
        path.dirname = path.dirname.replace('src\\app\\', '');
      }
    }))
    .pipe(print(function (filepath) {
      return 'Generated file: ' + filepath;
    }))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('dist/app'));
});