build-bundle.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const fs = require('fs')
  2. const chalk = require('chalk')
  3. const babelify = require('babelify')
  4. const tinyify = require('tinyify')
  5. const browserify = require('browserify')
  6. const exorcist = require('exorcist')
  7. const glob = require('glob')
  8. const path = require('path')
  9. const { minify } = require('terser')
  10. const { transformFileAsync } = require('@babel/core')
  11. function handleErr (err) {
  12. console.error(chalk.red('✗ Error:'), chalk.red(err.message))
  13. }
  14. // eslint-disable-next-line no-shadow
  15. function buildBundle (srcFile, bundleFile, { minify = false, standalone = '' } = {}) {
  16. const b = browserify(srcFile, { debug: true, standalone })
  17. if (minify) {
  18. b.plugin(tinyify)
  19. }
  20. b.transform(babelify)
  21. b.on('error', handleErr)
  22. return new Promise((resolve) => {
  23. b.bundle()
  24. .pipe(exorcist(`${bundleFile}.map`))
  25. .pipe(fs.createWriteStream(bundleFile), 'utf8')
  26. .on('error', handleErr)
  27. .on('finish', () => {
  28. if (minify) {
  29. console.info(chalk.green(`✓ Built Minified Bundle [${standalone}]:`), chalk.magenta(bundleFile))
  30. } else {
  31. console.info(chalk.green(`✓ Built Bundle [${standalone}]:`), chalk.magenta(bundleFile))
  32. }
  33. resolve([bundleFile, standalone])
  34. })
  35. })
  36. }
  37. async function minifyBundle ([bundleFile, standalone]) {
  38. const minifiedFilePath = bundleFile.replace(/\.js$/, '.min.js')
  39. const sourceMapPath = `${minifiedFilePath}.map`
  40. const js = await fs.promises.readFile(bundleFile, 'utf-8')
  41. const { code, map } = await minify(js, {
  42. sourceMap: {
  43. content: fs.readFileSync(`${bundleFile}.map`, 'utf-8'),
  44. url:sourceMapPath,
  45. },
  46. toplevel: true,
  47. })
  48. return Promise.all([
  49. fs.promises.writeFile(minifiedFilePath, code),
  50. fs.promises.writeFile(sourceMapPath, map),
  51. ])
  52. .then(() => console.info(chalk.green(`✓ Built Minified Bundle [${standalone}]:`), chalk.magenta(minifiedFilePath)))
  53. }
  54. async function transpileDownForIE ([bundleFile, standalone]) {
  55. const minifiedFilePath = bundleFile.replace(/\.js$/, '.min.js')
  56. const sourceMapPath = `${minifiedFilePath}.map`
  57. const { code: js, map: inputMap } = await transformFileAsync(bundleFile, {
  58. compact: false,
  59. highlightCode: false,
  60. inputSourceMap: true,
  61. browserslistEnv: 'legacy',
  62. presets: [['@babel/preset-env', {
  63. modules: false,
  64. loose: true,
  65. targets: { ie:11 },
  66. }]],
  67. })
  68. const { code, map } = await minify(js, {
  69. sourceMap: {
  70. content: inputMap,
  71. url: sourceMapPath,
  72. },
  73. toplevel: true,
  74. })
  75. return Promise.all([
  76. fs.promises.writeFile(bundleFile, js),
  77. fs.promises.writeFile(`${bundleFile}.map`, JSON.stringify(inputMap)),
  78. fs.promises.writeFile(minifiedFilePath, code),
  79. fs.promises.writeFile(sourceMapPath, map),
  80. ]).then(() => {
  81. console.info(chalk.green(`✓ Built Bundle [${standalone} (ES5)]:`), chalk.magenta(bundleFile))
  82. console.info(chalk.green(`✓ Built Minified Bundle [${standalone} (ES5)]:`), chalk.magenta(minifiedFilePath))
  83. })
  84. }
  85. fs.mkdirSync('./packages/uppy/dist', { recursive: true })
  86. fs.mkdirSync('./packages/@uppy/robodog/dist', { recursive: true })
  87. fs.mkdirSync('./packages/@uppy/locales/dist', { recursive: true })
  88. const methods = [
  89. buildBundle(
  90. './packages/uppy/index.js',
  91. './packages/uppy/dist/uppy.js',
  92. { standalone: 'Uppy' }
  93. ).then(minifyBundle),
  94. buildBundle(
  95. './packages/uppy/bundle.js',
  96. './packages/uppy/dist/uppy.legacy.js',
  97. { standalone: 'Uppy (with polyfills)' }
  98. ).then(transpileDownForIE),
  99. buildBundle(
  100. './packages/@uppy/robodog/bundle.js',
  101. './packages/@uppy/robodog/dist/robodog.js',
  102. { standalone: 'Robodog' }
  103. ).then(minifyBundle),
  104. ]
  105. // Build minified versions of all the locales
  106. const localePackagePath = path.join(__dirname, '..', 'packages', '@uppy', 'locales', 'src', '*.js')
  107. glob.sync(localePackagePath).forEach((localePath) => {
  108. const localeName = path.basename(localePath, '.js')
  109. methods.push(
  110. buildBundle(
  111. `./packages/@uppy/locales/src/${localeName}.js`,
  112. `./packages/@uppy/locales/dist/${localeName}.min.js`,
  113. { minify: true }
  114. )
  115. )
  116. })
  117. // Add BUNDLE-README.MD
  118. methods.push(
  119. fs.promises.copyFile(
  120. `${__dirname}/../BUNDLE-README.md`,
  121. `./packages/uppy/dist/README.md`
  122. )
  123. )
  124. Promise.all(methods).then(() => {
  125. console.info(chalk.yellow('✓ JS bundles 🎉'))
  126. })