gulpfile.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. (function(){
  2. 'use strict';
  3. var gulp = require('gulp'),
  4. connect = require('gulp-connect'),
  5. open = require('gulp-open'),
  6. less = require('gulp-less'),
  7. rename = require('gulp-rename'),
  8. header = require('gulp-header'),
  9. path = require('path'),
  10. uglify = require('gulp-uglify'),
  11. sourcemaps = require('gulp-sourcemaps'),
  12. cleanCSS = require('gulp-clean-css'),
  13. tap = require('gulp-tap'),
  14. concat = require('gulp-concat'),
  15. jshint = require('gulp-jshint'),
  16. stylish = require('jshint-stylish'),
  17. fs = require('fs'),
  18. paths = {
  19. root: './',
  20. build: {
  21. root: 'build/',
  22. styles: 'build/css/',
  23. scripts: 'build/js/'
  24. },
  25. custom: {
  26. root: 'custom/',
  27. styles: 'custom/css/',
  28. scripts: 'custom/js/'
  29. },
  30. dist: {
  31. root: 'dist/',
  32. styles: 'dist/css/',
  33. scripts: 'dist/js/'
  34. },
  35. playground: {
  36. root: 'playground/'
  37. },
  38. source: {
  39. root: 'src/',
  40. styles: 'src/less/',
  41. scripts: 'src/js/*.js'
  42. },
  43. },
  44. swiper = {
  45. filename: 'swiper',
  46. jsFiles: [
  47. 'src/js/wrap-start.js',
  48. 'src/js/swiper-intro.js',
  49. 'src/js/core.js',
  50. 'src/js/effects.js',
  51. 'src/js/lazy-load.js',
  52. 'src/js/scrollbar.js',
  53. 'src/js/controller.js',
  54. 'src/js/hashnav.js',
  55. 'src/js/history.js',
  56. 'src/js/keyboard.js',
  57. 'src/js/mousewheel.js',
  58. 'src/js/parallax.js',
  59. 'src/js/zoom.js',
  60. 'src/js/plugins.js',
  61. 'src/js/emitter.js',
  62. 'src/js/a11y.js',
  63. 'src/js/init.js',
  64. 'src/js/swiper-outro.js',
  65. 'src/js/swiper-proto.js',
  66. 'src/js/dom.js',
  67. 'src/js/get-dom-lib.js',
  68. 'src/js/dom-plugins.js',
  69. 'src/js/wrap-end.js',
  70. 'src/js/amd.js'
  71. ],
  72. jQueryFiles : [
  73. 'src/js/wrap-start.js',
  74. 'src/js/swiper-intro.js',
  75. 'src/js/core.js',
  76. 'src/js/effects.js',
  77. 'src/js/lazy-load.js',
  78. 'src/js/scrollbar.js',
  79. 'src/js/controller.js',
  80. 'src/js/hashnav.js',
  81. 'src/js/history.js',
  82. 'src/js/keyboard.js',
  83. 'src/js/mousewheel.js',
  84. 'src/js/parallax.js',
  85. 'src/js/zoom.js',
  86. 'src/js/plugins.js',
  87. 'src/js/emitter.js',
  88. 'src/js/a11y.js',
  89. 'src/js/init.js',
  90. 'src/js/swiper-outro.js',
  91. 'src/js/swiper-proto.js',
  92. 'src/js/get-dom-lib.js',
  93. 'src/js/dom-plugins.js',
  94. 'src/js/wrap-end.js',
  95. 'src/js/amd.js'
  96. ],
  97. jQueryUMDFiles : [
  98. 'src/js/wrap-start-umd.js',
  99. 'src/js/swiper-intro.js',
  100. 'src/js/core.js',
  101. 'src/js/effects.js',
  102. 'src/js/lazy-load.js',
  103. 'src/js/scrollbar.js',
  104. 'src/js/controller.js',
  105. 'src/js/hashnav.js',
  106. 'src/js/history.js',
  107. 'src/js/keyboard.js',
  108. 'src/js/mousewheel.js',
  109. 'src/js/parallax.js',
  110. 'src/js/zoom.js',
  111. 'src/js/plugins.js',
  112. 'src/js/emitter.js',
  113. 'src/js/a11y.js',
  114. 'src/js/init.js',
  115. 'src/js/swiper-outro.js',
  116. 'src/js/swiper-proto.js',
  117. 'src/js/get-jquery.js',
  118. 'src/js/dom-plugins.js',
  119. 'src/js/wrap-end-umd.js',
  120. ],
  121. Framework7Files : [
  122. 'src/js/swiper-intro-f7.js',
  123. 'src/js/core.js',
  124. 'src/js/effects.js',
  125. 'src/js/lazy-load.js',
  126. 'src/js/scrollbar.js',
  127. 'src/js/controller.js',
  128. 'src/js/parallax.js',
  129. 'src/js/zoom.js',
  130. 'src/js/plugins.js',
  131. 'src/js/emitter.js',
  132. 'src/js/a11y.js',
  133. 'src/js/init.js',
  134. 'src/js/swiper-outro.js',
  135. 'src/js/swiper-proto.js',
  136. ],
  137. pkg: require('./bower.json'),
  138. modules: require('./modules.json'),
  139. banner: [
  140. '/**',
  141. ' * Swiper <%= pkg.version %>',
  142. ' * <%= pkg.description %>',
  143. ' * ',
  144. ' * <%= pkg.homepage %>',
  145. ' * ',
  146. ' * Copyright <%= date.year %>, <%= pkg.author %>',
  147. ' * The iDangero.us',
  148. ' * http://www.idangero.us/',
  149. ' * ',
  150. ' * Licensed under <%= pkg.license.join(" & ") %>',
  151. ' * ',
  152. ' * Released on: <%= date.month %> <%= date.day %>, <%= date.year %>',
  153. ' */',
  154. ''].join('\n'),
  155. customBanner: [
  156. '/**',
  157. ' * Swiper <%= pkg.version %> - Custom Build',
  158. ' * <%= pkg.description %>',
  159. ' * ',
  160. ' * Included modules: <%= modulesList %>',
  161. ' * ',
  162. ' * <%= pkg.homepage %>',
  163. ' * ',
  164. ' * Copyright <%= date.year %>, <%= pkg.author %>',
  165. ' * The iDangero.us',
  166. ' * http://www.idangero.us/',
  167. ' * ',
  168. ' * Licensed under <%= pkg.license.join(" & ") %>',
  169. ' * ',
  170. ' * Released on: <%= date.month %> <%= date.day %>, <%= date.year %>',
  171. ' */',
  172. ''].join('\n'),
  173. date: {
  174. year: new Date().getFullYear(),
  175. month: ('January February March April May June July August September October November December').split(' ')[new Date().getMonth()],
  176. day: new Date().getDate()
  177. }
  178. };
  179. function addJSIndent (file, t, minusIndent) {
  180. var addIndent = ' ';
  181. var filename = file.path.split('src/js/')[1];
  182. if (['wrap-start.js', 'wrap-start-umd.js', 'wrap-end.js', 'wrap-end-umd.js', 'amd.js'].indexOf(filename) !== -1) {
  183. addIndent = '';
  184. }
  185. if (filename === 'swiper-intro.js' || filename === 'swiper-intro-f7.js' || filename === 'swiper-outro.js' || filename === 'dom.js' || filename === 'get-dom-lib.js' || filename === 'get-jquery.js' || filename === 'dom-plugins.js' || filename === 'swiper-proto.js') addIndent = ' ';
  186. if (minusIndent) {
  187. addIndent = addIndent.substring(4);
  188. }
  189. if (addIndent !== '') {
  190. var fileLines = fs.readFileSync(file.path).toString().split('\n');
  191. var newFileContents = '';
  192. for (var i = 0; i < fileLines.length; i++) {
  193. newFileContents += addIndent + fileLines[i] + (i === fileLines.length ? '' : '\n');
  194. }
  195. file.contents = new Buffer(newFileContents);
  196. }
  197. }
  198. gulp.task('scripts', function (cb) {
  199. gulp.src(swiper.jsFiles)
  200. .pipe(tap(function (file, t){
  201. addJSIndent (file, t);
  202. }))
  203. .pipe(sourcemaps.init())
  204. .pipe(concat(swiper.filename + '.js'))
  205. .pipe(header(swiper.banner, { pkg : swiper.pkg, date: swiper.date } ))
  206. .pipe(jshint())
  207. .pipe(jshint.reporter(stylish))
  208. .pipe(sourcemaps.write('./maps/'))
  209. .pipe(gulp.dest(paths.build.scripts));
  210. gulp.src(swiper.jQueryFiles)
  211. .pipe(tap(function (file, t){
  212. addJSIndent (file, t);
  213. }))
  214. .pipe(sourcemaps.init())
  215. .pipe(concat(swiper.filename + '.jquery.js'))
  216. .pipe(header(swiper.banner, { pkg : swiper.pkg, date: swiper.date } ))
  217. .pipe(sourcemaps.write('./maps/'))
  218. .pipe(gulp.dest(paths.build.scripts));
  219. gulp.src(swiper.jQueryUMDFiles)
  220. .pipe(tap(function (file, t){
  221. addJSIndent (file, t);
  222. }))
  223. .pipe(sourcemaps.init())
  224. .pipe(concat(swiper.filename + '.jquery.umd.js'))
  225. .pipe(header(swiper.banner, { pkg : swiper.pkg, date: swiper.date } ))
  226. .pipe(sourcemaps.write('./maps/'))
  227. .pipe(gulp.dest(paths.build.scripts));
  228. gulp.src(swiper.Framework7Files)
  229. .pipe(tap(function (file, t){
  230. addJSIndent (file, t, true);
  231. }))
  232. .pipe(sourcemaps.init())
  233. .pipe(concat(swiper.filename + '.framework7.js'))
  234. .pipe(header(swiper.banner, { pkg : swiper.pkg, date: swiper.date } ))
  235. .pipe(sourcemaps.write('./maps/'))
  236. .pipe(gulp.dest(paths.build.scripts))
  237. .pipe(connect.reload());
  238. cb();
  239. });
  240. gulp.task('styles', function (cb) {
  241. gulp.src(paths.source.styles + 'swiper.less')
  242. .pipe(less({
  243. paths: [ path.join(__dirname, 'less', 'includes') ]
  244. }))
  245. .pipe(header(swiper.banner, { pkg : swiper.pkg, date: swiper.date }))
  246. .pipe(rename(function(path) {
  247. path.basename = swiper.filename;
  248. }))
  249. .pipe(gulp.dest(paths.build.styles))
  250. .pipe(connect.reload());
  251. gulp.src([
  252. paths.source.styles + 'core.less',
  253. paths.source.styles + 'navigation-f7.less',
  254. paths.source.styles + 'effects.less',
  255. paths.source.styles + 'zoom.less',
  256. paths.source.styles + 'scrollbar.less',
  257. paths.source.styles + 'preloader-f7.less'
  258. ])
  259. .pipe(concat(swiper.filename + '.framework7.less'))
  260. .pipe(header('/* === Swiper === */\n'))
  261. .pipe(gulp.dest(paths.build.styles));
  262. cb();
  263. });
  264. gulp.task('build', ['scripts', 'styles'], function (cb) {
  265. cb();
  266. });
  267. gulp.task('dist', function () {
  268. gulp.src([paths.build.scripts + swiper.filename + '.js'])
  269. .pipe(gulp.dest(paths.dist.scripts))
  270. .pipe(sourcemaps.init())
  271. .pipe(uglify())
  272. .pipe(header(swiper.banner, { pkg : swiper.pkg, date: swiper.date }))
  273. .pipe(rename(function(path) {
  274. path.basename = swiper.filename + '.min';
  275. }))
  276. .pipe(sourcemaps.write('./maps'))
  277. .pipe(gulp.dest(paths.dist.scripts));
  278. gulp.src([paths.build.scripts + swiper.filename + '.jquery.js'])
  279. .pipe(gulp.dest(paths.dist.scripts))
  280. .pipe(sourcemaps.init())
  281. .pipe(uglify())
  282. .pipe(header(swiper.banner, { pkg : swiper.pkg, date: swiper.date } ))
  283. .pipe(rename(function(path) {
  284. path.basename = swiper.filename + '.jquery.min';
  285. }))
  286. .pipe(sourcemaps.write('./maps'))
  287. .pipe(gulp.dest(paths.dist.scripts));
  288. gulp.src([paths.build.scripts + swiper.filename + '.jquery.umd.js'])
  289. .pipe(gulp.dest(paths.dist.scripts))
  290. .pipe(sourcemaps.init())
  291. .pipe(uglify())
  292. .pipe(header(swiper.banner, { pkg : swiper.pkg, date: swiper.date } ))
  293. .pipe(rename(function(path) {
  294. path.basename = swiper.filename + '.jquery.umd.min';
  295. }))
  296. .pipe(sourcemaps.write('./maps'))
  297. .pipe(gulp.dest(paths.dist.scripts));
  298. gulp.src(paths.build.styles + '*.css')
  299. .pipe(gulp.dest(paths.dist.styles))
  300. .pipe(cleanCSS({
  301. advanced: false,
  302. aggressiveMerging: false,
  303. }))
  304. .pipe(header(swiper.banner, { pkg : swiper.pkg, date: swiper.date }))
  305. .pipe(rename(function(path) {
  306. path.basename = swiper.filename + '.min';
  307. }))
  308. .pipe(gulp.dest(paths.dist.styles));
  309. });
  310. /* =================================
  311. Custom Build
  312. ================================= */
  313. gulp.task('custom', function () {
  314. var modules = process.argv.slice(3);
  315. modules = modules.toString();
  316. if (modules === '') {
  317. modules = [];
  318. }
  319. else {
  320. modules = modules.substring(1).replace(/ /g, '').replace(/,,/g, ',');
  321. modules = modules.split(',');
  322. }
  323. var modulesJs = [], modulesLess = [];
  324. var i, module;
  325. modulesJs.push.apply(modulesJs, swiper.modules.core_intro.js);
  326. modulesLess.push.apply(modulesLess, swiper.modules.core_intro.less);
  327. for (i = 0; i < modules.length; i++) {
  328. module = swiper.modules[modules[i]];
  329. if (!(module)) continue;
  330. if (module.dependencies && module.dependencies.length > 0) {
  331. modules.push.apply(modules, module.dependencies);
  332. }
  333. if (module.js.length > 0) {
  334. modulesJs.push.apply(modulesJs, module.js);
  335. }
  336. if (module.less && module.less.length > 0) {
  337. modulesLess.push.apply(modulesLess, module.less);
  338. }
  339. }
  340. modulesJs.push.apply(modulesJs, swiper.modules.core_outro.js);
  341. modulesLess.push.apply(modulesLess, swiper.modules.core_outro.less);
  342. // Unique
  343. var customJsList = [];
  344. var customLessList = [];
  345. for (i = 0; i < modulesJs.length; i++) {
  346. if (customJsList.indexOf(modulesJs[i]) < 0) customJsList.push(modulesJs[i]);
  347. }
  348. for (i = 0; i < modulesLess.length; i++) {
  349. if (customLessList.indexOf(modulesLess[i]) < 0) customLessList.push(modulesLess[i]);
  350. }
  351. // JS
  352. gulp.src(customJsList)
  353. .pipe(tap(function (file, t){
  354. addJSIndent (file, t);
  355. }))
  356. .pipe(concat(swiper.filename + '.custom.js'))
  357. .pipe(header(swiper.customBanner, { pkg : swiper.pkg, date: swiper.date, modulesList: modules.join(',') } ))
  358. .pipe(jshint())
  359. .pipe(jshint.reporter(stylish))
  360. .pipe(gulp.dest(paths.custom.scripts))
  361. .pipe(uglify())
  362. .pipe(header(swiper.customBanner, { pkg : swiper.pkg, date: swiper.date, modulesList: modules.join(',') }))
  363. .pipe(rename(function(path) {
  364. path.basename = path.basename + '.min';
  365. }))
  366. .pipe(gulp.dest(paths.custom.scripts));
  367. // CSSes
  368. gulp.src(customLessList)
  369. .pipe(concat(swiper.filename + '.custom.less'))
  370. .pipe(less({
  371. paths: [ path.join(__dirname, 'less', 'includes') ]
  372. }))
  373. .pipe(header(swiper.customBanner, { pkg : swiper.pkg, date: swiper.date, modulesList: modules.join(',') } ))
  374. .pipe(gulp.dest(paths.custom.styles))
  375. .pipe(cleanCSS({
  376. advanced: false,
  377. aggressiveMerging: false
  378. }))
  379. .pipe(header(swiper.customBanner, { pkg : swiper.pkg, date: swiper.date, modulesList: modules.join(',') }))
  380. .pipe(rename(function(path) {
  381. path.basename = path.basename + '.min';
  382. }))
  383. .pipe(gulp.dest(paths.custom.styles));
  384. });
  385. gulp.task('watch', function () {
  386. gulp.watch(paths.source.scripts, [ 'scripts' ]);
  387. gulp.watch(paths.source.styles + '*.less', [ 'styles' ]);
  388. });
  389. gulp.task('connect', function () {
  390. return connect.server({
  391. root: [ paths.root ],
  392. livereload: true,
  393. port:'3000'
  394. });
  395. });
  396. gulp.task('open', function () {
  397. return gulp.src(paths.playground.root + 'index.html').pipe(open({ uri: 'http://localhost:3000/' + paths.playground.root + 'index.html'}));
  398. });
  399. gulp.task('server', [ 'watch', 'connect', 'open' ]);
  400. gulp.task('default', [ 'server' ]);
  401. })();