Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. const minimatch = require( "minimatch" );
  2. const _ = require( "lodash" );
  3. const process = require( "process" );
  4. const auto = require( "auto-builder" );
  5. const fsp = require( "fs-promise" );
  6. const path = require( "path" );
  7. const babel = require( "babel-core" );
  8. const globby = require( "globby" );
  9. const winston = require( "winston" );
  10. const browserify = require( "browserify" );
  11. const pug = require( "pug" );
  12.  
  13. async function bundlePug( src, tgt ) {
  14. winston.info( `compiling ${src} to ${tgt} @ html` );
  15. var code = pug.render( await fsp.readFile( src ), { filename : src, basedir : process.cwd() } );
  16. await fsp.writeFile( tgt, code );
  17. }
  18.  
  19. async function compileJSXToES8( src, tgt ) {
  20. winston.info( `compiling ${src} @ JSX to ${tgt} @ ES8` );
  21. var code = babel.transform( await fsp.readFile( src ), {
  22. plugins : [ "transform-react-jsx" ]
  23. } ).code;
  24. await fsp.writeFile( tgt, code );
  25. }
  26.  
  27. async function compileES8ToES7( src, tgt ) {
  28. winston.info( `compiling ${src} @ ES8 to ${tgt} @ ES7` );
  29. var code = babel.transform( await fsp.readFile( src ), {
  30. presets : [ require.resolve( "babel-preset-es2017" ) ]
  31. } ).code;
  32. await fsp.writeFile( tgt, code );
  33. }
  34.  
  35. async function compileES7ToES5( src, tgt ) {
  36. winston.info( `compiling ${src} @ ES7 to ${tgt} @ ES5` );
  37. var code = babel.transform( await fsp.readFile( src ), {
  38. presets : [ require.resolve( "babel-preset-es2016" ) ]
  39. } ).code;
  40. code = babel.transform( code, {
  41. presets : [ require.resolve( "babel-preset-es2015" ) ],
  42. plugins : [ [ require.resolve( "babel-plugin-transform-runtime" ), { regenerator : true } ] ]
  43. } ).code;
  44. await fsp.writeFile( tgt, code );
  45. }
  46.  
  47. async function bundleJS( src, tgt ) {
  48. winston.info( `bundling (using browserify) ${src} to ${tgt}` );
  49. var code = await new Promise( function( rs, rj ) {
  50. var b = browserify();
  51. b.add( path.resolve( src ) );
  52. b.bundle( (e,r) => e ? rj( e ) : rs( r.toString( "utf8" ) ) );
  53. });
  54. await fsp.writeFile( tgt, code );
  55. }
  56.  
  57. async function copy( src, tgt ) {
  58. winston.info( `copying ${src} to ${tgt}` );
  59. await fsp.copy( src, tgt );
  60. }
  61.  
  62. function rename( p, fn ) {
  63. var ext = path.extname( p );
  64. var base = path.basename( p, ext );
  65. var dir = path.dirname( p );
  66. return fn( dir, base, ext );
  67. }
  68.  
  69. async function build() {
  70.  
  71. var make = {};
  72. var list = await globby( [ "*.*", "www/**/*.*", "lib/**/*.*" ] );
  73. var grpDirs = _.groupBy( list, x => path.dirname( x ) );
  74.  
  75. for( let f of list ) {
  76.  
  77. let es8 = rename( f, (d,b,_e) => path.join( "dist", d, b + ".js.es8" ) );
  78. // if the file is already a vanilla js file, just copy the file to its es8 destination
  79. if( path.extname( f ) === ".js" )
  80. make[ es8 ] = {
  81. dependencies : [ f ],
  82. recipe : () => copy( f, es8 )
  83. };
  84. // otherwise apply jsx pre-processor to convert to es8
  85. else if( path.extname( f ) === ".jsx" )
  86. make[ es8 ] = {
  87. dependencies : [ f ],
  88. recipe : () => compileJSXToES8( f, es8 )
  89. };
  90.  
  91. // are we dealing with a js-type file
  92. if( path.extname( f ) === ".jsx" || path.extname( f ) === ".js" ) {
  93. // we will necessarily need to compile down to es7
  94. let es7 = rename( f, (d,b,_e) => path.join( "dist", d, b + ".js.es7" ) );
  95. make[ es7 ] = {
  96. dependencies : [ es8 ],
  97. recipe : () => compileES8ToES7( es8, es7 )
  98. };
  99. // if its a front-end file compile further to es5
  100. let tgt = rename( f, (d,b,_e) => path.join( "dist", d, b + ".js" ) );
  101. if( minimatch( f, "www/**/*.*" ) ) {
  102. let es5 = rename( f, (d,b,_e) => path.join( "dist", d, b + ".js.es5" ) );
  103. make[ es5 ] = {
  104. dependencies : [ es7 ],
  105. recipe : () => compileES7ToES5( es7, es5 )
  106. };
  107. // if its *not* an index file, then copy it to .js and be done
  108. if( path.basename( f ) !== "index.js" ) {
  109. make[ tgt ] = {
  110. dependencies : [ es5 ],
  111. recipe : () => copy( es5, tgt )
  112. };
  113. }
  114. // else it needs to be bundled
  115. else {
  116. // gather all dependencies (all other directory-local js files and any js files in the global lib directory)
  117. let allDeps = _.chain( grpDirs[ path.dirname( f ) ].concat( grpDirs[ "www/js" ] || [] ) )
  118. .filter( x => ( path.extname( x ) === ".js" || path.extname( x ) === ".jsx" ) && x !== f )
  119. .map( x => rename( x, (d,b,_e) => path.join( "dist", d, b + ".js" ) ) )
  120. .value();
  121. make[ tgt ] = {
  122. dependencies : [ es5 ].concat( allDeps ),
  123. recipe : () => bundleJS( es5, tgt )
  124. };
  125. }
  126. // otherwise just copy the back-end file over
  127. } else {
  128. make[ tgt ] = {
  129. dependencies : [ es7 ],
  130. recipe : () => copy( es7, tgt )
  131. };
  132. }
  133. // otherwise we're not dealing with a js file
  134. } else {
  135.  
  136. if( path.extname( f ) === ".pug" ) {
  137. let tgt = rename( f, (d,b,_e) => path.join( "dist", d, b + ".html" ) );
  138. let allDeps = _.chain( grpDirs[ path.dirname( f ) ].concat( grpDirs[ "www/include" ] || [] ).concat( grpDirs[ "www/css" ] || [] ) )
  139. .filter( x => path.extname( x ) === ".pug" || path.extname( x ) === ".css" )
  140. .value();
  141. make[ tgt ] = {
  142. dependencies : allDeps,
  143. recipe : () => bundlePug( f, tgt )
  144. };
  145. } else {
  146. let tgt = path.join( "dist", f );
  147. make[ tgt ] = {
  148. dependencies : [ f ],
  149. recipe : () => copy( f, tgt )
  150. };
  151. }
  152.  
  153. }
  154.  
  155. }
  156.  
  157. make[ "dist/build" ] = { dependencies : Object.keys(make) };
  158. var runner = auto( make );
  159. await runner( "dist/build" );
  160.  
  161. }
  162.  
  163. build()
  164. .then( () => winston.info( "BUILD SUCCEEDED" ) )
  165. .catch( (e) => winston.error( "BUILD FAILED", e ) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement