Skip to content

Commit 275945d

Browse files
committed
Gulp with componets
1 parent fce129c commit 275945d

File tree

5 files changed

+198
-141
lines changed

5 files changed

+198
-141
lines changed

gulp/build.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* eslint-disable no-var, strict, prefer-arrow-callback */
2+
'use strict';
3+
4+
var gulp = require('gulp');
5+
var browserify = require('browserify');
6+
var source = require('vinyl-source-stream');
7+
var buffer = require('vinyl-buffer');
8+
var uglify = require('gulp-uglify');
9+
var sourcemaps = require('gulp-sourcemaps');
10+
var gutil = require('gulp-util');
11+
var through = require('through2');
12+
var globby = require('globby');
13+
var config = require('./config');
14+
15+
module.exports = {
16+
browserify: function() {
17+
// gulp expects tasks to return a stream, so we create one here.
18+
var bundledStream = through();
19+
20+
bundledStream
21+
// turns the output bundle stream into a stream containing
22+
// the normal attributes gulp plugins expect.
23+
.pipe(source('eid.js'))
24+
// the rest of the gulp task, as you would normally write it.
25+
// here we're copying from the Browserify + Uglify2 recipe.
26+
.pipe(buffer())
27+
.on('error', gutil.log)
28+
.pipe(gulp.dest(config.dist + '/browser/js'));
29+
30+
// "globby" replaces the normal "gulp.src" as Browserify
31+
// creates it's own readable stream.
32+
globby(config.sources).then(function(entries) {
33+
// create the Browserify instance.
34+
var b = browserify({
35+
entries: entries,
36+
debug: true
37+
});
38+
39+
// pipe the Browserify stream into the stream we created earlier
40+
// this starts our gulp pipeline.
41+
b.bundle().pipe(bundledStream);
42+
}).catch(function(err) {
43+
// ensure any errors from globby are handled
44+
bundledStream.emit('error', err);
45+
});
46+
47+
// finally, we return the stream, so gulp knows when this task is done.
48+
return bundledStream;
49+
},
50+
minified: function() {
51+
// gulp expects tasks to return a stream, so we create one here.
52+
var bundledStream = through();
53+
54+
bundledStream
55+
// turns the output bundle stream into a stream containing
56+
// the normal attributes gulp plugins expect.
57+
.pipe(source('eid.min.js'))
58+
// the rest of the gulp task, as you would normally write it.
59+
// here we're copying from the Browserify + Uglify2 recipe.
60+
.pipe(buffer())
61+
.pipe(sourcemaps.init({loadMaps: true}))
62+
// Add gulp plugins to the pipeline here.
63+
.pipe(uglify())
64+
.on('error', gutil.log)
65+
.pipe(sourcemaps.write('./'))
66+
.pipe(gulp.dest(config.dist + '/browser/js'));
67+
68+
// "globby" replaces the normal "gulp.src" as Browserify
69+
// creates it's own readable stream.
70+
globby(config.sources).then(function(entries) {
71+
// create the Browserify instance.
72+
var b = browserify({
73+
entries: entries,
74+
debug: true
75+
});
76+
77+
// pipe the Browserify stream into the stream we created earlier
78+
// this starts our gulp pipeline.
79+
b.bundle().pipe(bundledStream);
80+
}).catch(function(err) {
81+
// ensure any errors from globby are handled
82+
bundledStream.emit('error', err);
83+
});
84+
85+
// finally, we return the stream, so gulp knows when this task is done.
86+
return bundledStream;
87+
}
88+
};

gulp/clean.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-disable no-var, strict, prefer-arrow-callback */
2+
'use strict';
3+
4+
var fs = require('fs');
5+
var config = require('./config');
6+
7+
var deleteFolderRecursive = function(path) {
8+
var files = [];
9+
if( fs.existsSync(path) ) {
10+
files = fs.readdirSync(path);
11+
files.forEach(function(file,index){
12+
var curPath = path + "/" + file;
13+
if(fs.lstatSync(curPath).isDirectory()) { // recurse
14+
deleteFolderRecursive(curPath);
15+
} else { // delete file
16+
fs.unlinkSync(curPath);
17+
}
18+
});
19+
fs.rmdirSync(path);
20+
}
21+
};
22+
23+
module.exports = function() {
24+
deleteFolderRecursive(config.target);
25+
deleteFolderRecursive(config.dist);
26+
}

