build-bundle.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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: target !== 'es5',
  18. plugins,
  19. tsconfigRaw: '{}',
  20. target,
  21. format,
  22. }).then(() => {
  23. if (minify) {
  24. console.info(chalk.green(`✓ Built Minified Bundle [${standalone}]:`), chalk.magenta(bundleFile))
  25. } else {
  26. console.info(chalk.green(`✓ Built Bundle [${standalone}]:`), chalk.magenta(bundleFile))
  27. }
  28. })
  29. }
  30. await fs.mkdir(new URL('./uppy/dist', PACKAGES_ROOT), { recursive: true })
  31. await fs.mkdir(new URL('./@uppy/locales/dist', PACKAGES_ROOT), { recursive: true })
  32. const methods = [
  33. buildBundle(
  34. './packages/uppy/index.mjs',
  35. './packages/uppy/dist/uppy.min.mjs',
  36. { standalone: 'Uppy (ESM)', format: 'esm' },
  37. ),
  38. buildBundle(
  39. './packages/uppy/bundle.mjs',
  40. './packages/uppy/dist/uppy.min.js',
  41. { standalone: 'Uppy', format: 'iife' },
  42. ),
  43. buildBundle(
  44. './packages/uppy/bundle-legacy.mjs',
  45. './packages/uppy/dist/uppy.legacy.min.js',
  46. {
  47. standalone: 'Uppy (with polyfills)',
  48. target: 'es5',
  49. plugins:[babel({
  50. config:{
  51. compact: false,
  52. highlightCode: false,
  53. inputSourceMap: true,
  54. browserslistEnv: 'legacy',
  55. presets: [['@babel/preset-env', {
  56. loose: false,
  57. targets: { ie:11 },
  58. useBuiltIns: 'entry',
  59. corejs: { version: '3.24', proposals: true },
  60. }]],
  61. },
  62. })],
  63. },
  64. ),
  65. ]
  66. // Build minified versions of all the locales
  67. const localesModules = await fs.opendir(new URL('./@uppy/locales/src/', PACKAGES_ROOT))
  68. for await (const dirent of localesModules) {
  69. if (!dirent.isDirectory() && dirent.name.endsWith('.js')) {
  70. const localeName = path.basename(dirent.name, '.js')
  71. methods.push(
  72. buildBundle(
  73. `./packages/@uppy/locales/src/${localeName}.js`,
  74. `./packages/@uppy/locales/dist/${localeName}.min.js`,
  75. { minify: true },
  76. ),
  77. )
  78. }
  79. }
  80. // Add BUNDLE-README.MD
  81. methods.push(
  82. fs.copyFile(
  83. new URL('./BUNDLE-README.md', UPPY_ROOT),
  84. new URL('./uppy/dist/README.md', PACKAGES_ROOT),
  85. ),
  86. )
  87. await Promise.all(methods).then(() => {
  88. console.info(chalk.yellow('✓ JS bundles 🎉'))
  89. }, (err) => {
  90. console.error(chalk.red('✗ Error:'), chalk.red(err.message))
  91. })