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 } = {}) {
  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. plugins,
  20. target,
  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/bundle.js',
  35. './packages/uppy/dist/uppy.min.js',
  36. { standalone: 'Uppy' },
  37. ),
  38. buildBundle(
  39. './packages/uppy/bundle-legacy.js',
  40. './packages/uppy/dist/uppy.legacy.min.js',
  41. {
  42. standalone: 'Uppy (with polyfills)',
  43. target: 'es5',
  44. plugins:[babel({
  45. config:{
  46. compact: false,
  47. highlightCode: false,
  48. inputSourceMap: true,
  49. browserslistEnv: 'legacy',
  50. presets: [['@babel/preset-env', {
  51. loose: false,
  52. targets: { ie:11 },
  53. useBuiltIns: 'entry',
  54. corejs: { version: '3.15', proposals: true },
  55. }]],
  56. },
  57. })],
  58. },
  59. ),
  60. buildBundle(
  61. './packages/@uppy/robodog/bundle.js',
  62. './packages/@uppy/robodog/dist/robodog.min.js',
  63. { standalone: 'Robodog' },
  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. Promise.all(methods).then(() => {
  88. console.info(chalk.yellow('✓ JS bundles 🎉'))
  89. }, (err) => {
  90. console.error(chalk.red('✗ Error:'), chalk.red(err.message))
  91. })