build-bundle.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. import babel from 'esbuild-plugin-babel'
  7. const UPPY_ROOT = new URL('../', import.meta.url)
  8. const PACKAGES_ROOT = new URL('./packages/', UPPY_ROOT)
  9. function buildBundle (srcFile, bundleFile, { minify = true, standalone = '', plugins, target, format } = {}) {
  10. return esbuild.build({
  11. bundle: true,
  12. sourcemap: true,
  13. entryPoints: [srcFile],
  14. outfile: bundleFile,
  15. platform: 'browser',
  16. minify,
  17. keepNames: true,
  18. plugins,
  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. buildBundle(
  43. './packages/uppy/bundle-legacy.mjs',
  44. './packages/uppy/dist/uppy.legacy.min.js',
  45. {
  46. standalone: 'Uppy (with polyfills)',
  47. target: 'es5',
  48. plugins:[babel({
  49. config:{
  50. compact: false,
  51. highlightCode: false,
  52. inputSourceMap: true,
  53. browserslistEnv: 'legacy',
  54. presets: [['@babel/preset-env', {
  55. loose: false,
  56. targets: { ie:11 },
  57. useBuiltIns: 'entry',
  58. corejs: { version: '3.24', proposals: true },
  59. }]],
  60. },
  61. })],
  62. },
  63. ),
  64. ]
  65. // Build minified versions of all the locales
  66. const localesModules = await fs.opendir(new URL('./@uppy/locales/src/', PACKAGES_ROOT))
  67. for await (const dirent of localesModules) {
  68. if (!dirent.isDirectory() && dirent.name.endsWith('.js')) {
  69. const localeName = path.basename(dirent.name, '.js')
  70. methods.push(
  71. buildBundle(
  72. `./packages/@uppy/locales/src/${localeName}.js`,
  73. `./packages/@uppy/locales/dist/${localeName}.min.js`,
  74. { minify: true },
  75. ),
  76. )
  77. }
  78. }
  79. // Add BUNDLE-README.MD
  80. methods.push(
  81. fs.copyFile(
  82. new URL('./BUNDLE-README.md', UPPY_ROOT),
  83. new URL('./uppy/dist/README.md', PACKAGES_ROOT),
  84. ),
  85. )
  86. await Promise.all(methods).then(() => {
  87. console.info(chalk.yellow('✓ JS bundles 🎉'))
  88. }, (err) => {
  89. console.error(chalk.red('✗ Error:'), chalk.red(err.message))
  90. })