Académique Documents
Professionnel Documents
Culture Documents
Our API is incredibly simple & powerful. You can use it to create your own tiny node
program for local development or integrate with other tools. To use it, simply
require the Browsersync module like you would any other. This will give you access
to the public methods detailed below.
browserSync({server: "./app"});
browserSync.reload("core.css");
bs.init({
server: "./app"
});
bs.reload("*.html");
name
Type: String
Get a single instance by name. This is useful if you have your build scripts in separate
files
name
Type: String
bs.init({
server: true
});
bs.watch('*.html').on('change', bs.reload);
name
Type: String
Start the Browsersync service. This will launch a server, proxy or start the snippet
mode depending on your use-case.
config
Type: Object [optional]
This is the main configuration for your Browsersync instance and can contain
any of the available options If you do not pass a config an argument for
configuration, Browsersync will still run; but it will be in the snippet mode
cb
Type: Function [optional]
var bs = require("browser-sync").create();
bs.init({
server: "./app"
});
bs.init({
proxy: "http://www.bbc.co.uk"
});
The reload method will inform all browsers about changed files and will either cause
the browser to refresh, or inject the files where possible.
arg
Type: String | Array | Object [optional]
// browser reload
bs.reload();
// single file
bs.reload("styles.css");
// multiple files
bs.reload(["styles.css", "ie.css"]);
bs.reload("*.css");
The stream method returns a transform stream and can act once or on many files.
opts
Type: Object [optional]
gulp.task('sass', function () {
return gulp.src('scss/styles.scss')
.pipe(sass({includePaths: ['scss']}))
.pipe(gulp.dest('css'))
.pipe(bs.stream());
});
gulp.task('templates', function () {
return gulp.src('*.jade')
.pipe(jade())
.pipe(gulp.dest('app'))
.pipe(bs.stream({once: true}));
});
gulp.task('less', function () {
return gulp.src('*.less')
.pipe(less())
.pipe(gulp.dest('css'))
.pipe(bs.stream({match: "**/*.css"}));
});
msg
Type: String | HTML
timeout
Type: Number [optional]
How long the message will remain in the browser. @since 1.3.0
var bs = require("browser-sync").create();
// Text message
// HTML message
.exit() ^ TOP
This method will close any running server, stop file watching & exit the current
process.
var bs = require("browser-sync").create();
bs.init({server: "./app"});
setTimeout(function () {
bs.exit();
}, 5000);
.watch( patterns, opts, fn ) ^ TOP
Stand alone file-watcher. Use this along with Browsersync to create your own,
minimal build system
patterns
Type: String
opts
Type: Object [optional]
fn
Type: Function [optional]
bs.watch("*.html").on("change", bs.reload);
bs.reload("*.css");
});
bs.init({
server: "./app"
});
.pause() ^ TOP
.resume() ^ TOP
.emitter ^ TOP
The internal Event Emitter used by the running Browsersync instance (if there is one).
You can use this to emit your own events, such as changed files, logging etc.
var bs = require("browser-sync").create();
bs.emitter.on("init", function () {
console.log("Browsersync is running!");
});
bs.init(config);
.active ^ TOP
A simple true/false flag that you can use to determine if there's a currently-running
Browsersync instance.
var bs = require("browser-sync").create();
console.log(bs.active);
});
.paused ^ TOP