build-bundle.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env node
  2. import fs from 'node:fs/promises'
  3. import path from 'node:path'
  4. import chalk from 'chalk'
  5. import esbuild from 'esbuild'
  6. const UPPY_ROOT = new URL('../', import.meta.url)
  7. const PACKAGES_ROOT = new URL('./packages/', UPPY_ROOT)
  8. function buildBundle (srcFile, bundleFile, { minify = true, standalone = '', plugins, target, format } = {}) {
  9. return esbuild.build({
  10. bundle: true,
  11. sourcemap: true,
  12. entryPoints: [srcFile],
  13. outfile: bundleFile,
  14. platform: 'browser',
  15. minify,
  16. keepNames: true,
  17. plugins,
  18. target,
  19. format,
  20. }).then(() => {
  21. if (minify) {
  22. console.info(chalk.green(`✓ Built Minified Bundle [${standalone}]:`), chalk.magenta(bundleFile))
  23. } else {
  24. console.info(chalk.green(`✓ Built Bundle [${standalone}]:`), chalk.magenta(bundleFile))
  25. }
  26. })
  27. }
  28. await fs.mkdir(new URL('./uppy/dist', PACKAGES_ROOT), { recursive: true })
  29. await fs.mkdir(new URL('./@uppy/locales/dist', PACKAGES_ROOT), { recursive: true })
  30. const methods = [
  31. buildBundle(
  32. './packages/uppy/index.mjs',
  33. './packages/uppy/dist/uppy.min.mjs',
  34. { standalone: 'Uppy (ESM)', format: 'esm' },
  35. ),
  36. buildBundle(
  37. './packages/uppy/bundle.mjs',
  38. './packages/uppy/dist/uppy.min.js',
  39. { standalone: 'Uppy', format: 'iife' },
  40. ),
  41. ]
  42. // Build minified versions of all the locales
  43. const localesModules = await fs.opendir(new URL('./@uppy/locales/src/', PACKAGES_ROOT))
  44. for await (const dirent of localesModules) {
  45. if (!dirent.isDirectory() && dirent.name.endsWith('.js')) {
  46. const localeName = path.basename(dirent.name, '.js')
  47. methods.push(
  48. buildBundle(
  49. `./packages/@uppy/locales/src/${localeName}.js`,
  50. `./packages/@uppy/locales/dist/${localeName}.min.js`,
  51. { minify: true },
  52. ),
  53. )
  54. }
  55. }
  56. // Add BUNDLE-README.MD
  57. methods.push(
  58. fs.copyFile(
  59. new URL('./BUNDLE-README.md', UPPY_ROOT),
  60. new URL('./uppy/dist/README.md', PACKAGES_ROOT),
  61. ),
  62. )
  63. await Promise.all(methods).then(() => {
  64. console.info(chalk.yellow('✓ JS bundles 🎉'))
  65. }, (err) => {
  66. console.error(chalk.red('✗ Error:'), chalk.red(err.message))
  67. })