build.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import swc from "@swc/core";
  2. import fs from "node:fs/promises";
  3. import path from "path";
  4. const inputDir = "./src";
  5. const outputDir = "./dist";
  6. const compileExtensions = [".ts", ".tsx", ".js", ".jsx"];
  7. const ignoreExtensions = [".md"];
  8. const dryRun = false;
  9. const started = Date.now();
  10. const inputDirName = path.basename(inputDir);
  11. const outputDirName = path.basename(outputDir);
  12. // Recursively find all files to compile
  13. const findFiles = async (dir, { allowNonExistent } = {}) => {
  14. try {
  15. let files = await fs.readdir(dir, { withFileTypes: true });
  16. let paths = await Promise.all(
  17. files.map(async (file) => {
  18. const res = path.join(dir, file.name);
  19. return file.isDirectory() ? findFiles(res) : res;
  20. }),
  21. );
  22. return paths.flat();
  23. } catch (e) {
  24. if (allowNonExistent && e.code === "ENOENT") {
  25. return [];
  26. }
  27. throw e;
  28. }
  29. };
  30. const files = await findFiles(inputDir);
  31. // Clear out dir
  32. await fs.rm(outputDir, { recursive: true }).catch(() => {});
  33. const getOutputPath = (file, config) => {
  34. const { inputDir, outputDir, targetExtension } = config;
  35. const relativePath = file.replace(inputDirName, outputDirName);
  36. let outputPath = relativePath;
  37. if (targetExtension) {
  38. const currentExtension = path.extname(outputPath);
  39. outputPath = outputPath.replace(currentExtension, targetExtension);
  40. }
  41. return outputPath;
  42. };
  43. const writeToOut = async (file, content, config) => {
  44. const outputPath = getOutputPath(file, config);
  45. if (dryRun) {
  46. console.log(`Writing ${file} to ${outputPath}`);
  47. return;
  48. }
  49. await fs.mkdir(outputPath.replace(/\/[^/]+$/, ""), { recursive: true });
  50. await fs.writeFile(outputPath, content);
  51. };
  52. const copyToOut = async (file, config) => {
  53. const outputPath = getOutputPath(file, config);
  54. if (dryRun) {
  55. console.log(`Copying ${file} to ${outputPath}`);
  56. return;
  57. }
  58. await fs.mkdir(outputPath.replace(/\/[^/]+$/, ""), { recursive: true });
  59. await fs.copyFile(file, outputPath);
  60. };
  61. const medusaTransform = async (file) => {
  62. if (compileExtensions.some((ext) => file.endsWith(ext))) {
  63. const outputPath = getOutputPath(file, { inputDir, outputDir });
  64. const output = await swc.transformFile(file, {
  65. sourceFileName: path.relative(path.dirname(outputPath), file),
  66. sourceMaps: "inline",
  67. module: {
  68. type: "commonjs",
  69. },
  70. jsc: {
  71. parser: {
  72. syntax: "typescript",
  73. decorators: true,
  74. },
  75. transform: {
  76. decoratorMetadata: true,
  77. },
  78. },
  79. });
  80. await writeToOut(file, output.code, {
  81. inputDir,
  82. outputDir,
  83. targetExtension: ".js",
  84. });
  85. } else if (!ignoreExtensions.some((ext) => file.endsWith(ext))) {
  86. // Copy non-ts files
  87. await copyToOut(file, { inputDir, outputDir });
  88. }
  89. };
  90. // Compile all files
  91. await Promise.all(files.map(async (file) => medusaTransform(file)));
  92. console.log(`Compiled all files in ${Date.now() - started}ms`);