W
W
Woshibai2021-03-21 17:23:34
Frontend
Woshibai, 2021-03-21 17:23:34

Blank page when deploying with gulp via browser-sync?

When you run gulp, a blank page pops up. If you enter ' localhost:3000 ' into the url line, then everything opens normally.
gulpfile.js:

let project__folder = require("path").basename(__dirname);
let source_folder = "#src";

let fs = require('fs');

let path = {
  build: {
    html: project__folder + "/",
    css: project__folder + "/css/",
    js: project__folder + "/js/",
    img: project__folder + "/img/",
    fonts: project__folder + "/fonts/",
  },
  src: {
    html: [source_folder + "/*.html", "!" + source_folder + "/_*.html"],
    css: source_folder + "/sass/style.sass",
    js: source_folder + "/js/main.js",
    img: source_folder + "/img/**/*.{jpg,png,svg,gif,ico,webp}",
    fonts: source_folder + "/fonts/*.ttf",
  },
  watch: {
    html: source_folder + "/**/*.html",
    css: source_folder + "/sass/**/*.sass",
    js: source_folder + "/js/**/*.js",
    img: source_folder + "/img/**/*.{jpg,png,svg,gif,ico,webp}"
  },
  clean: "./" + project__folder + "/",
};

let { src, dest } = require("gulp"),
  gulp = require("gulp"),
  browsersync = require("browser-sync").create(),
  fileinclude = require("gulp-file-include"),
  del = require("del"),
  sass = require("gulp-sass"),
  autoprefixer = require("gulp-autoprefixer"),
  group_media = require("gulp-group-css-media-queries"),
  clean_css = require("gulp-clean-css"),
  rename = require("gulp-rename"),
  uglify = require("gulp-uglify-es").default,
  imagemin = require("gulp-imagemin"),
  webp = require("gulp-webp"),
  webphtml = require("gulp-webp-html"),
  webpcss = require("gulp-webp-css"),
  svgSprite = require("gulp-svg-sprite"),
  ttf2woff = require("gulp-ttf2woff"),
  ttf2woff2 = require("gulp-ttf2woff2"),
  fonter = require("gulp-fonter")

function browserSync() {
  browsersync.init({
    server: {
      baseDir: "./" + project__folder + "/",
    },
    port: 3000,
    notify: false,
  });
}

function html() {
  return src(path.src.html)
    .pipe(fileinclude())
    .pipe(webphtml())
    .pipe(dest(path.build.html))
    .pipe(browsersync.stream())
}


function css() {
  return src(path.src.css, {})
    .pipe(
      sass({
        outputStyle: "expanded",
      })
    )
    .pipe(group_media())

    .pipe(
      autoprefixer({
        overrideBrowserlist: ["last 5 versions"],
        cascade: true,
      })
    )
    .pipe(webpcss())
    .pipe(dest(path.build.css))
    .pipe(clean_css())
    .pipe(
      rename({
        extname: ".min.css",
      })
    )
    .pipe(dest(path.build.css))
    .pipe(browsersync.stream());
}

function js() {
  return src(path.src.js)
    .pipe(fileinclude())
    .pipe(dest(path.build.js))                              //выгрузка незжатого файла
    .pipe(
      uglify()
    )
    .pipe(                                                  //переименование сжатого файла
      rename({
        extname: ".min.js",
      })
    )
    .pipe(dest(path.build.js))                              //выгрузка сжатого файла
    .pipe(browsersync.stream());                            //лайв апдейт
}

function images() {
  return src(path.src.img)
    .pipe(
      webp({
        quality: 75,
      }),
    )
    .pipe(
      rename({
        extname: ".webp",
      })
    )
    .pipe(dest(path.build.img))
    .pipe(src(path.src.img))
    .pipe(
      imagemin({
        progressive: true,
        svgoPlugins: [{ removeViewBox: false }],
        interlaced: true,
        optimizationLevel: 3, // 0 to 7
      })
    )
    .pipe(dest(path.build.img));
}

function fonts() {
  src(path.src.fonts)
    .pipe(ttf2woff())
    .pipe(dest(path.build.fonts));
  return src(path.src.fonts)
    .pipe(ttf2woff2())
    .pipe(dest(path.build.fonts))
    .pipe(browsersync.stream());
}

function fonts_otf() {
  return src('./' + src_folder + '/fonts/*.otf')
    .pipe(fonter({
      formats: ['ttf']
    }))
    .pipe(gulp.dest('./' + src_folder + +'/fonts/'));
}

gulp.task('svgSprite', function (){
  return gulp.src([source_folder + '/icons/*.svg'])
    .pipe(svgSprite({
      mode: {
        stack: {
          sprite: '../icons/icons.svg',
          // example: true
        }
      },
    }
    ))
    .pipe(dest(path.build.img))
})

function fontstyle() {
  let file_content = fs.readFileSync(source_folder + '/sass/_fonts.sass')
  if (file_content == '') {
    fs.writeFile(source_folder + '/sass/_fonts.sass', '', cb);
    return fs.readdir(path.build.fonts, function (err, items) {
      if (items) {
        let c_fontname;
        for (var i = 0; i < items.length; i++) {
          let fontname = items[i].split('.');
          fontname = fontname[0];
          if (c_fontname != fontname) {
            fs.appendFile(source_folder + '/sass/_fonts.sass', '@include font("' + fontname + '", "' + fontname + '", "400", "normal")\r\n', cb);
          }
          c_fontname = fontname;
        }
      }
    })
  }
}

function cb() {

}

function watchFiles() {
  gulp.watch([path.watch.html], html);
  gulp.watch([path.watch.css], css);
  gulp.watch([path.watch.js], js);
  gulp.watch([path.watch.img], images);
}

function clean() {
  return del(path.clean);
}

let build = gulp.series(clean, gulp.parallel(js ,css, html, images, fonts), fontstyle);
let watch = gulp.parallel(build, watchFiles, browserSync);

exports.fontstyle = fontstyle;
exports.fonts = fonts;
exports.images = images;
exports.js = js;
exports.html = html;
exports.css = css;
exports.build = build;
exports.watch = watch;
exports.default = watch;
exports.clean = clean;

Terminal:
[17:54:26] Starting 'default'...
[17:54:26] Starting 'watchFiles'...
[17:54:26] Starting 'browserSync'...
[17:54:26] Starting 'clean'...
[Browsersync] Access URLs:
 --------------------------------------  
       Local: http://localhost:3000      
    External: http://192.168.0.104:3000  
 --------------------------------------  
          UI: http://localhost:3001      
 UI External: http://localhost:3001      
 --------------------------------------  
[Browsersync] Serving files from: ./ACME/
[17:54:26] Finished 'clean' after 109 ms
[17:54:26] Starting 'js'...
[17:54:26] Starting 'css'...
[17:54:26] Starting 'html'...
[17:54:26] Starting 'images'...
[17:54:26] Starting 'fonts'...
[17:54:26] gulp-imagemin: Minified 0 images
[17:54:26] Finished 'images' after 41 ms   
[Browsersync] 1 file changed (index.html)
[17:54:27] Finished 'html' after 1.14 s
[Browsersync] 1 file changed (ProximaNova-Light.woff2)
[17:54:27] Finished 'fonts' after 1.14 s
[Browsersync] 1 file changed (style.min.css)
[17:54:27] Finished 'css' after 1.27 s    
[Browsersync] 1 file changed (main.min.js)
[17:54:27] Finished 'js' after 1.27 s     
[17:54:27] Starting 'fontstyle'...        
[Browsersync] Reloading Browsers... (buffered 4 events)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question