build.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Build JS and CSS using esbuild and Lightning CSS
  2. import { promises as fs } from 'node:fs';
  3. import path from 'node:path';
  4. import browserslist from 'browserslist';
  5. import esbuild from 'esbuild';
  6. import * as lightningcss from 'lightningcss';
  7. // Packages to build but exclude from bundle
  8. const externalPackages = [
  9. 'chart.js/auto',
  10. 'alpinejs/dist/cdn.min.js',
  11. '@alpinejs/collapse/dist/cdn.min.js',
  12. 'xterm',
  13. 'xterm-addon-webgl',
  14. 'xterm-addon-canvas',
  15. ];
  16. // Build main bundle
  17. async function buildJS() {
  18. const inputPath = './web/js/src/index.js';
  19. try {
  20. await esbuild.build({
  21. entryPoints: [inputPath],
  22. outfile: './web/js/dist/main.min.js',
  23. bundle: true,
  24. minify: true,
  25. sourcemap: true,
  26. external: externalPackages,
  27. });
  28. console.log('✅ JavaScript build completed for', inputPath);
  29. } catch (error) {
  30. console.error('❌ Error building JavaScript:', error);
  31. process.exit(1);
  32. }
  33. }
  34. // Build external packages
  35. async function buildExternalJS() {
  36. try {
  37. const buildPromises = externalPackages.map(async (pkg) => {
  38. const outputPath = getOutputPath(pkg);
  39. await esbuild.build({
  40. entryPoints: [pkg],
  41. outfile: outputPath,
  42. bundle: true,
  43. minify: true,
  44. format: 'esm',
  45. });
  46. console.log(`✅ Dependency build completed for ${pkg}`);
  47. });
  48. await Promise.all(buildPromises);
  49. } catch (error) {
  50. console.error('❌ Error building external packages:', error);
  51. process.exit(1);
  52. }
  53. }
  54. function getOutputPath(pkg) {
  55. let pkgName;
  56. if (pkg.startsWith('alpinejs')) {
  57. pkgName = 'alpinejs';
  58. } else if (pkg.startsWith('@alpinejs/collapse')) {
  59. pkgName = 'alpinejs-collapse';
  60. } else {
  61. pkgName = pkg.replace(/\//g, '-');
  62. }
  63. return `./web/js/dist/${pkgName}.min.js`;
  64. }
  65. // Process a CSS file
  66. async function processCSS(inputFile, outputFile) {
  67. try {
  68. await ensureDir(path.dirname(outputFile));
  69. const css = await fs.readFile(inputFile);
  70. const bundle = await lightningcss.bundleAsync({
  71. filename: inputFile,
  72. sourceMap: true,
  73. code: Buffer.from(css),
  74. minify: true,
  75. targets: lightningcss.browserslistToTargets(browserslist()),
  76. drafts: { customMedia: true, nesting: true },
  77. visitor: {
  78. Url: (node) => {
  79. // Fix relative paths for webfonts
  80. if (node.url.startsWith('../webfonts/')) {
  81. return {
  82. url: node.url.replace('../webfonts/', '/webfonts/'),
  83. loc: node.loc,
  84. };
  85. }
  86. return node;
  87. },
  88. },
  89. resolver: {
  90. resolve(specifier, from) {
  91. if (!specifier.endsWith('.css')) {
  92. specifier += '.css';
  93. }
  94. if (specifier.startsWith('node:')) {
  95. return `node_modules/${specifier.replace('node:', '')}`;
  96. }
  97. return `${path.dirname(from)}/${specifier}`;
  98. },
  99. },
  100. });
  101. await fs.writeFile(outputFile, bundle.code);
  102. await fs.writeFile(`${outputFile}.map`, bundle.map);
  103. console.log(`✅ CSS build completed for ${inputFile}`);
  104. } catch (error) {
  105. console.error(`❌ Error processing CSS for ${inputFile}:`, error);
  106. process.exit(1);
  107. }
  108. }
  109. // Build CSS
  110. async function buildCSS() {
  111. const themesSourcePath = './web/css/src/themes/';
  112. const cssEntries = await fs.readdir(themesSourcePath);
  113. const cssBuildPromises = cssEntries
  114. .filter((entry) => path.extname(entry) === '.css')
  115. .map(async (entry) => {
  116. const entryName = entry.replace('.css', '.min.css');
  117. const inputPath = path.join(themesSourcePath, entry);
  118. const outputPath = `./web/css/themes/${entryName}`;
  119. await processCSS(inputPath, outputPath);
  120. });
  121. await Promise.all(cssBuildPromises);
  122. }
  123. // Ensure a directory exists
  124. async function ensureDir(dir) {
  125. try {
  126. await fs.mkdir(dir, { recursive: true });
  127. } catch (error) {
  128. if (error.code !== 'EEXIST') {
  129. throw error;
  130. }
  131. }
  132. }
  133. // Build all assets
  134. async function build() {
  135. console.log('🚀 Building JS and CSS...');
  136. await buildJS();
  137. await buildExternalJS();
  138. await buildCSS();
  139. console.log('🎉 Build completed.');
  140. }
  141. // Execute build
  142. build();