build.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. let { writeToPackageDotJson, getFromPackageDotJson } = require('./utils');
  2. let fs = require('fs');
  3. let zlib = require('zlib');
  4. ([
  5. // Packages:
  6. 'alpinejs',
  7. 'csp',
  8. // 'history', - removed because this plugin has been moved to livewire/livewire until it's stable...
  9. // 'navigate', - remove because this plugin has been moved to livewire/livewire until it's stable...
  10. 'intersect',
  11. 'persist',
  12. 'collapse',
  13. 'anchor',
  14. 'morph',
  15. 'focus',
  16. 'sort',
  17. 'mask',
  18. 'ui',
  19. ]).forEach(package => {
  20. if (! fs.existsSync(`./packages/${package}/dist`)) {
  21. fs.mkdirSync(`./packages/${package}/dist`, 0744);
  22. }
  23. // Go through each file in the package's "build" directory
  24. // and use the appropriate bundling strategy based on its name.
  25. fs.readdirSync(`./packages/${package}/builds`).forEach(file => {
  26. bundleFile(package, file)
  27. });
  28. })
  29. function bundleFile(package, file) {
  30. // Based on the filename, give esbuild a specific configuration to build.
  31. ({
  32. // This output file is meant to be loaded in a browser's <script> tag.
  33. 'cdn.js': () => {
  34. build({
  35. entryPoints: [`packages/${package}/builds/${file}`],
  36. outfile: `packages/${package}/dist/${file}`,
  37. bundle: true,
  38. platform: 'browser',
  39. define: { CDN: 'true' },
  40. })
  41. // Build a minified version.
  42. build({
  43. entryPoints: [`packages/${package}/builds/${file}`],
  44. outfile: `packages/${package}/dist/${file.replace('.js', '.min.js')}`,
  45. bundle: true,
  46. minify: true,
  47. platform: 'browser',
  48. define: { CDN: 'true' },
  49. }).then(() => {
  50. outputSize(package, `packages/${package}/dist/${file.replace('.js', '.min.js')}`)
  51. })
  52. },
  53. // This file outputs two files: an esm module and a cjs module.
  54. // The ESM one is meant for "import" statements (bundlers and new browsers)
  55. // and the cjs one is meant for "require" statements (node).
  56. 'module.js': () => {
  57. build({
  58. entryPoints: [`packages/${package}/builds/${file}`],
  59. outfile: `packages/${package}/dist/${file.replace('.js', '.esm.js')}`,
  60. bundle: true,
  61. platform: 'neutral',
  62. mainFields: ['module', 'main'],
  63. })
  64. build({
  65. entryPoints: [`packages/${package}/builds/${file}`],
  66. outfile: `packages/${package}/dist/${file.replace('.js', '.cjs.js')}`,
  67. bundle: true,
  68. target: ['node10.4'],
  69. platform: 'node',
  70. }).then(() => {
  71. writeToPackageDotJson(package, 'main', `dist/${file.replace('.js', '.cjs.js')}`)
  72. writeToPackageDotJson(package, 'module', `dist/${file.replace('.js', '.esm.js')}`)
  73. })
  74. },
  75. })[file]()
  76. }
  77. function build(options) {
  78. options.define || (options.define = {})
  79. options.define['ALPINE_VERSION'] = `'${getFromPackageDotJson('alpinejs', 'version')}'`
  80. options.define['process.env.NODE_ENV'] = process.argv.includes('--watch') ? `'production'` : `'development'`
  81. return require('esbuild').build({
  82. logLevel: process.argv.includes('--watch') ? 'info' : 'warning',
  83. watch: process.argv.includes('--watch'),
  84. // external: ['alpinejs'],
  85. ...options,
  86. }).catch(() => process.exit(1))
  87. }
  88. function outputSize(package, file) {
  89. let size = bytesToSize(zlib.brotliCompressSync(fs.readFileSync(file)).length)
  90. console.log("\x1b[32m", `${package}: ${size}`)
  91. }
  92. function bytesToSize(bytes) {
  93. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
  94. if (bytes === 0) return 'n/a'
  95. const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
  96. if (i === 0) return `${bytes} ${sizes[i]}`
  97. return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`
  98. }