N
N
Nikinikonewbie2016-04-01 01:49:07
JavaScript
Nikinikonewbie, 2016-04-01 01:49:07

Optimizing specific JS code (gulp)?

Hello. Sorry for the question, which asks you to write more code for TCa, but I don’t know how to do it ..
In general, the logic of gulp.js is this:
There are files in 3 folders ./haml/p; ./haml/l; ./haml/i
These contain .haml files. Next, you need to collect these files into the appropriate ./_p ; ./_l ; ./_i directories, but it collects them in one line. I decided to apply prettify for html in addition to everything. In my code below, we save each haml file first to ./haml/html/(p|l|i) and then prettify to the correct directory.
Ideally, everything should be done in two tasks, but I can't figure out how to do it. Here is the code that works:

var gulp = require('gulp');
var haml = require('gulp-haml');
var prettify = require('gulp-prettify');


gulp.task('layouts', function() {
  gulp.src('./haml/layouts/*.haml')
    .pipe(haml())
    .pipe(gulp.dest('./haml/html/_layouts/'));
});

gulp.task('prettify_layouts', function(){
  gulp.src('./haml/html/_layouts/*.html')
    .pipe(prettify({indent_size: 2}))
    .pipe(gulp.dest('./_layouts/'));
});

gulp.task('includes', function() {
  gulp.src('./haml/includes/*.haml')
    .pipe(haml())
    .pipe(gulp.dest('./haml/html/_includes/'));
});

gulp.task('prettify_includes', function(){
  gulp.src('./haml/html/_includes/*.html')
    .pipe(prettify({indent_size: 2}))
    .pipe(gulp.dest('./_includes/'));
});

gulp.task('posts', function() {
  gulp.src('./haml/posts/*.haml')
    .pipe(haml())
    .pipe(gulp.dest('./haml/html/_posts/'));
});

gulp.task('prettify_posts', function(){
  gulp.src('./haml/html/_posts/*.html')
    .pipe(prettify({indent_size: 2}))
    .pipe(gulp.dest('./_posts/'));
});

gulp.task('default', function() {
  gulp.start('includes','prettify_includes', 'layouts', 'prettify_layouts', 'posts', 'prettify_posts');
});

sorry for this question and help please

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Puzynya, 2016-04-01
@3y3

What does not suit you in the following code (the option can be improved if you understand where to go):

var gulp = require('gulp');
var haml = require('gulp-haml');
var prettify = require('gulp-prettify');


gulp.task('layouts', function() {
  gulp.src('./haml/layouts/*.haml')
    .pipe(haml())
    .pipe(prettify({indent_size: 2}))
    .pipe(gulp.dest('./_layouts/'));
});

gulp.task('includes', function() {
  gulp.src('./haml/includes/*.haml')
    .pipe(haml())
    .pipe(prettify({indent_size: 2}))
    .pipe(gulp.dest('./_includes/'));
});

gulp.task('posts', function() {
  gulp.src('./haml/posts/*.haml')
    .pipe(haml())
    .pipe(prettify({indent_size: 2}))
    .pipe(gulp.dest('./_posts/'));
});

gulp.task('default', ['includes', 'layouts', 'posts']);

N
nk_pl, 2016-04-01
@nk_pl

var gulp = require('gulp');
var haml = require('gulp-haml');
var prettify = require('gulp-prettify');

gulp.task('haml', function() {
  gulp.src('./haml/**/*.haml', {base: './haml'})
    .pipe(haml())
    .pipe(prettify({indent_size: 2}))
    .pipe(gulp.dest('./'))
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question