Gulp在Dist中创建SRC目录结构



我有以下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,我在dist中获得了另一个app/src,那就是

|
+- 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"
    ]
}

我求助于使用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'));
});

最新更新