build.js 3.8 KB

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