|
| 1 | + |
| 2 | +/* |
| 3 | + * This file is the build process for this repository. |
| 4 | + * |
| 5 | + * It begins by importing all the gulp modules needed to do the tasks below. |
| 6 | + */ |
| 7 | +var gulp = require( 'gulp' ); |
| 8 | +var htmlmin = require( 'gulp-htmlmin' ); |
| 9 | +var embed = require( 'gulp-embed' ); |
| 10 | +var uglify = require( 'gulp-uglify' ); |
| 11 | +var each = require( 'gulp-each' ); |
| 12 | +var rename = require( 'gulp-rename' ); |
| 13 | +var concat = require( 'gulp-concat' ); |
| 14 | +var addsrc = require( 'gulp-add-src' ); |
| 15 | + |
| 16 | +/* |
| 17 | + * Any client will need to import our scripts. The main one is the |
| 18 | + * cloud-storage.js file itself, which we compile here, from the source |
| 19 | + * folder to the release folder. |
| 20 | + * |
| 21 | + * This is a simple minification of the source file, with one additional |
| 22 | + * step. We embed the entire dialog.{html,css,js} content as a single, |
| 23 | + * base64-encoded string at the top of the compiled file, so that it can be |
| 24 | + * used programmatically to fill an iframe, without the need for those three |
| 25 | + * files to be present in the same folder. |
| 26 | + */ |
| 27 | +gulp.task( 'build-main-script', function () { |
| 28 | + return gulp.src( 'source/dialog.html' ) |
| 29 | + // embed dialog.{js,css} into dialog.html, then minify |
| 30 | + .pipe( embed() ) |
| 31 | + .pipe( htmlmin( { collapseWhitespace : true, |
| 32 | + removeComments : true, |
| 33 | + minifyJS : true, |
| 34 | + minifyCSS : true } ) ) |
| 35 | + // manually base64 encode the result as a JS string variable |
| 36 | + .pipe( each( function ( content, file, callback ) { |
| 37 | + callback( null, |
| 38 | + 'var dialogHTML = "data:text/html;base64,' + |
| 39 | + ( new Buffer( content ).toString( 'base64' ) ) + |
| 40 | + '";' ); |
| 41 | + } ) ) |
| 42 | + // prepend this onto cloud-storage.js and make it use the var |
| 43 | + .pipe( addsrc.append( 'source/cloud-storage.js' ) ) |
| 44 | + .pipe( concat( 'cloud-storage.js' ) ) |
| 45 | + .pipe( each( function ( content, file, callback ) { |
| 46 | + callback( null, |
| 47 | + content.replace( "'./dialog.html'", "dialogHTML" ) ); |
| 48 | + } ) ) |
| 49 | + // minify and output |
| 50 | + .pipe( uglify() ) |
| 51 | + .pipe( gulp.dest( 'release/' ) ); |
| 52 | +} ); |
| 53 | + |
| 54 | +/* |
| 55 | + * Any client will need to import our scripts. In addition to the |
| 56 | + * cloud-storage.js file built by the process above, there are also a |
| 57 | + * handful of "backends" that reside in their own .js files, so that clients |
| 58 | + * can import exactly the subset they need, and no more. We compile those |
| 59 | + * here, also from the source folder to the release folder. |
| 60 | + */ |
| 61 | +gulp.task( 'minify-backends', function () { |
| 62 | + return gulp.src( 'source/*-backend.js' ) |
| 63 | + .pipe( uglify() ) |
| 64 | + .pipe( gulp.dest( 'release/' ) ); |
| 65 | +} ); |
| 66 | + |
| 67 | +/* |
| 68 | + * A client who chooses to use the Dropbox backend will need a page in their |
| 69 | + * own web space to which to direct the Dropbox login procedure. We provide |
| 70 | + * the file dropbox-login.html that receives that redirection from Dropbox |
| 71 | + * and emits the messages that the scripts in dropbox-backend.js need to |
| 72 | + * access the user's Dropbox. The user will need a copy of thta file in |
| 73 | + * the same folder as the page importing cloud-storage.js. |
| 74 | + * |
| 75 | + * This build task just copies the dropbox-login.html page to the release |
| 76 | + * folder, from which clients who need it can download it. But while we're |
| 77 | + * at it, we might as well minify its contents. |
| 78 | + */ |
| 79 | +gulp.task( 'minify-login-page', function () { |
| 80 | + return gulp.src( 'source/dropbox-login.html' ) |
| 81 | + .pipe( htmlmin( { collapseWhitespace : true, |
| 82 | + removeComments : true, |
| 83 | + minifyJS : true } ) ) |
| 84 | + .pipe( gulp.dest( 'release/' ) ); |
| 85 | +} ); |
| 86 | + |
| 87 | +/* |
| 88 | + * This repository contains an example/ folder to show how to use the tools |
| 89 | + * provided. This task just copies the minified login page from the release |
| 90 | + * folder to the example folder. |
| 91 | + */ |
| 92 | +gulp.task( 'build-example', [ 'minify-login-page' ], function () { |
| 93 | + return gulp.src( 'release/dropbox-login.html' ) |
| 94 | + .pipe( gulp.dest( 'example/' ) ); |
| 95 | +} ); |
| 96 | + |
| 97 | +/* |
| 98 | + * The default build task is to do everything. |
| 99 | + */ |
| 100 | +gulp.task( 'default', [ 'build-main-script', |
| 101 | + 'minify-backends', |
| 102 | + 'minify-login-page', |
| 103 | + 'build-example' ] ); |
0 commit comments