简体   繁体   English

Gulp开发工作流程和“ dist”文件夹

[英]Gulp development workflow and the 'dist' folder

I'm relatively new to gulp and am using it as a taskrunner to concatenate and compile .js and sass/css files. 我对gulp比较陌生,并且将它用作任务运行器来连接和编译.js和sass / css文件。 Those aspects are currently working in a development environment. 这些方面目前正在开发环境中工作。

What I'm confused about is a workflow issue. 我感到困惑的是工作流程问题。 Right now my concatentated files are in these folders: 现在,我所包含的文件位于以下文件夹中:

-css (myapp.css)
-js (myappconcat.js)

If I'm working locally, I don't want to minify these yet for debugging purposes (right?). 如果我在本地工作,我还不想为了调试目的而缩小它们(对吗?)。

However, reading up on gulp, I see that build tasks are normally set up to minify the assets into a 'dist' folder for production. 但是,仔细阅读gulp,我发现通常会建立构建任务以将资产缩小到“ dist”文件夹中进行生产。 How do I manage this in my HTML files? 如何在HTML文件中进行管理?

For example, in my development HTML file I would have CSS and js includes like so: 例如,在我的开发HTML文件中,我将包含CSS和js,如下所示:

<link href="/css/myapp.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/js/myappconcat.js"></script>

However, when I build my minified files to a 'dist' directory, I would have to change the paths in the includes above for every HTML file. 但是,当我将缩小的文件构建到“ dist”目录时,我将不得不为每个HTML文件更改上述includes中的路径。

How does one go about managing a gulp build to a dist folder without having to change the paths in each include in the HTML? 如何在不更改HTML的每个include路径的情况下将gulp构建管理到dist文件夹? Similarly, when I minify for production, don't I also have to change the .js and CSS filenames in the HTML includes as well? 同样,当我缩小生产规模时,也不必更改HTML包含文件中的.js和CSS文件名吗? Or should I be using minified files in development? 还是应该在开发中使用缩小文件?

EDIT: Here's the basic site structure: 编辑:这是基本的网站结构:

wwwroot
   -.html files
   - gulpfile.js
   - package.json
   /css
     -main.css
   /dist
   /js
   /scss  

AND the gulpfile.js 和gulpfile.js

// Required gulp modules
var gulp      = require('gulp'),
    concat    = require('gulp-concat'),
    uglify    = require('gulp-uglify'),
    rename    = require('gulp-rename'),
    sass      = require('gulp-sass'),
    maps      = require('gulp-sourcemaps'),
    del       = require('del'),
    jshint    = require('gulp-jshint'),
    stylish   = require('jshint-stylish'),
    gutil     = require( 'gulp-util' ),
    merge     = require('merge-stream'),
    imagemin  = require('gulp-imagemin'),
    cleanCSS  = require('gulp-clean-css');

// error reporter in progress
var reportError = function( err ) {

  gutil.beep();

  var title = 'Error';

  if( err.plugin === 'gulp-sass' ) {
    title = 'SCSS Compilation Error';
  }

  var msg = '\n============================================\n\n';
  msg += title += '\n\n';
  msg += err.message + '\n\n';
  msg += '============================================';

  gutil.log( gutil.colors.red( msg ) );

  if( err.plugin === 'gulp-sass' ) {

   this.emit('end');

  }

};

// Concatenate .js files with lint-js as the dependency
gulp.task("concatScripts", ["lint-js"], function() {
   return gulp.src([
       'js/*.js'
       ,'node_modules/jquery-placeholder/jquery.placeholder.js'
       ])
   .pipe(maps.init())
   .pipe(concat('zConcat.js'))
   .pipe(maps.write('./'))
   .pipe(gulp.dest('dist/js'));//write to separate dir than src otherwise 
     overwrites itself!
});

// Minify .js files with concatscripts as the dependency
gulp.task("minifyScripts", ["concatScripts"], function() {
  return gulp.src("dist/js/zConcat.js")
    .pipe(uglify())
    .pipe(rename('zConcat.js'))
    .pipe(gulp.dest('dist/js'));
});

// .js Linter for catching errors when any .js changes
gulp.task('lint-js', function () {
  return gulp.src([
      'js/*.js',       // all custom .js
      //'!_js/public/js/html5shiv.js', // ignore shiv
    ])
    .pipe(jshint())
    .pipe(jshint.reporter(stylish));    // color-coded line by line errors
});

// Compile sass files
gulp.task('compileSass', function() {
  return gulp.src("scss/main.scss")
      .pipe(maps.init())
      .pipe(sass(
       {
       outputStyle: 'expanded'
       }
     ).on( 'error',  reportError) ) //sass.logError
      .pipe(maps.write('./'))
      .pipe(gulp.dest('css'));
});

// Minify CSS after compiling
 gulp.task('minifyCSS', () => {
   return gulp.src('css/*.css')
   .pipe(maps.init())
   .pipe(cleanCSS({compatibility: 'ie8'}))
   .pipe(rename('main.min.css'))
   .pipe(maps.write('./'))
   .pipe(gulp.dest('css'));
});

// Watch and process SASS and .js file changes
gulp.task('watchFiles', function() {
  gulp.watch(['scss/**/*.scss','scss/*.scss'], ['compileSass']);
  gulp.watch( 'css/*.css', ['minifyCSS']);
  gulp.watch('js/**/*.js', ['concatScripts']);
});

this might be late, but no one answered. 这可能已经晚了,但没人回答。

For me, I create two production files (or folders if the output is more than one file, with each folder containing a set of files). 对我来说,我创建了两个生产文件(如果输出是多个文件,则创建文件夹,每个文件夹包含一组文件)。 One is minified, and the other is not. 一个是缩小的,另一个不是。

For development and testing purposes, I use the non-minified version. 为了进行开发和测试,我使用非缩小版。 Once the product is ready for production, I retest but using the minified version, and typically this is the one I release. 产品准备好生产后,我将使用缩小版进行重新测试,通常这是我发布的版本。

So I write my scripts to produce two files (or file sets if I have more than a file to produce). 因此,我编写脚本以生成两个文件(如果要生成的文件超过一个,则为文件集)。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM