1

我目前正在将 gulp 3.x 迁移到 gulp 4.x,在这个 gulp html 文件中创建一个临时文件夹。在此,会自动更新 src/ 文件夹。

预期结果 .tmp/serve

里面需要以下文件结构。应用程序/index.css index.html

实际结果 1.serve/app/src/app/index.css 2.serve/src/index.html

在这个 src 文件夹中需要删除。我将附上整个 gulp build.js 文件。

  (function () {
      'use strict';

        var path = require('path');
        var gulp = require('gulp');
        var conf = require('./conf');
        var inject = require('./inject');

       var $ = require('gulp-load-plugins')({
pattern: ['gulp-*', 'npmfiles', 'uglify-save-license', 'del']

});

      function partials(){
          return gulp.src([
          path.join(conf.paths.src, '/app/**/*.html'),
         path.join(conf.paths.tmp, '/serve/app/**/*.html')
    ])
  .pipe($.htmlmin({
    removeEmptyAttributes: true,
    removeAttributeQuotes: true,
    collapseBooleanAttributes: true,
    collapseWhitespace: true
  }))
  .pipe($.angularTemplatecache('templateCacheHtml.js', {
    module: 'hubble',
    root: 'app'
  }))
  .pipe(gulp.dest(conf.paths.tmp + '/partials/'));
    };
     gulp.task('partials', gulp.series(partials));


     gulp.task('html', gulp.series('inject', 'partials', function html () 
    {
       var partialsInjectFile = gulp.src(path.join(conf.paths.tmp, 
       '/partials/templateCacheHtml.js'), { read: false });
       var partialsInjectOptions = {
        starttag: '<!-- inject:partials -->',
        ignorePath: path.join(conf.paths.tmp, '/partials'),
        addRootSlash: false
    };

var htmlFilter = $.filter('*.html', { restore: true, dot: true, matchBase: true });
var jsFilter = $.filter('**/*.js', { restore: true , dot: true});
var cssFilter = $.filter('**/*.css', { restore: true , dot: true});

return gulp.src(path.join(conf.paths.tmp, '/serve/*.html'))
  .pipe($.inject(partialsInjectFile, partialsInjectOptions))
  .pipe($.useref())
  .pipe(jsFilter)
  .pipe($.rev())
  .pipe($.sourcemaps.init())
  .pipe($.ngAnnotate())
  .pipe($.uglify()).on('error', conf.errorHandler('Uglify'))
  //.pipe($.sourcemaps.write('maps'))
  .pipe(jsFilter.restore)
  .pipe(cssFilter)
  // .pipe($.sourcemaps.init())
  .pipe($.replace('Casino_Hand/', '../fonts/'))
  .pipe($.cssnano({
    zindex: false
  }))
  .pipe($.rev())
  // .pipe($.sourcemaps.write('maps'))
  .pipe(cssFilter.restore)
  .pipe($.revReplace())
  .pipe(htmlFilter)
  .pipe($.htmlmin({
    removeEmptyAttributes: true,
    removeAttributeQuotes: false,
    collapseBooleanAttributes: true,
    collapseWhitespace: true
  }))
  .pipe(htmlFilter.restore)
  .pipe(gulp.dest(path.join(conf.paths.dist, '/')))
  .pipe($.size({ title: path.join(conf.paths.dist, '/'), showFiles: true }));

}));

  function other(){
var fileFilter = $.filter(function (file) {
  return file.stat.isFile();
});0

return gulp.src([
  path.join(conf.paths.src, '/**/*'),
  path.join('!' + conf.paths.src, '/**/*.{html,css,js,scss}')
])
.pipe(fileFilter)
.pipe(gulp.dest(path.join(conf.paths.dist, '/')));

};

     function clean(){
return $.del([path.join(conf.paths.dist, '/'), path.join(conf.paths.tmp, 
   '/')]);
  };
     gulp.task('clean',gulp.series(clean));

// 仍然注入 GlobalConfig.js(但不是最小化)以允许部署工具在需要时更新现有值

      function config(){
return gulp.src(path.join(conf.paths.src, '/GlobalConfig.js'))
.pipe(gulp.dest(path.join(conf.paths.dist, '')));

   };

     gulp.task('build',gulp.series(config, 'html', other));

 })();
4

1 回答 1

0

不要使用 path.join 提供 src 的路径,一切都会好的。例如,而不是:

gulp.src([
    path.join(conf.paths.src, '/**/*'),
    path.join('!' + conf.paths.src, '/**/*.{html,css,js,scss}')
])

利用:

gulp.src([
    conf.paths.src + '/**/*',
    '!' + conf.paths.src + '/**/*.{html,css,js,scss}'
])
于 2020-06-17T13:02:44.243 回答