build-bundle.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. loose: true,
  64. targets: { ie:11 },
  65. useBuiltIns: 'entry',
  66. corejs: { version: '3.15', proposals: true },
  67. }]],
  68. })
  69. const { code, map } = await minify(js, {
  70. sourceMap: {
  71. content: inputMap,
  72. url: sourceMapPath,
  73. },
  74. toplevel: true,
  75. })
  76. return Promise.all([
  77. fs.promises.writeFile(bundleFile, js),
  78. fs.promises.writeFile(`${bundleFile}.map`, JSON.stringify(inputMap)),
  79. fs.promises.writeFile(minifiedFilePath, code),
  80. fs.promises.writeFile(sourceMapPath, map),
  81. ]).then(() => {
  82. console.info(chalk.green(`✓ Built Bundle [${standalone} (ES5)]:`), chalk.magenta(bundleFile))
  83. console.info(chalk.green(`✓ Built Minified Bundle [${standalone} (ES5)]:`), chalk.magenta(minifiedFilePath))
  84. })
  85. }
  86. fs.mkdirSync('./packages/uppy/dist', { recursive: true })
  87. fs.mkdirSync('./packages/@uppy/robodog/dist', { recursive: true })
  88. fs.mkdirSync('./packages/@uppy/locales/dist', { recursive: true })
  89. const methods = [
  90. buildBundle(
  91. './packages/uppy/index.js',
  92. './packages/uppy/dist/uppy.js',
  93. { standalone: 'Uppy' },
  94. ).then(minifyBundle),
  95. buildBundle(
  96. './packages/uppy/bundle.js',
  97. './packages/uppy/dist/uppy.legacy.js',
  98. { standalone: 'Uppy (with polyfills)' },
  99. ).then(transpileDownForIE),
  100. buildBundle(
  101. './packages/@uppy/robodog/bundle.js',
  102. './packages/@uppy/robodog/dist/robodog.js',
  103. { standalone: 'Robodog' },
  104. ).then(minifyBundle),
  105. ]
  106. // Build minified versions of all the locales
  107. const localePackagePath = path.join(__dirname, '..', 'packages', '@uppy', 'locales', 'src', '*.js')
  108. glob.sync(localePackagePath).forEach((localePath) => {
  109. const localeName = path.basename(localePath, '.js')
  110. methods.push(
  111. buildBundle(
  112. `./packages/@uppy/locales/src/${localeName}.js`,
  113. `./packages/@uppy/locales/dist/${localeName}.min.js`,
  114. { minify: true },
  115. ),
  116. )
  117. })
  118. // Add BUNDLE-README.MD
  119. methods.push(
  120. fs.promises.copyFile(
  121. `${__dirname}/../BUNDLE-README.md`,
  122. `./packages/uppy/dist/README.md`,
  123. ),
  124. )
  125. Promise.all(methods).then(() => {
  126. console.info(chalk.yellow('✓ JS bundles 🎉'))
  127. })