11#!/usr/bin/env node
2- /*eslint-disable no-console*/
2+ /*eslint-disable no-console, camelcase */
33const browserify = require ( 'browserify' )
4- const closure = require ( 'google-closure-compiler-js ' ) . compile
4+ const ClosureCompiler = require ( 'google-closure-compiler' ) . compiler
55const fs = require ( 'fs' )
6- const ReplaceStream = require ( './lib/replace-stream ' )
6+ const path = require ( 'path ' )
77const Simultaneity = require ( 'simultaneity' )
88
99const uploadB = browserify ( )
10- uploadB . add ( __dirname + '/ client-side/upload.js')
10+ uploadB . add ( path . join ( __dirname , ' client-side/upload.js') )
1111const downloadB = browserify ( )
12- downloadB . add ( __dirname + '/ client-side/download.js')
12+ downloadB . add ( path . join ( __dirname , ' client-side/download.js') )
1313const uploadDownloadB = browserify ( )
14- uploadDownloadB . add ( __dirname + '/ client-side/upload-download.js')
14+ uploadDownloadB . add ( path . join ( __dirname , ' client-side/upload-download.js') )
1515
16- const s = new Simultaneity
17- //Replace require('util'), which is only used for util.inspect(), to minimize file size
18- for ( const utilFile of [ '/lib/assert' , '/structure-types' , '/read' ] ) {
19- s . addTask ( s => {
20- fs . createReadStream ( __dirname + utilFile + '.js' )
21- . pipe ( new ReplaceStream ( "require('util')" , "require('/lib/util-inspect.js')" ) )
22- . pipe ( fs . createWriteStream ( __dirname + utilFile + '-noutil.js' ) ) . on ( 'finish' , ( ) => {
23- s . taskFinished ( )
24- } )
25- } )
26- }
27- s . addTask ( s => {
28- //Load the upload and download code and append them to each other to make a combined include file
29- //These files are not too big, so it is not terrible to load them into memory
30- let uploadCode , downloadCode
31- new Simultaneity ( )
32- . addTask ( s => {
33- fs . readFile ( __dirname + '/client-side/upload.js' , ( err , data ) => {
34- if ( err ) throw err
35- uploadCode = data
36- s . taskFinished ( )
37- } )
38- } )
39- . addTask ( s => {
40- fs . readFile ( __dirname + '/client-side/download.js' , ( err , data ) => {
41- if ( err ) throw err
42- downloadCode = data
43- s . taskFinished ( )
44- } )
45- } )
46- . callback ( ( ) => {
47- fs . writeFile (
48- __dirname + '/client-side/upload-download.js' ,
49- Buffer . concat ( [ uploadCode , Buffer . from ( ';' ) , downloadCode ] ) ,
50- err => {
51- if ( err ) throw err
52- s . taskFinished ( )
53- }
54- )
55- } )
56- } )
57- console . log ( 'Compiling: Replacing large dependencies' )
58- const uploadFiles = [
59- '/client-side/binary-ajax.js' ,
60- '/client-side/common.js' ,
61- '/config.js' ,
62- '/lib/bit-math.js' ,
63- '/lib/buffer-string.js' ,
64- '/lib/flex-int.js' ,
65- '/lib/growable-buffer.js' ,
66- '/lib/strint.js' ,
67- '/lib/util-inspect.js' ,
68- '/recursive-registry.js'
16+ const UPLOAD_FILES = [
17+ './client-side/binary-ajax.js' ,
18+ './client-side/common.js' ,
19+ './config.js' ,
20+ './lib/assert.js' ,
21+ './lib/bit-math.js' ,
22+ './lib/buffer-string.js' ,
23+ './lib/flex-int.js' ,
24+ './lib/growable-buffer.js' ,
25+ './lib/strint.js' ,
26+ './recursive-registry.js' ,
27+ './structure-types.js'
6928]
70- const downloadFiles = uploadFiles . concat ( [ '/constructor-registry.js' ] )
71- s . callback ( ( ) => {
72- //Include the file in the browserify result because it is require()d by other files
73- function exposeFile ( b , name , fileName = name ) {
74- b . require ( __dirname + fileName , { expose : name } )
29+ const DOWNLOAD_FILES = UPLOAD_FILES . concat ( [
30+ './constructor-registry.js' ,
31+ './read.js'
32+ ] )
33+ function initiateCompile ( ) {
34+ //Include a file in the browserify result because it is require()d by other files
35+ function exposeFile ( b , fileName ) {
36+ b . require ( fileName , { expose : fileName } )
7537 }
76- function compile ( b , { modifiedFiles , exposeFiles, outputFile} ) {
38+ function compile ( b , { exposeFiles, outputFile} ) {
7739 console . log ( 'Compiling: Browserifying ' + outputFile )
78- //Expose the files with require('util') removed in place of the true file
79- for ( const ending in modifiedFiles ) { //eslint-disable-line guard-for-in
80- for ( const file of modifiedFiles [ ending ] ) exposeFile ( b , file + '.js' , file + '-' + ending + '.js' )
81- }
82- //Expose all the unmodified files as normal
40+ const absoluteOutputFile = path . join ( __dirname , outputFile )
8341 for ( const file of exposeFiles ) exposeFile ( b , file )
84- const chunks = [ ]
85- b . bundle ( ) . on ( 'data' , chunk => chunks . push ( chunk ) ) . on ( 'end' , ( ) => { //load output into memory
86- console . log ( 'Compiling: Minifying ' + outputFile )
87- const minified = closure ( {
88- assumeFunctionWrapper : true ,
89- jsCode : [ { src : Buffer . concat ( chunks ) . toString ( ) } ] ,
90- rewritePolyfills : true
91- } ) . compiledCode
92- fs . writeFile ( __dirname + outputFile , '!function(){' + minified + '}()' , err => { //write out the minified code
93- if ( err ) throw err
42+ const bundleOutputFile = absoluteOutputFile . replace ( / .j s $ / , '-unminified.js' )
43+ b . bundle ( ) . pipe ( fs . createWriteStream ( bundleOutputFile ) )
44+ . on ( 'finish' , ( ) => {
45+ console . log ( 'Compiling: Minifying ' + outputFile )
46+ new ClosureCompiler ( {
47+ js : [ bundleOutputFile ] ,
48+ js_output_file : absoluteOutputFile ,
49+ output_wrapper : '!function(){%output%}()' ,
50+ language_out : 'ES5' ,
51+ jscomp_off : '*' //browserify generates some ugly code that can be ignored
52+ } ) . run ( ( exitCode , stdOut , stdErr ) => {
53+ stdOut = stdOut . trim ( )
54+ if ( stdOut ) console . log ( stdOut )
55+ stdErr = stdErr . trim ( )
56+ if ( stdErr ) console . error ( stdErr )
57+ } )
9458 } )
95- } )
9659 }
9760 compile ( uploadB , {
98- modifiedFiles : {
99- noutil : [ '/lib/assert' , '/structure-types' ]
100- } ,
101- exposeFiles : uploadFiles ,
102- outputFile : '/compiled/upload.js'
61+ exposeFiles : UPLOAD_FILES ,
62+ outputFile : 'compiled/upload.js'
10363 } )
10464 compile ( downloadB , {
105- modifiedFiles : {
106- noutil : [ '/lib/assert' , '/structure-types' , '/read' ]
107- } ,
108- exposeFiles : downloadFiles ,
109- outputFile : '/compiled/download.js'
65+ exposeFiles : DOWNLOAD_FILES ,
66+ outputFile : 'compiled/download.js'
11067 } )
11168 compile ( uploadDownloadB , {
112- modifiedFiles : {
113- noutil : [ '/lib/assert' , '/structure-types' , '/read' ]
114- } ,
115- exposeFiles : downloadFiles ,
116- outputFile : '/compiled/upload-download.js'
69+ exposeFiles : DOWNLOAD_FILES ,
70+ outputFile : 'compiled/upload-download.js'
11771 } )
118- } )
72+ }
73+
74+ //Load the upload and download code and append them to each other to make a combined include file
75+ //These files are not too big, so it is not terrible to load them into memory
76+ let uploadCode , downloadCode
77+ new Simultaneity ( )
78+ . addTask ( s => {
79+ fs . readFile ( path . join ( __dirname , 'client-side/upload.js' ) , ( err , data ) => {
80+ if ( err ) throw err
81+ uploadCode = data
82+ s . taskFinished ( )
83+ } )
84+ } )
85+ . addTask ( s => {
86+ fs . readFile ( path . join ( __dirname , 'client-side/download.js' ) , ( err , data ) => {
87+ if ( err ) throw err
88+ downloadCode = data
89+ s . taskFinished ( )
90+ } )
91+ } )
92+ . callback ( ( ) => {
93+ fs . writeFile (
94+ path . join ( __dirname , 'client-side/upload-download.js' ) ,
95+ Buffer . concat ( [ uploadCode , Buffer . from ( ';' ) , downloadCode ] ) ,
96+ err => {
97+ if ( err ) throw err
98+ initiateCompile ( )
99+ }
100+ )
101+ } )
0 commit comments