build-js.js 2.8 KB

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