GULP 4 SASS 用MAMP注射浏览器



我已经花了整整一天的时间来正确地解决这个问题。我知道吞咽4&browsersync:样式注射?并尝试了不同的方法。我想念什么?

我试图让Gulp向浏览器注入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

我缺少什么?

我不知道我是否完全了解您想要什么。

要使用浏览器自动刷新浏览器,保存了您的最后一个更改后,我在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);

然后i 只需执行gulp 用于构建和观看自动重新加载。

最新更新