1

我正在尝试将livereload添加到西兰花

不幸的是,live-reload 插件文档有点短,我无法让它工作。在文档中,声明执行以下操作:

var injectLivereload = require('broccoli-inject-livereload');

var public = injectLivereload('public');

我认为这应该放在Brocfile.js(对吗?)。但是无论我什么都不做都会重新加载(我必须点击重新加载才能刷新)我也改变了'public'我认为代表一个目录的部分。任何帮助,将不胜感激。

4

2 回答 2

3

我正在使用 BrowserSync 而不是“仅”LiveReload。它还支持 LiveReload(以及用于 CSS 的 LiveInject),但它还具有大量其他功能(如重影)。

让我们创建一个名为的文件server.js和一个名为的文件夹app,在它旁边放置我们的index.html,.css.js. 这server.js包含:

var broccoli = require("broccoli");
var brocware = require("broccoli/lib/middleware");
var mergeTrees = require("broccoli-merge-trees");
var Watcher = require("broccoli-sane-watcher");
var browserSync = require("browser-sync");

var tree = mergeTrees(["app"]); // your public directory
var builder = new broccoli.Builder(tree);
var watcher = new Watcher(builder);

watcher.on("change", function(results) {
    if (!results.filePath) return;

    // Enable CSS live inject
    if (results.filePath.indexOf("css") > -1) {
        return browserSync.reload("*.css");
    }

    browserSync.reload();
});

browserSync({
    server: {
        baseDir: "./",
        middleware: brocware(watcher)
    }
});

现在启动服务器(它将自动打开浏览器):

node server.js

我知道这不像 Gulp 或 Grunt 乍一看那么简单,但它提供了对所有内容的细粒度控制,而且速度非常快,即使您的应用程序不断增长。

于 2015-09-11T10:41:08.983 回答
2

Instead of Livereload I opted to use Browsersync via the Broccoli Browser Sync plugin

My final Brocfile.js was very similar to (pulled from plugins npm page):

var fastBrowserify = require('broccoli-fast-browserify');
var babelify = require('babelify');
var mergeTrees = require('broccoli-merge-trees');
var compileSass = require('broccoli-sass-source-maps');
var funnel = require('broccoli-funnel');
var BrowserSync = require('broccoli-browser-sync');

var optionalTransforms = [
  'regenerator'
  // 'minification.deadCodeElimination', 
  // 'minification.inlineExpressions' 
];

var babelOptions = {stage: 0, optional: optionalTransforms, compact: true};

// var browserifyOpts = {deps: true, entries: files, noParse: noParse, ignoreMissing: true}; 
var transformedBabelify = fastBrowserify('app', {
  browserify: {
    extensions: [".js"]
  },
  bundles: {
    'js/app.js': {
      entryPoints: ['app.js'],
      transform: {
        tr: babelify,
        options: {
          stage: 0
        }
      }
    }
  }
});

var appCss = compileSass(['piggy/frontend/app'], 'main.scss', 'css/app.css');

var staticFiles = funnel('frontend', {
  srcDir: 'static'
});

var browserSync = new BrowserSync([staticFiles, transformedBabelify, appCss]);

module.exports = mergeTrees([staticFiles, transformedBabelify, appCss, browserSync]);

With this solution I was able to continue using broccoli to serve my assets via broccoli serve and all my assets would be rebuilt then reloaded in the browser including my css.

于 2016-07-22T18:40:33.097 回答