Skip to content

Commit 88d283b

Browse files
committed
🍹 add gulpfile & UMD
1 parent c8b16d1 commit 88d283b

File tree

13 files changed

+447
-32
lines changed

13 files changed

+447
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
bower_components/
22
node_modules/
3+
package-lock.json

.jshintrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"browser": true,
3+
"unused": true,
4+
"undef": true,
5+
"globals": {
6+
"console": false
7+
}
8+
}

gulpfile.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*jshint node: true, strict: false */
2+
3+
var fs = require('fs');
4+
var gulp = require('gulp');
5+
var rename = require('gulp-rename');
6+
var replace = require('gulp-replace');
7+
8+
// ----- hint ----- //
9+
10+
var jshint = require('gulp-jshint');
11+
12+
gulp.task( 'hint-js', function() {
13+
return gulp.src('js/**/*.js')
14+
.pipe( jshint() )
15+
.pipe( jshint.reporter('default') );
16+
});
17+
18+
gulp.task( 'hint-test', function() {
19+
return gulp.src('test/unit/*.js')
20+
.pipe( jshint() )
21+
.pipe( jshint.reporter('default') );
22+
});
23+
24+
gulp.task( 'hint-task', function() {
25+
return gulp.src('gulpfile.js')
26+
.pipe( jshint() )
27+
.pipe( jshint.reporter('default') );
28+
});
29+
30+
var jsonlint = require('gulp-json-lint');
31+
32+
gulp.task( 'jsonlint', function() {
33+
return gulp.src( '*.json' )
34+
.pipe( jsonlint() )
35+
.pipe( jsonlint.report('verbose') );
36+
});
37+
38+
gulp.task( 'hint', [ 'hint-js', 'hint-test', 'hint-task', 'jsonlint' ]);
39+
40+
// -------------------------- RequireJS makes pkgd -------------------------- //
41+
42+
var gutil = require('gulp-util');
43+
var chalk = require('chalk');
44+
var rjsOptimize = require('gulp-requirejs-optimize');
45+
46+
// regex for banner comment
47+
var reBannerComment = new RegExp('^\\s*(?:\\/\\*[\\s\\S]*?\\*\\/)\\s*');
48+
49+
function getBanner() {
50+
var src = fs.readFileSync( 'js/index.js', 'utf8' );
51+
var matches = src.match( reBannerComment );
52+
var banner = matches[0].replace( 'Infinite Scroll', 'Infinite Scroll PACKAGED' );
53+
return banner;
54+
}
55+
56+
function addBanner( str ) {
57+
return replace( /^/, str );
58+
}
59+
60+
gulp.task( 'requirejs', function() {
61+
var banner = getBanner();
62+
// HACK src is not needed
63+
// should refactor rjsOptimize to produce src
64+
return gulp.src('js/index.js')
65+
.pipe( rjsOptimize({
66+
baseUrl: 'bower_components',
67+
optimize: 'none',
68+
include: [
69+
'jquery-bridget/jquery-bridget',
70+
'infinite-scroll/js/index',
71+
'imagesloaded/imagesloaded'
72+
],
73+
paths: {
74+
'infinite-scroll': '../',
75+
jquery: 'empty:'
76+
}
77+
}) )
78+
// remove named module
79+
// .pipe( replace( "'infinite-scroll/js/index',", '' ) )
80+
// add banner
81+
.pipe( addBanner( banner ) )
82+
.pipe( rename('infinite-scroll.pkgd.js') )
83+
.pipe( gulp.dest('dist') );
84+
});
85+
86+
87+
// ----- uglify ----- //
88+
89+
var uglify = require('gulp-uglify');
90+
91+
gulp.task( 'uglify', [ 'requirejs' ], function() {
92+
var banner = getBanner();
93+
gulp.src('dist/infinite-scroll.pkgd.js')
94+
.pipe( uglify() )
95+
// add banner
96+
.pipe( addBanner( banner ) )
97+
.pipe( rename('infinite-scroll.pkgd.min.js') )
98+
.pipe( gulp.dest('dist') );
99+
});
100+
101+
// ----- version ----- //
102+
103+
// set version in source files
104+
105+
var minimist = require('minimist');
106+
107+
// use gulp version -t 1.2.3
108+
gulp.task( 'version', function() {
109+
var args = minimist( process.argv.slice(3) );
110+
var version = args.t;
111+
if ( !version || !/\d\.\d\.\d/.test( version ) ) {
112+
gutil.log( 'invalid version: ' + chalk.red( version ) );
113+
return;
114+
}
115+
gutil.log( 'ticking version to ' + chalk.green( version ) );
116+
117+
gulp.src('js/index.js')
118+
.pipe( replace( /Infinite Scroll v\d\.\d+\.\d+/, 'Infinite Scroll v' + version ) )
119+
.pipe( gulp.dest('js') );
120+
121+
gulp.src('package.json')
122+
.pipe( replace( /"version": "\d\.\d+\.\d+"/, '"version": "' + version + '"' ) )
123+
.pipe( gulp.dest('.') );
124+
});
125+
126+
// ----- default ----- //
127+
128+
gulp.task( 'default', [
129+
'hint',
130+
'uglify',
131+
]);

