build.js 3.7 KB

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