build.js 3.9 KB

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