build-bundle.mjs 2.9 KB

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