build-bundle.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env node
  2. import fs from 'node:fs/promises'
  3. import chalk from 'chalk'
  4. import esbuild from 'esbuild'
  5. const UPPY_ROOT = new URL('../', import.meta.url)
  6. const PACKAGES_ROOT = new URL('./packages/', UPPY_ROOT)
  7. function buildBundle (srcFile, bundleFile, { minify = true, standalone = '', plugins, target, format } = {}) {
  8. return esbuild.build({
  9. bundle: true,
  10. sourcemap: true,
  11. entryPoints: [srcFile],
  12. outfile: bundleFile,
  13. platform: 'browser',
  14. minify,
  15. keepNames: target !== 'es5',
  16. plugins,
  17. tsconfigRaw: '{}',
  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. const methods = [
  30. buildBundle(
  31. './packages/uppy/src/bundle.ts',
  32. './packages/uppy/dist/uppy.min.mjs',
  33. { standalone: 'Uppy (ESM)', format: 'esm' },
  34. ),
  35. buildBundle(
  36. './packages/uppy/bundle.mjs',
  37. './packages/uppy/dist/uppy.min.js',
  38. { standalone: 'Uppy', format: 'iife' },
  39. ),
  40. ]
  41. // Add BUNDLE-README.MD
  42. methods.push(
  43. fs.copyFile(
  44. new URL('./BUNDLE-README.md', UPPY_ROOT),
  45. new URL('./uppy/dist/README.md', PACKAGES_ROOT),
  46. ),
  47. )
  48. await Promise.all(methods).then(() => {
  49. console.info(chalk.yellow('✓ JS bundles 🎉'))
  50. }, (err) => {
  51. console.error(chalk.red('✗ Error:'), chalk.red(err.message))
  52. })