@Alexrch

Почему не работает Gulp?

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 + "/scss/style.scss",
        js: source_folder + "/js/script.js",
        img: source_folder + "/img/**/*.{jpg,png,svg,gif,ico,webp}",
        fonts: source_folder + "/fonts/*.ttf",
    },
    watch: {
        html: source_folder + "/**/*.html",
        css: source_folder + "/scss/**/*.scss",
        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"),
scss = 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-webpcss"),
ttf2woff = require("gulp-ttf2woff"),
ttf2woff2 = require("gulp-ttf2woff2"),
fonter = require("gulp-fonter");

function browserSync(params) {
    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(
        scss({
            outputStyle: "expanded"
        })
    )
        .pipe(
        group_media()
    )
        .pipe(
        autoprefixer({
            overrideBrowserslist: ["last 5 version"],
            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:70
            })
        )
        .pipe(dest(path.build.img))
        .pipe(src(path.src.img))
        .pipe(
        imagemin({
            progressive: true,
            svgoPlugins: [{ removeViewBox: false}],
            interlaced: true,
            optmizationLevel: 3 
        })
    )
        .pipe(dest(path.build.img))
        .pipe(browsersync.stream())
    }

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));
};

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

function fontsStyle(params) {
    let file_content = fs.readFileSync(source_folder + '/scss/fonts.scss');
    if (file_content == '') {
        fs.writeFile(source_folder + '/scss/fonts.scss', '', 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 + '/scss/fonts.scss', '@include font("' + fontname + '", "' + fontname + '", "400", "normal");\r\n', cb);
                    }
                    c_fontname = fontname;
                }
            }
        })
    }
}


function cb( {

})

function watchfile(params) {
    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(params) {
    return del(path.clean);
}

let build = gulp.series(clean, gulp.parallel(js, css, html, images), fontsStyle);
let watch = gulp.parallel(build,watchfile,browserSync);


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


D:\fls-gulp>gulp
D:\fls-gulp\gulpfile.js:177
function watchfile(params) {
^^^^^^^^

SyntaxError: Unexpected token 'function'
←[90m    at wrapSafe (internal/modules/cjs/loader.js:1047:16)←[39m
←[90m    at Module._compile (internal/modules/cjs/loader.js:1097:27)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:977:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:877:14)←[39m
←[90m    at Module.require (internal/modules/cjs/loader.js:1019:19)←[39m
←[90m    at require (internal/modules/cjs/helpers.js:77:18)←[39m
    at execute (C:\Users\User\AppData\Roaming\npm\node_modules\←[4mgulp-cli←[24m\lib\versioned\^4.0.0\index.js:36:18)
    at Liftoff.handleArguments (C:\Users\User\AppData\Roaming\npm\node_modules\←[4mgulp-cli←[24m\index.js:201:24)
    at Liftoff.execute (C:\Users\User\AppData\Roaming\npm\node_modules\←[4mgulp-cli←[24m\node_modules\←[4mliftoff←[24m\index.js:201:12)
  • Вопрос задан
  • 658 просмотров
Решения вопроса 1
alexey-m-ukolov
@alexey-m-ukolov Куратор тега JavaScript
function cb( {

})

Это синтаксически невалидный код, удалите или исправьте его.
Видимо, вы хотели написать вот это:
function cb() {
}


Любой приличный редактор кода вам бы указал на эту ошибку. Вон, даже подсветка кода здесь, и та от него поломалась.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы