Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a minimal setup for BrowserSync with Gulp 4 & ES2015 #1659

Merged
merged 1 commit into from
May 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions docs/recipes/minimal-browsersync-setup-with-gulp4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Minimal BrowserSync setup with Gulp 4

[BrowserSync](https://www.browsersync.io/) is a great tool to streamline
the development process with the ability to reflect code changes instantaneously
in the browser through live-reloading. Setting up a live-reloading
BrowserSync server with Gulp 4 is very clean and easy.

## Step 1: Install the dependencies

```
npm install --save-dev browser-sync
```

## Step 2: Setup the project structure

```
src/
scripts/
|__ index.js
dist/
scripts/
index.html
gulpfile.babel.js
```

The goal here is to be able to:
- Build the source script file in `src/scripts/`, e.g. compiling with babel, minifying, etc.
- Put the compiled version in `dist/scripts` for use in `index.html`
- Watch for changes in the source file and rebuild the `dist` package
- With each rebuild of the `dist` package, reload the browser to immediately reflect the changes

## Step 3: Write the gulpfile

The gulpfile could be broken in 3 parts.

### 1. Write the task to prepare the dist package as usual

Refer to the main [README](https://github.com/gulpjs/gulp/blob/4.0/README.md#use-last-javascript-version-in-your-gulpfile)
for more information.

```javascript
import babel from 'gulp-babel';
import concat from 'gulp-concat';
import del from 'del';
import gulp from 'gulp';
import uglify from 'gulp-uglify';

const paths = {
scripts: {
src: 'src/scripts/*.js',
dest: 'dist/scripts/'
}
};

const clean = () => del(['dist']);

function scripts() {
return gulp.src(paths.scripts.src, { sourcemaps: true })
.pipe(babel())
.pipe(uglify())
.pipe(concat('index.min.js'))
.pipe(gulp.dest(paths.scripts.dest));
}
```

### 2. Setup the BrowserSync server

And write the tasks to serve and reload the server accordingly.

```javascript
import browserSync from 'browser-sync';
const server = browserSync.create();

function reload(done) {
server.reload();
done();
}

function serve(done) {
server.init({
server: {
baseDir: './'
}
});
done();
}
```

### 3. Watch for source change, rebuild the scripts and reload the server

This is trivially accomplished with `gulp.series`

```javascript
const watch = () => gulp.watch(paths.scripts.src, gulp.series(scripts, reload));
```

### 4. Expose the default task

```javascript
const dev = gulp.series(clean, scripts, serve, watch);
export default dev;
```

### 5. And profit

```bash
$ gulp
```

Now if you go to [http://localhost:3000](http://localhost:3000), which is the default address of the
BrowserSync server, you will see that the end result in the browser is updated everytime you change
the content of the source file. Here is the whole gulpfile:

```javascript
import babel from 'gulp-babel';
import concat from 'gulp-concat';
import del from 'del';
import gulp from 'gulp';
import uglify from 'gulp-uglify';
import browserSync from 'browser-sync';

const server = browserSync.create();

const paths = {
scripts: {
src: 'src/scripts/*.js',
dest: 'dist/scripts/'
}
};

const clean = () => del(['dist']);

function scripts() {
return gulp.src(paths.scripts.src, { sourcemaps: true })
.pipe(babel())
.pipe(uglify())
.pipe(concat('index.min.js'))
.pipe(gulp.dest(paths.scripts.dest));
}

function reload(done) {
browserSync.reload();
done();
}

function serve(done) {
server.init({
server: {
baseDir: './'
}
});
done();
}

const watch = () => gulp.watch(paths.scripts.src, gulp.series(scripts, reload));

const dev = gulp.series(clean, scripts, serve, watch);
export default dev;
```