build-bundle.mjs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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: target !== 'es5',
  17. plugins,
  18. tsconfigRaw: '{}',
  19. target,
  20. format,
  21. }).then(() => {
  22. if (minify) {
  23. console.info(chalk.green(`✓ Built Minified Bundle [${standalone}]:`), chalk.magenta(bundleFile))
  24. } else {
  25. console.info(chalk.green(`✓ Built Bundle [${standalone}]:`), chalk.magenta(bundleFile))
  26. }
  27. })
  28. }
  29. await fs.mkdir(new URL('./uppy/dist', PACKAGES_ROOT), { recursive: true })
  30. await fs.mkdir(new URL('./@uppy/locales/dist', PACKAGES_ROOT), { recursive: true })
  31. const methods = [
  32. buildBundle(
  33. './packages/uppy/index.mjs',
  34. './packages/uppy/dist/uppy.min.mjs',
  35. { standalone: 'Uppy (ESM)', format: 'esm' },
  36. ),
  37. buildBundle(
  38. './packages/uppy/bundle.mjs',
  39. './packages/uppy/dist/uppy.min.js',
  40. { standalone: 'Uppy', format: 'iife' },
  41. ),
  42. ]
  43. // Build minified versions of all the locales
  44. const localesModules = await fs.opendir(new URL('./@uppy/locales/src/', PACKAGES_ROOT))
  45. for await (const dirent of localesModules) {
  46. if (!dirent.isDirectory() && dirent.name.endsWith('.js')) {
  47. const localeName = path.basename(dirent.name, '.js')
  48. methods.push(
  49. buildBundle(
  50. `./packages/@uppy/locales/src/${localeName}.js`,
  51. `./packages/@uppy/locales/dist/${localeName}.min.js`,
  52. { minify: true },
  53. ),
  54. )
  55. }
  56. }
  57. // Add BUNDLE-README.MD
  58. methods.push(
  59. fs.copyFile(
  60. new URL('./BUNDLE-README.md', UPPY_ROOT),
  61. new URL('./uppy/dist/README.md', PACKAGES_ROOT),
  62. ),
  63. )
  64. await Promise.all(methods).then(() => {
  65. console.info(chalk.yellow('✓ JS bundles 🎉'))
  66. }, (err) => {
  67. console.error(chalk.red('✗ Error:'), chalk.red(err.message))
  68. })