gulp/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable no-var, strict, prefer-arrow-callback */
2+
'use strict';
3+
4+
module.exports = {
5+
sources: ['lib/**/*.js'],
6+
testSources: ['test/**/*.js'],
7+
target: 'target',
8+
dist: 'dist'
9+
};

gulp/tests.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint-disable no-var, strict, prefer-arrow-callback */
2+
'use strict';
3+
4+
var gulp = require('gulp');
5+
var eslint = require('gulp-eslint');
6+
var mocha = require('gulp-mocha');
7+
var cover = require('gulp-coverage');
8+
var config = require('./config');
9+
10+
module.exports = {
11+
lint: function () {
12+
return gulp.src(config.sources)
13+
.pipe(eslint({
14+
extends: 'eslint:recommended',
15+
rules: {
16+
strict: 2
17+
},
18+
env: {
19+
node: true
20+
}
21+
}))
22+
.pipe(eslint.format())
23+
.pipe(eslint.failAfterError());
24+
},
25+
specs: function (done) {
26+
var prependToAll = function(path, globs) {
27+
var ret = [];
28+
for (var v of globs) {
29+
ret.push(path + '/' + v);
30+
}
31+
return ret;
32+
};
33+
var fs = require('fs');
34+
var target = config.target;
35+
if (!fs.existsSync(target)) {
36+
fs.mkdirSync(target);
37+
}
38+
var pwd = process.cwd();
39+
process.chdir(target);
40+
var testSrc = prependToAll('..', config.testSources);
41+
var src = prependToAll('..', config.sources);
42+
return gulp.src(testSrc, { read: false })
43+
.pipe(cover.instrument({
44+
pattern: src,
45+
debugDirectory: 'debug'
46+
}))
47+
.pipe(mocha())
48+
.pipe(cover.gather())
49+
.pipe(cover.enforce({
50+
statements: 98,
51+
blocks: 98,
52+
lines: 98,
53+
}))
54+
.pipe(cover.format([
55+
{ reporter: 'html', outFile: 'coverage.html' },
56+
{ reporter: 'json', outFile: 'coverage.json' },
57+
{ reporter: 'lcov', outFile: 'coverage.lcov' },
58+
]))
59+
.pipe(gulp.dest('reports'))
60+
.on('end', function() {
61+
process.chdir(pwd);
62+
});
63+
}
64+
};

gulpfile.js

Lines changed: 11 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -2,149 +2,19 @@
22
'use strict';
33

44
var gulp = require('gulp');
5-
var eslint = require('gulp-eslint');
6-
var mocha = require('gulp-mocha');
7-
var cover = require('gulp-coverage');
8-
var browserify = require('browserify');
9-
var source = require('vinyl-source-stream');
10-
var buffer = require('vinyl-buffer');
11-
var uglify = require('gulp-uglify');
12-
var sourcemaps = require('gulp-sourcemaps');
13-
var gutil = require('gulp-util');
14-
var through = require('through2');
15-
var globby = require('globby');
165

17-
var sources = ['lib/**/*.js'];
18-
var testSources = ['test/**/*.js'];
6+
var tests = require('./gulp/tests');
7+
var build = require('./gulp/build');
8+
var clean = require('./gulp/clean');
199

