build-bundle.mjs 2.8 KB

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