build-js.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var path = require('path')
  2. var fs = require('fs')
  3. var chalk = require('chalk')
  4. var mkdirp = require('mkdirp')
  5. // var glob = require('glob')
  6. var babelify = require('babelify')
  7. // var yoyoify = require('yo-yoify')
  8. var browserify = require('browserify')
  9. // var exec = require('child_process').exec
  10. var exorcist = require('exorcist')
  11. var distPath = './dist'
  12. var srcPath = './src'
  13. function handleErr (err) {
  14. console.error(chalk.red('✗ Error:'), chalk.red(err.message))
  15. }
  16. function buildUppyBundle (minify) {
  17. var src = path.join(srcPath, 'index.js')
  18. var bundleFile = minify ? 'uppy.min.js' : 'uppy.js'
  19. var b = browserify(src, { debug: true, standalone: 'Uppy' })
  20. if (minify) {
  21. b.plugin('minifyify', {
  22. map: bundleFile + '.map',
  23. output: path.join(distPath, bundleFile + '.map')
  24. })
  25. }
  26. // b.transform(yoyoify)
  27. b.transform(babelify)
  28. b.on('error', handleErr)
  29. return new Promise(function (resolve, reject) {
  30. if (minify) {
  31. b.bundle()
  32. .pipe(fs.createWriteStream(path.join(distPath, bundleFile), 'utf8'))
  33. .on('error', handleErr)
  34. .on('finish', function () {
  35. console.info(chalk.green('✓ Built Minified Bundle:'), chalk.magenta(bundleFile))
  36. resolve()
  37. })
  38. } else {
  39. b.bundle()
  40. .pipe(exorcist(path.join(distPath, 'uppy.js.map')))
  41. .pipe(fs.createWriteStream(path.join(distPath, bundleFile), 'utf8'))
  42. .on('error', handleErr)
  43. .on('finish', function () {
  44. console.info(chalk.green('✓ Built Bundle:'), chalk.magenta(bundleFile))
  45. resolve()
  46. })
  47. }
  48. })
  49. }
  50. // function copyLocales () {
  51. // var copyCommand = 'cp -R ' + path.join(srcPath, 'locales/') + ' ' + path.join(distPath, 'locales/')
  52. // return new Promise(function (resolve, reject) {
  53. // exec(copyCommand, function (error, stdout, stderr) {
  54. // if (error) {
  55. // handleErr(error)
  56. // reject(error)
  57. // return
  58. // }
  59. // console.info(chalk.green('✓ Copied locales to dist'))
  60. // resolve()
  61. // })
  62. // })
  63. // }
  64. // function buildLocale (file) {
  65. // return new Promise(function (resolve, reject) {
  66. // var fileName = path.basename(file, '.js')
  67. // browserify(file)
  68. // .transform(babelify)
  69. // .on('error', handleErr)
  70. // .bundle()
  71. // .pipe(fs.createWriteStream('./dist/locales/' + fileName + '.js', 'utf8'))
  72. // .on('error', handleErr)
  73. // .on('finish', function () {
  74. // console.info(chalk.green('✓ Built Locale:'), chalk.magenta(fileName + '.js'))
  75. // resolve()
  76. // })
  77. // })
  78. // }
  79. // function buildUppyLocales () {
  80. // mkdirp.sync('./dist/locales')
  81. // var localePromises = []
  82. // glob('./src/locales/*.js', function (err, files) {
  83. // if (err) console.log(err)
  84. // files.forEach(function (file) {
  85. // localePromises.push(buildLocale(file))
  86. // })
  87. // })
  88. // return Promise.all(localePromises)
  89. // }
  90. mkdirp.sync(distPath)
  91. Promise.all([buildUppyBundle(), buildUppyBundle(true)])
  92. .then(function () {
  93. console.info(chalk.yellow('✓ JS Bundle 🎉'))
  94. })
  95. // Promise.all([buildUppyBundle(), buildUppyBundle(true), buildUppyLocales()])
  96. // .then(function () {
  97. // console.info(chalk.yellow('✓ JS Bundle 🎉'))
  98. // })