js/button.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1-
var utils = fizzyUIUtils;
1+
// button
2+
( function( window, factory ) {
3+
// universal module definition
4+
/* globals define, module, require */
5+
if ( typeof define == 'function' && define.amd ) {
6+
// AMD
7+
define( [
8+
'./core',
9+
'fizzy-ui-utils/utils',
10+
], function( InfiniteScroll, utils ) {
11+
return factory( window, InfiniteScroll, utils );
12+
});
13+
} else if ( typeof module == 'object' && module.exports ) {
14+
// CommonJS
15+
module.exports = factory(
16+
window,
17+
require('./core'),
18+
require('fizzy-ui-utils')
19+
);
20+
} else {
21+
// browser global
22+
factory(
23+
window,
24+
window.InfiniteScroll,
25+
window.fizzyUIUtils
26+
);
27+
}
28+
29+
}( window, function factory( window, InfiniteScroll, utils ) {
230

331
// InfiniteScroll.defaults.button = null;
432

@@ -50,3 +78,11 @@ InfiniteScrollButton.prototype.hide = function() {
5078
InfiniteScrollButton.prototype.destroy = function() {
5179
this.element.removeEventListener( this.clickHandler );
5280
};
81+
82+
// -------------------------- -------------------------- //
83+
84+
InfiniteScroll.Button = InfiniteScrollButton;
85+
86+
return InfiniteScroll;
87+
88+
}));

js/core.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1-
var utils = fizzyUIUtils;
1+
// core
2+
( function( window, factory ) {
3+
// universal module definition
4+
/* globals define, module, require */
5+
if ( typeof define == 'function' && define.amd ) {
6+
// AMD
7+
define( [
8+
'ev-emitter/ev-emitter',
9+
'fizzy-ui-utils/utils',
10+
], function( EvEmitter, utils) {
11+
return factory( window, EvEmitter, utils );
12+
});
13+
} else if ( typeof module == 'object' && module.exports ) {
14+
// CommonJS
15+
module.exports = factory(
16+
window,
17+
require('ev-emitter'),
18+
require('fizzy-ui-utils')
19+
);
20+
} else {
21+
// browser global
22+
window.InfiniteScroll = factory(
23+
window,
24+
window.EvEmitter,
25+
window.fizzyUIUtils
26+
);
27+
}
28+
29+
}( window, function factory( window, EvEmitter, utils ) {
30+
231
var jQuery = window.jQuery;
332
// internal store of all InfiniteScroll intances
433
var instances = {};
@@ -343,3 +372,9 @@ utils.htmlInit( InfiniteScroll, 'infinite-scroll' );
343372
if ( jQuery && jQuery.bridget ) {
344373
jQuery.bridget( 'infiniteScroll', InfiniteScroll );
345374
}
375+
376+
// -------------------------- -------------------------- //
377+
378+
return InfiniteScroll;
379+
380+
}));

js/history.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1+
// history
2+
( function( window, factory ) {
3+
// universal module definition
4+
/* globals define, module, require */
5+
if ( typeof define == 'function' && define.amd ) {
6+
// AMD
7+
define( [
8+
'./core',
9+
'fizzy-ui-utils/utils',
10+
], function( InfiniteScroll, utils ) {
11+
return factory( window, InfiniteScroll, utils );
12+
});
13+
} else if ( typeof module == 'object' && module.exports ) {
14+
// CommonJS
15+
module.exports = factory(
16+
window,
17+
require('./core'),
18+
require('fizzy-ui-utils')
19+
);
20+
} else {
21+
// browser global
22+
factory(
23+
window,
24+
window.InfiniteScroll,
25+
window.fizzyUIUtils
26+
);
27+
}
28+
29+
}( window, function factory( window, InfiniteScroll, utils ) {
30+
131
var proto = InfiniteScroll.prototype;
2-
var utils = fizzyUIUtils;
332

433
InfiniteScroll.defaults.history = 'replace';
534
// InfiniteScroll.defaults.historyTitle = false;
@@ -167,3 +196,9 @@ proto.onBeforeunload = function() {
167196
proto.onPageLoadHistory = function( response, path ) {
168197
this.setHistory( response.title, path );
169198
};
199+
200+
// -------------------------- -------------------------- //
201+
202+
return InfiniteScroll;
203+
204+
}));

js/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*!
2+
* Infinite Scroll v3.0.0-beta.1
3+
* Automatically add next page
4+
*
5+
* Licensed GPLv3 for open source use
6+
* or Infinite Scroll Commercial License for commercial use
7+
*
8+
* https://infinite-scroll.com
9+
* Copyright 2017 Metafizzy
10+
*/
11+
12+
( function( window, factory ) {
13+
// universal module definition
14+
/* globals define, module, require */
15+
if ( typeof define == 'function' && define.amd ) {
16+
// AMD
17+
define( [
18+
'./core',
19+
'./page-load',
20+
'./scroll-watch',
21+
'./history',
22+
'./button',
23+
'./status',
24+
], factory );
25+
} else if ( typeof module == 'object' && module.exports ) {
26+
// CommonJS
27+
module.exports = factory(
28+
require('./core'),
29+
require('./page-load'),
30+
require('./scroll-watch'),
31+
require('./history'),
32+
require('./button'),
33+
require('./status')
34+
);
35+
}
36+
37+
})( window, function factory( InfiniteScroll ) {
38+
return InfiniteScroll;
39+
});

js/page-load.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
// page-load
2+
( function( window, factory ) {
3+
// universal module definition
4+
/* globals define, module, require */
5+
if ( typeof define == 'function' && define.amd ) {
6+
// AMD
7+
define( [
8+
'./core',
9+
], function( InfiniteScroll ) {
10+
return factory( window, InfiniteScroll );
11+
});
12+
} else if ( typeof module == 'object' && module.exports ) {
13+
// CommonJS
14+
module.exports = factory(
15+
window,
16+
require('./core')
17+
);
18+
} else {
19+
// browser global
20+
factory(
21+
window,
22+
window.InfiniteScroll
23+
);
24+
}
25+
26+
}( window, function factory( window, InfiniteScroll ) {
27+
128
var proto = InfiniteScroll.prototype;
229

330
// InfiniteScroll.defaults.append = false;
@@ -230,3 +257,9 @@ function request( url, responseType, onLoad, onError ) {
230257

231258
req.send();
232259
}
260+
261+
// -------------------------- -------------------------- //
262+
263+
return InfiniteScroll;
264+
265+
}));

0 commit comments

Comments
 (0)