Gulp任务:如何将我的公共目录转移到我的dist文件夹中?

时间:2017-01-21 17:27:09

标签: node.js express gulp

在我的Node / Express应用程序中使用以下文件结构,我尝试将服务器/公共目录复制到dist文件夹,但只传输内容(favicon.ico):

my_app
|___ config
|      |____ env
|      |     |___ development.js
|      |       |___ index.js
|      |       |___ production.js
|      |       |___ test.js
|      |_____ express.js
|
|_____ dist ...
|_____ node_modules ...
|_____ server
|       |____ controllers ...
|       |____ helpers ...
|       |____ models ...
|       |____ controllers ...
|       |____ public ...
|       |       |__ favicon.ico
|       |____ routes ...
|       |____ test ...
|_______ ...

在我的gulp文件中,我使用复制任务将所有nonJs文件复制到dist中,但是favicon会直接复制到dist(例如.gitignore),公共文件夹不会在dist文件夹中创建。

import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import path from 'path';
import del from 'del';
import runSequence from 'run-sequence';

const plugins = gulpLoadPlugins();

const paths = {
  js: ['./**/*.js', '!dist/**', '!node_modules/**', '!coverage/**'],
  nonJs: ['./package.json', './.gitignore', './server/public/*.*'],
  tests: './server/tests/*.js'
};

// Clean up dist and coverage directory
gulp.task('clean', () =>
  del(['dist/**', 'coverage/**', '!dist', '!coverage'])
);

// Copy non-js files to dist
gulp.task('copy', () =>
  gulp.src(paths.nonJs)
    .pipe(plugins.newer('dist'))
    .pipe(gulp.dest('dist'))
);

// Compile ES6 to ES5 and copy to dist
gulp.task('babel', () =>
  gulp.src([...paths.js, '!gulpfile.babel.js'], { base: '.' })
    .pipe(plugins.newer('dist'))
    .pipe(plugins.sourcemaps.init())
    .pipe(plugins.babel())
    .pipe(plugins.sourcemaps.write('.', {
      includeContent: false,
      sourceRoot(file) {
        return path.relative(file.path, __dirname);
      }
    }))
    .pipe(gulp.dest('dist'))
);

// Start server with restart on file changes
gulp.task('nodemon', ['copy', 'babel'], () =>
  plugins.nodemon({
    script: path.join('dist', 'index.js'),
    ext: 'js',
    ignore: ['node_modules/**/*.js', 'dist/**/*.js'],
    tasks: ['copy', 'babel']
  })
);

// gulp serve for development
gulp.task('serve', ['clean'], () => runSequence('nodemon'));

// default task: clean dist, compile js files and copy non-js files.
gulp.task('default', ['clean'], () => {
  runSequence(
    ['copy', 'babel']
  );
});

执行复制任务后,我在dist文件夹中获得以下结构

my_app
  |
  |_____ dist
          |___ config ...
          |___ server ...
          |       |___ controllers ...
          |       |___ helpers ...
          |       |___ models ...
          |       |___ routes ...
          |       |___ test ...
          |___ .gitignore
          |___ favicon.ico
          |___ index.js
          |___ package.json

1 个答案:

答案 0 :(得分:1)

我在复制任务中添加了一个基础:

// Copy non-js files to dist
gulp.task('copy', () =>
  gulp.src(paths.nonJs, {base: './'})
    .pipe(plugins.newer('dist'))
    .pipe(gulp.dest('dist'))
);