|
| 1 | +# Minimal BrowserSync setup with Gulp 4 |
| 2 | + |
| 3 | +[BrowserSync](https://www.browsersync.io/) is a great tool to streamline |
| 4 | +the development process with the ability to reflect code changes instantaneously |
| 5 | +in the browser through live-reloading. Setting up a live-reloading |
| 6 | +BrowserSync server with Gulp 4 is very clean and easy. |
| 7 | + |
| 8 | +## Step 1: Install the dependencies |
| 9 | + |
| 10 | +``` |
| 11 | +npm install --save-dev browser-sync |
| 12 | +``` |
| 13 | + |
| 14 | +## Step 2: Setup the project structure |
| 15 | + |
| 16 | +``` |
| 17 | +src/ |
| 18 | + scripts/ |
| 19 | + |__ index.js |
| 20 | +dist/ |
| 21 | + scripts/ |
| 22 | +index.html |
| 23 | +gulpfile.babel.js |
| 24 | +``` |
| 25 | + |
| 26 | +The goal here is to be able to: |
| 27 | +- Build the source script file in `src/scripts/`, e.g. compiling with babel, minifying, etc. |
| 28 | +- Put the compiled version in `dist/scripts` for use in `index.html` |
| 29 | +- Watch for changes in the source file and rebuild the `dist` package |
| 30 | +- With each rebuild of the `dist` package, reload the browser to immediately reflect the changes |
| 31 | + |
| 32 | +## Step 3: Write the gulpfile |
| 33 | + |
| 34 | +The gulpfile could be broken in 3 parts. |
| 35 | + |
| 36 | +### 1. Write the task to prepare the dist package as usual |
| 37 | + |
| 38 | +Refer to the main [README](https://github.com/gulpjs/gulp/blob/4.0/README.md#use-last-javascript-version-in-your-gulpfile) |
| 39 | +for more information. |
| 40 | + |
| 41 | +```javascript |
| 42 | +import babel from 'gulp-babel'; |
| 43 | +import concat from 'gulp-concat'; |
| 44 | +import del from 'del'; |
| 45 | +import gulp from 'gulp'; |
| 46 | +import uglify from 'gulp-uglify'; |
| 47 | + |
| 48 | +const paths = { |
| 49 | + scripts: { |
| 50 | + src: 'src/scripts/*.js', |
| 51 | + dest: 'dist/scripts/' |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +const clean = () => del(['dist']); |
| 56 | + |
| 57 | +function scripts() { |
| 58 | + return gulp.src(paths.scripts.src, { sourcemaps: true }) |
| 59 | + .pipe(babel()) |
| 60 | + .pipe(uglify()) |
| 61 | + .pipe(concat('index.min.js')) |
| 62 | + .pipe(gulp.dest(paths.scripts.dest)); |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### 2. Setup the BrowserSync server |
| 67 | + |
| 68 | +And write the tasks to serve and reload the server accordingly. |
| 69 | + |
| 70 | +```javascript |
| 71 | +import browserSync from 'browser-sync'; |
| 72 | +const server = browserSync.create(); |
| 73 | + |
| 74 | +function reload(done) { |
| 75 | + server.reload(); |
| 76 | + done(); |
| 77 | +} |
| 78 | + |
| 79 | +function serve(done) { |
| 80 | + server.init({ |
| 81 | + server: { |
| 82 | + baseDir: './' |
| 83 | + } |
| 84 | + }); |
| 85 | + done(); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### 3. Watch for source change, rebuild the scripts and reload the server |
| 90 | + |
| 91 | +This is trivially accomplished with `gulp.series` |
| 92 | + |
| 93 | +```javascript |
| 94 | +const watch = () => gulp.watch(paths.scripts.src, gulp.series(scripts, reload)); |
| 95 | +``` |
| 96 | + |
| 97 | +## Step 4: Bring it all together |
| 98 | + |
| 99 | +The last step is to expose the default task |
| 100 | + |
| 101 | +```javascript |
| 102 | +const dev = gulp.series(clean, scripts, serve, watch); |
| 103 | +export default dev; |
| 104 | +``` |
| 105 | + |
| 106 | +And profit |
| 107 | + |
| 108 | +```bash |
| 109 | +$ gulp |
| 110 | +``` |
| 111 | + |
| 112 | +Now if you go to [http://localhost:3000](http://localhost:3000), which is the default address of the |
| 113 | +BrowserSync server, you will see that the end result in the browser is updated everytime you change |
| 114 | +the content of the source file. Here is the whole gulpfile: |
| 115 | + |
| 116 | +```javascript |
| 117 | +import babel from 'gulp-babel'; |
| 118 | +import concat from 'gulp-concat'; |
| 119 | +import del from 'del'; |
| 120 | +import gulp from 'gulp'; |
| 121 | +import uglify from 'gulp-uglify'; |
| 122 | +import browserSync from 'browser-sync'; |
| 123 | + |
| 124 | +const server = browserSync.create(); |
| 125 | + |
| 126 | +const paths = { |
| 127 | + scripts: { |
| 128 | + src: 'src/scripts/*.js', |
| 129 | + dest: 'dist/scripts/' |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +const clean = () => del(['dist']); |
| 134 | + |
| 135 | +function scripts() { |
| 136 | + return gulp.src(paths.scripts.src, { sourcemaps: true }) |
| 137 | + .pipe(babel()) |
| 138 | + .pipe(uglify()) |
| 139 | + .pipe(concat('index.min.js')) |
| 140 | + .pipe(gulp.dest(paths.scripts.dest)); |
| 141 | +} |
| 142 | + |
| 143 | +function reload(done) { |
| 144 | + server.reload(); |
| 145 | + done(); |
| 146 | +} |
| 147 | + |
| 148 | +function serve(done) { |
| 149 | + server.init({ |
| 150 | + server: { |
| 151 | + baseDir: './' |
| 152 | + } |
| 153 | + }); |
| 154 | + done(); |
| 155 | +} |
| 156 | + |
| 157 | +const watch = () => gulp.watch(paths.scripts.src, gulp.series(scripts, reload)); |
| 158 | + |
| 159 | +const dev = gulp.series(clean, scripts, serve, watch); |
| 160 | +export default dev; |
| 161 | +``` |
0 commit comments