1
0

build.js 3.9 KB

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