20-
gulp.task('lint', function () {
21-
return gulp.src(sources)
22-
.pipe(eslint({
23-
extends: 'eslint:recommended',
24-
rules: {
25-
strict: 2
26-
},
27-
env: {
28-
node: true
29-
}
30-
}))
31-
.pipe(eslint.format())
32-
.pipe(eslint.failAfterError());
33-
});
34-
35-
gulp.task('test', ['lint'], function (done) {
36-
var prependToAll = function(path, globs) {
37-
var ret = [];
38-
for (var v of globs) {
39-
ret.push(path + '/' + v);
40-
}
41-
return ret;
42-
};
43-
var fs = require('fs');
44-
var target = 'target';
45-
if (!fs.existsSync(target)) {
46-
fs.mkdirSync(target);
47-
}
48-
var pwd = process.cwd();
49-
process.chdir(target);
50-
var testSrc = prependToAll('..', testSources);
51-
var src = prependToAll('..', sources);
52-
return gulp.src(testSrc, { read: false })
53-
.pipe(cover.instrument({
54-
pattern: src,
55-
debugDirectory: 'debug'
56-
}))
57-
.pipe(mocha())
58-
.pipe(cover.gather())
59-
.pipe(cover.enforce({
60-
statements: 98,
61-
blocks: 98,
62-
lines: 98,
63-
}))
64-
.pipe(cover.format([
65-
{ reporter: 'html', outFile: 'coverage.html' },
66-
{ reporter: 'json', outFile: 'coverage.json' },
67-
{ reporter: 'lcov', outFile: 'coverage.lcov' },
68-
]))
69-
.pipe(gulp.dest('reports'))
70-
.on('end', function() {
71-
process.chdir(pwd);
72-
});
73-
});
10+
gulp.task('clean', clean);
11+
gulp.task('lint', tests.lint);
12+
gulp.task('specs', ['lint'], tests.specs);
13+
gulp.task('test', ['lint', 'specs']);
7414
gulp.task('watch', function() {
7515
gulp.watch(sources.concat(testSources), ['test']);
7616
});
77-
gulp.task('build-browser', ['test'], function() {
78-
// gulp expects tasks to return a stream, so we create one here.
79-
var bundledStream = through();
80-
81-
bundledStream
82-
// turns the output bundle stream into a stream containing
83-
// the normal attributes gulp plugins expect.
84-
.pipe(source('eid.js'))
85-
// the rest of the gulp task, as you would normally write it.
86-
// here we're copying from the Browserify + Uglify2 recipe.
87-
.pipe(buffer())
88-
.on('error', gutil.log)
89-
.pipe(gulp.dest('dist/browser/js/'));
90-
91-
// "globby" replaces the normal "gulp.src" as Browserify
92-
// creates it's own readable stream.
93-
globby(['./lib/**/*.js']).then(function(entries) {
94-
// create the Browserify instance.
95-
var b = browserify({
96-
entries: entries,
97-
debug: true
98-
});
99-
100-
// pipe the Browserify stream into the stream we created earlier
101-
// this starts our gulp pipeline.
102-
b.bundle().pipe(bundledStream);
103-
}).catch(function(err) {
104-
// ensure any errors from globby are handled
105-
bundledStream.emit('error', err);
106-
});
107-
108-
// finally, we return the stream, so gulp knows when this task is done.
109-
return bundledStream;
110-
});
111-
gulp.task('build-minified', ['test'], function() {
112-
// gulp expects tasks to return a stream, so we create one here.
113-
var bundledStream = through();
114-
115-
bundledStream
116-
// turns the output bundle stream into a stream containing
117-
// the normal attributes gulp plugins expect.
118-
.pipe(source('eid.min.js'))
119-
// the rest of the gulp task, as you would normally write it.
120-
// here we're copying from the Browserify + Uglify2 recipe.
121-
.pipe(buffer())
122-
.pipe(sourcemaps.init({loadMaps: true}))
123-
// Add gulp plugins to the pipeline here.
124-
.pipe(uglify())
125-
.on('error', gutil.log)
126-
.pipe(sourcemaps.write('./'))
127-
.pipe(gulp.dest('dist/browser/js/'));
128-
129-
// "globby" replaces the normal "gulp.src" as Browserify
130-
// creates it's own readable stream.
131-
globby(['./lib/**/*.js']).then(function(entries) {
132-
// create the Browserify instance.
133-
var b = browserify({
134-
entries: entries,
135-
debug: true
136-
});
137-
138-
// pipe the Browserify stream into the stream we created earlier
139-
// this starts our gulp pipeline.
140-
b.bundle().pipe(bundledStream);
141-
}).catch(function(err) {
142-
// ensure any errors from globby are handled
143-
bundledStream.emit('error', err);
144-
});
145-
146-
// finally, we return the stream, so gulp knows when this task is done.
147-
return bundledStream;
148-
});
149-
gulp.task('build', ['build-minified', 'build-browser']);
150-
gulp.task('default', ['lint', 'test', 'build']);
17+
gulp.task('build-browser', ['test'], build.browserify);
18+
gulp.task('build-minified', ['test'], build.minified);
19+
gulp.task('build', ['build-browser', 'build-minified']);
20+
gulp.task('default', ['test', 'build']);

0 commit comments

Comments
 (0)