@@ -4,12 +4,14 @@ var gulp = require('gulp');
44var exec = require ( 'child_process' ) . exec ;
55var jsdoc = require ( 'gulp-jsdoc3' ) ;
66var pandoc = require ( 'gulp-pandoc' ) ;
7+ var fs = require ( 'fs' ) ;
78
89// Task and dependencies to distribute for all environments;
910var babel = require ( 'babelify' ) ;
1011var browserify = require ( 'browserify' ) ;
1112var buffer = require ( 'vinyl-buffer' ) ;
1213var source = require ( 'vinyl-source-stream' ) ;
14+ var sourcemaps = require ( 'gulp-sourcemaps' ) ;
1315var replace = require ( 'gulp-replace' ) ;
1416var insert = require ( 'gulp-insert' ) ;
1517var uglify = require ( 'gulp-uglify' ) ;
@@ -18,6 +20,8 @@ var argv = require('yargs').argv;
1820var through = require ( 'through2' ) ;
1921var path = require ( 'path' ) ;
2022var gulpif = require ( 'gulp-if' ) ;
23+ var gutil = require ( 'gulp-util' ) ;
24+ var extensions = [ '.js' , '.json' ] ;
2125
2226var git = require ( 'gulp-git' ) ;
2327var prompt = require ( 'gulp-prompt' ) ;
@@ -127,107 +131,120 @@ function prependLicense(clean) {
127131
128132}
129133
130- gulp . task ( 'runtime' , function ( ) {
131-
132- var compact = true ;
133-
134- return browserify ( {
135- entries : [ './src/runtime/RuntimeUA.js' ] ,
136- standalone : 'Runtime' ,
137- debug : false
138- } )
139- . transform ( babel , {
140- compact : compact ,
141- presets : [ 'es2015' , 'stage-0' ] ,
142- plugins : [ 'add-module-exports' , 'transform-runtime' , 'transform-regenerator' ]
143- } )
144- . bundle ( )
145- . on ( 'error' , function ( err ) {
146- console . error ( err ) ;
147- this . emit ( 'end' ) ;
148- } )
149- . pipe ( source ( 'Runtime.js' ) )
150- . pipe ( buffer ( ) )
151- . pipe ( gulpif ( compact , uglify ( ) ) )
152- . pipe ( gulpif ( compact , insert . prepend ( license + '// Runtime User Agent \n\n// version: {{version}}\n\n' ) ) )
153- . pipe ( gulpif ( compact , replace ( '{{version}}' , pkg . version ) ) )
154- . pipe ( gulp . dest ( './dist' ) ) ;
134+ /**
135+ * Make 3 distriution files
136+ * By default the compact mode is true;
137+ * How to use: gulp dist --compact false | true;
138+ */
139+ gulp . task ( 'dist' , function ( ) {
140+
141+ return gulp . src ( [ 'src/sandbox.js' , 'src/minibus.js' , 'src/runtime/RuntimeUA.js' ] )
142+ . pipe ( dist ( ) ) ;
155143
156144} ) ;
157145
158- gulp . task ( 'minibus' , function ( ) {
159-
160- var compact = true ;
161-
162- var descriptionNote = '/**\n' +
163- '* Minimal interface and implementation to send and receive messages. It can be reused in many type of components.\n' +
164- '* Components that need a message system should receive this class as a dependency or extend it.\n' +
165- '* Extensions should implement the following private methods: _onPostMessage and _registerExternalListener.\n' +
166- '*/\n' ;
167-
168- return browserify ( {
169- entries : [ './src/minibus.js' ] ,
170- standalone : 'MiniBus' ,
171- debug : false
172- } )
173- . transform ( babel , {
174- compact : compact ,
175- presets : [ 'es2015' , 'stage-0' ] ,
176- plugins : [ 'add-module-exports' , 'transform-runtime' , 'transform-regenerator' ]
177- } )
178- . bundle ( )
179- . on ( 'error' , function ( err ) {
180- console . error ( err ) ;
181- this . emit ( 'end' ) ;
182- } )
183- . pipe ( source ( 'minibus.js' ) )
184- . pipe ( buffer ( ) )
185- . pipe ( gulpif ( compact , uglify ( ) ) )
186- . pipe ( gulpif ( compact , insert . prepend ( descriptionNote + '// version: {{version}}\n\n' ) ) )
187- . pipe ( gulpif ( compact , replace ( '{{version}}' , pkg . version ) ) )
188- . pipe ( gulp . dest ( './dist' ) ) ;
146+ function dist ( debug ) {
189147
190- } ) ;
148+ if ( ! debug ) debug = false ;
191149
192- gulp . task ( 'sandbox' , function ( ) {
193-
194- var descriptionNote = '/**\n' +
195- '* @author micaelpedrosa@gmail.com\n' +
196- '* Base class to implement external sandbox component\n' +
197- '*/\n\n' ;
198-
199- var compact = true ;
200-
201- return browserify ( {
202- entries : [ './src/sandbox.js' ] ,
203- standalone : 'sandbox' ,
204- debug : false
205- } )
206- . transform ( babel , {
207- compact : compact ,
208- presets : [ 'es2015' , 'stage-0' ] ,
209- plugins : [ 'add-module-exports' , 'transform-runtime' , 'transform-regenerator' ]
210- } )
211- . bundle ( )
212- . on ( 'error' , function ( err ) {
213- console . error ( err ) ;
214- this . emit ( 'end' ) ;
215- } )
216- . pipe ( source ( 'sandbox.js' ) )
217- . pipe ( buffer ( ) )
218- . pipe ( gulpif ( compact , uglify ( ) ) )
219- . pipe ( gulpif ( compact , insert . prepend ( descriptionNote + '// version: {{version}}\n\n' ) ) )
220- . pipe ( gulpif ( compact , replace ( '{{version}}' , pkg . version ) ) )
221- . pipe ( gulp . dest ( './dist' ) ) ;
150+ return through . obj ( function ( file , enc , cb ) {
222151
223- } ) ;
152+ if ( file . isNull ( ) ) {
153+ return cb ( new Error ( 'Fil is null' ) ) ;
154+ }
224155
225- /**
226- * Make 3 distriution files
227- * By default the compact mode is true;
228- * How to use: gulp dist --compact false | true;
229- */
230- gulp . task ( 'dist' , [ 'runtime' , 'minibus' , 'sandbox' ] ) ;
156+ if ( file . isStream ( ) ) {
157+ return cb ( new Error ( 'Streaming not supported' ) ) ;
158+ }
159+
160+ var filename = path . basename ( file . path , '.js' ) ;
161+
162+ var opts = {
163+ configuration : { } ,
164+ debug : false ,
165+ standalone : filename === 'RuntimeUA' ? 'Runtime' : filename ,
166+ destination : __dirname + '/dist'
167+ } ;
168+
169+ gutil . log ( gutil . colors . yellow ( 'Make a distribution file from' , filename + '.js' ) ) ;
170+
171+ gulp . src ( [ file . path ] )
172+ . pipe ( transpile ( opts ) )
173+ . pipe ( mark ( ) )
174+ . pipe ( gulp . dest ( __dirname + '/dist' ) )
175+ . on ( 'error' , function ( error ) {
176+ gutil . log ( gutil . colors . red ( error ) ) ;
177+ } )
178+ . on ( 'end' , function ( ) {
179+ gutil . log ( '> ' + gutil . colors . green ( 'Distribution ' ) + gutil . colors . white ( filename ) + gutil . colors . green ( ' done!' ) ) ;
180+ cb ( ) ;
181+ } ) ;
182+ } ) ;
183+
184+ }
185+
186+ function mark ( ) {
187+
188+ return through . obj ( function ( file , enc , cb ) {
189+
190+ var fileObject = path . parse ( file . path ) ;
191+
192+ gulp . src ( [ file . path ] )
193+ . pipe ( insert . prepend ( license + '// Distribution file for {{package}} \n// version: {{version}}\n// Last build: {{date}}\n\n' ) )
194+ . pipe ( replace ( '{{version}}' , pkg . version ) )
195+ . pipe ( replace ( '{{package}}' , fileObject . name + '.js' ) )
196+ . pipe ( replace ( '{{date}}' , new Date ( ) ) )
197+ . pipe ( gulp . dest ( __dirname + '/dist' ) )
198+ . on ( 'end' , function ( ) {
199+ cb ( ) ;
200+ } ) ;
201+
202+ } ) ;
203+
204+ }
205+
206+ function transpile ( opts ) {
207+
208+ return through . obj ( function ( file , enc , cb ) {
209+
210+ var fileObject = path . parse ( file . path ) ;
211+ var filename = fileObject . base === 'RuntimeUA.js' ? 'Runtime.js' : fileObject . base ;
212+ var args = { } ;
213+
214+ var environment = argv . production || process . env . NODE_ENV ;
215+ process . env . environment = environment ? 'production' : 'development' ;
216+
217+ args . entries = [ file . path ] ;
218+ args . extensions = extensions ;
219+ if ( opts . debug ) args . debug = opts . debug ;
220+ if ( opts . standalone ) args . standalone = opts . standalone ;
221+
222+ return browserify ( args )
223+ . transform ( babel , {
224+ compact : true ,
225+ presets : [ 'es2015' , 'stage-0' ] ,
226+ plugins : [ 'add-module-exports' , 'babel-polyfill' , 'transform-runtime' , 'transform-regenerator' ]
227+ } )
228+ . bundle ( )
229+ . on ( 'error' , function ( err ) {
230+ gutil . log ( gutil . colors . red ( err ) ) ;
231+ this . emit ( 'end' ) ;
232+ } )
233+ . pipe ( source ( filename ) )
234+ . pipe ( buffer ( ) )
235+ . pipe ( sourcemaps . init ( { loadMaps : true } ) )
236+ . pipe ( uglify ( ) )
237+ . pipe ( sourcemaps . write ( './' ) )
238+ . pipe ( gulp . dest ( opts . destination ) )
239+ . on ( 'end' , function ( ) {
240+ file . contents = fs . readFileSync ( opts . destination + '/' + filename ) ;
241+ file . path = opts . destination + '/' + filename ;
242+ cb ( null , file ) ;
243+ } ) ;
244+
245+ } ) ;
246+
247+ }
231248
232249/**
233250 * Bumping version number and tagging the repository with it.
0 commit comments