1
0

build.js 3.9 KB

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