使用MAMP进行Gulp 4 Sass + BrowserSync注入

问题描述 投票:0回答:1

我花了一整天的时间试图弄清楚这一点。我知道Gulp 4 & BrowserSync: Style Injection?并尝试了不同的方法。我错过了什么?

我试图让saulp为浏览器注入sass生成的样式。截至目前,它确实打开了一个带有生成的css的新选项卡,但它在更改时既不刷新也不注入新生成的样式。我明白了:

[Browsersync] Watching files...
[21:27:36] Starting 'buildStyles'...
[21:27:36] Finished 'buildStyles' after 40 ms
[Browsersync] File event [change] : site/templates/styles/main.css

但它没有注入。这是我的gulpfile.js:

const { src, dest, watch, series, parallel } = require('gulp');
const sass = require('gulp-sass');
const browsersync = require('browser-sync').create();


const paths = {
    sass: {
        // By using styles/**/*.sass we're telling gulp to check all folders for any sass file
        src: "site/templates/styles/scss/*.scss",
        // Compiled files will end up in whichever folder it's found in (partials are not compiled)
        dest: "site/templates/styles"
    },
    css: {
        src: "site/templates/styles/main.css"
    }
};

function buildStyles(){
    return src(paths.sass.src)
        .pipe(sass())
        .on("error", sass.logError)
        .pipe(dest(paths.sass.dest))
}

function watchFiles(){
    watch(paths.sass.src,{ events: 'all', ignoreInitial: false }, series(buildStyles));
}


function browserSync(done){
    browsersync.init({
        injectChanges: true,
        proxy: "http://client2019.local/",
        port: 8000,
        host: 'client2019.local',
        socket: {
            domain: 'localhost:80'
        },
        files: [
            paths.css.src
        ]
    });
    done();
    //   gulp.watch("./**/*.php").on("change", browserSync.reload);

}

exports.default = parallel(browserSync, watchFiles); // $ gulp
exports.sass = buildStyles;
exports.watch = watchFiles;
exports.build = series(buildStyles); // $ gulp build

我错过了什么?

javascript sass gulp mamp
1个回答
0
投票

我不知道我是否完全明白你想要什么。

要使用BrowserSync自动刷新浏览器,保存上次更改后,我在gulp文件中使用此代码:

    var paths = {
        styles: {
            src: "./src/styles/scss/**/*.scss",
            dest: "./view/styles"
        },
        htmls: {
            src: "./src/**/*.html",
            dest: "./view"
        }
    };

    const styles= () => {
            return...
        }

    const html= () => {
                return...
            }

    const watch = () => {
            browserSync.init({
                server: {
                    baseDir: "./view"
                }
            });
            gulp.watch(paths.styles.src, style);
            gulp.watch(paths.htmls.src, html);
            gulp.watch("src/**/*.html").on('change', browserSync.reload);
        };

    exports.style = style;
    exports.html = html;
    exports.watch = watch;

    var build = gulp.parallel(style, html, watch);

    gulp.task('default', working);

然后我只需执行gulp进行构建并使用自动重新加载进行监视。

© www.soinside.com 2019 - 2024. All rights reserved.