Skip to content

Commit e1afdfd

Browse files
jeremenichelliphated
authored andcommittedDec 31, 2017
Docs: Add ES2015 gulpfile example to readme
1 parent d90198c commit e1afdfd

File tree

1 file changed

+143
-41
lines changed

1 file changed

+143
-41
lines changed
 

‎README.md

+143-41
Original file line numberDiff line numberDiff line change
@@ -28,65 +28,167 @@ This file will give you a taste of what gulp does.
2828

2929
```js
3030
var gulp = require('gulp');
31-
var coffee = require('gulp-coffee');
31+
var less = require('gulp-less');
32+
var babel = require('gulp-babel');
3233
var concat = require('gulp-concat');
3334
var uglify = require('gulp-uglify');
34-
var imagemin = require('gulp-imagemin');
35-
var sourcemaps = require('gulp-sourcemaps');
35+
var rename = require('gulp-rename');
36+
var cleanCSS = require('gulp-clean-css');
3637
var del = require('del');
3738

3839
var paths = {
39-
scripts: ['client/js/**/*.coffee', '!client/external/**/*.coffee'],
40-
images: 'client/img/**/*'
40+
styles: {
41+
src: 'src/styles/**/*.less',
42+
dest: 'assets/styles/'
43+
},
44+
scripts: {
45+
src: 'src/scripts/**/*.js',
46+
dest: 'assets/scripts/'
47+
}
4148
};
4249

43-
/* Register some tasks to expose to the cli */
44-
gulp.task('build', gulp.series(
45-
clean,
46-
gulp.parallel(scripts, images)
47-
));
48-
gulp.task(clean);
49-
gulp.task(watch);
50+
/* Not all tasks need to use streams, a gulpfile is just another node program
51+
* and you can use all packages available on npm, but it must return either a
52+
* Promise, a Stream or take a callback and call it
53+
*/
54+
function clean() {
55+
// You can use multiple globbing patterns as you would with `gulp.src`,
56+
// for example if you are using del 2.0 or above, return its promise
57+
return del([ 'assets' ]);
58+
}
5059

51-
// The default task (called when you run `gulp` from cli)
52-
gulp.task('default', gulp.series('build'));
60+
/*
61+
* Define our tasks using plain functions
62+
*/
63+
function styles() {
64+
return gulp.src(paths.styles.src)
65+
.pipe(less())
66+
.pipe(cleanCSS())
67+
// pass in options to the stream
68+
.pipe(rename({
69+
basename: 'main',
70+
suffix: '.min'
71+
}))
72+
.pipe(gulp.dest(paths.styles.dest));
73+
}
5374

75+
function scripts() {
76+
return gulp.src(paths.scripts.src, { sourcemaps: true })
77+
.pipe(babel())
78+
.pipe(uglify())
79+
.pipe(concat('main.min.js'))
80+
.pipe(gulp.dest(paths.scripts.dest));
81+
}
5482

55-
/* Define our tasks using plain functions */
83+
function watch() {
84+
gulp.watch(paths.scripts.src, scripts);
85+
gulp.watch(paths.styles.src, styles);
86+
}
5687

57-
// Not all tasks need to use streams
58-
// But it must return either a Promise or Stream or take a Callback and call it
59-
function clean() {
60-
// You can use multiple globbing patterns as you would with `gulp.src`
61-
// If you are using del 2.0 or above, return its promise
62-
return del(['build']);
88+
/*
89+
* You can use CommonJS `exports` module notation to declare tasks
90+
*/
91+
exports.clean = clean;
92+
exports.styles = styles;
93+
exports.scripts = scripts;
94+
exports.watch = watch;
95+
96+
/*
97+
* Specify if tasks run in series or parallel using `gulp.series` and `gulp.parallel`
98+
*/
99+
var build = gulp.series(clean, gulp.parallel(styles, scripts));
100+
101+
/*
102+
* You can still use `gulp.task` to expose tasks
103+
*/
104+
gulp.task('build', build);
105+
106+
/*
107+
* Define default task that can be called by just running `gulp` from cli
108+
*/
109+
gulp.task('default', build);
110+
```
111+
112+
## Use latest JavaScript version in your gulpfile
113+
114+
Node already supports a lot of **ES2015**, to avoid compatibility problem we suggest to install Babel and rename your `gulpfile.js` as `gulpfile.babel.js`.
115+
116+
```sh
117+
npm install --save-dev babel-register babel-preset-es2015
118+
```
119+
120+
Then create a **.babelrc** file with the preset configuration.
121+
122+
```js
123+
{
124+
"presets": [ "es2015" ]
63125
}
126+
```
64127

65-
// Copy all static images
66-
function images() {
67-
return gulp.src(paths.images)
68-
// Pass in options to the task
69-
.pipe(imagemin({optimizationLevel: 5}))
70-
.pipe(gulp.dest('build/img'));
128+
And here's the same sample from above written in **ES2015**.
129+
130+
```js
131+
import gulp from 'gulp';
132+
import less from 'gulp-less';
133+
import babel from 'gulp-babel';
134+
import concat from 'gulp-concat';
135+
import uglify from 'gulp-uglify';
136+
import rename from 'gulp-rename';
137+
import cleanCSS from 'gulp-clean-css';
138+
import del from 'del';
139+
140+
const paths = {
141+
styles: {
142+
src: 'src/styles/**/*.less',
143+
dest: 'assets/styles/'
144+
},
145+
scripts: {
146+
src: 'src/scripts/**/*.js',
147+
dest: 'assets/scripts/'
148+
}
149+
};
150+
151+
/*
152+
* For small tasks you can use arrow functions and export
153+
*/
154+
const clean = () => del([ 'assets' ]);
155+
export { clean };
156+
157+
/*
158+
* You can still declare named functions and export them as tasks
159+
*/
160+
export function styles() {
161+
return gulp.src(paths.styles.src)
162+
.pipe(less())
163+
.pipe(cleanCSS())
164+
// pass in options to the stream
165+
.pipe(rename({
166+
basename: 'main',
167+
suffix: '.min'
168+
}))
169+
.pipe(gulp.dest(paths.styles.dest));
71170
}
72171

73-
// Minify and copy all JavaScript (except vendor scripts)
74-
// with sourcemaps all the way down
75-
function scripts() {
76-
return gulp.src(paths.scripts)
77-
.pipe(sourcemaps.init())
78-
.pipe(coffee())
79-
.pipe(uglify())
80-
.pipe(concat('all.min.js'))
81-
.pipe(sourcemaps.write())
82-
.pipe(gulp.dest('build/js'));
172+
export function scripts() {
173+
return gulp.src(paths.scripts.src, { sourcemaps: true })
174+
.pipe(babel())
175+
.pipe(uglify())
176+
.pipe(concat('main.min.js'))
177+
.pipe(gulp.dest(paths.scripts.dest));
83178
}
84179

85-
// Rerun the task when a file changes
86-
function watch() {
87-
gulp.watch(paths.scripts, scripts);
88-
gulp.watch(paths.images, images);
180+
export function watch() {
181+
gulp.watch(paths.scripts.src, scripts);
182+
gulp.watch(paths.styles.src, styles);
89183
}
184+
185+
const build = gulp.series(clean, gulp.parallel(styles, scripts));
186+
export { build };
187+
188+
/*
189+
* Export a default task
190+
*/
191+
export default build;
90192
```
91193

92194
## Incremental Builds

0 commit comments

Comments
 (0)
Please sign in to comment.