build-bundle.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. 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/robodog/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.15', proposals: true },
  59. }]],
  60. },
  61. })],
  62. },
  63. ),
  64. buildBundle(
  65. './packages/@uppy/robodog/bundle.js',
  66. './packages/@uppy/robodog/dist/robodog.min.js',
  67. { standalone: 'Robodog' },
  68. ),
  69. ]
  70. // Build minified versions of all the locales
  71. const localesModules = await fs.opendir(new URL('./@uppy/locales/src/', PACKAGES_ROOT))
  72. for await (const dirent of localesModules) {
  73. if (!dirent.isDirectory() && dirent.name.endsWith('.js')) {
  74. const localeName = path.basename(dirent.name, '.js')
  75. methods.push(
  76. buildBundle(
  77. `./packages/@uppy/locales/src/${localeName}.js`,
  78. `./packages/@uppy/locales/dist/${localeName}.min.js`,
  79. { minify: true },
  80. ),
  81. )
  82. }
  83. }
  84. // Add BUNDLE-README.MD
  85. methods.push(
  86. fs.copyFile(
  87. new URL('./BUNDLE-README.md', UPPY_ROOT),
  88. new URL('./uppy/dist/README.md', PACKAGES_ROOT),
  89. ),
  90. )
  91. await Promise.all(methods).then(() => {
  92. console.info(chalk.yellow('✓ JS bundles 🎉'))
  93. }, (err) => {
  94. console.error(chalk.red('✗ Error:'), chalk.red(err.message))
  95. })