build.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. let dirNameRegex = new RegExp("\\" + path.sep + "([^\\" + path.sep + "]+)$");
  59. await fs.mkdir(outputPath.replace(dirNameRegex, ""), { recursive: true });
  60. await fs.copyFile(file, outputPath);
  61. };
  62. const medusaTransform = async (file) => {
  63. if (compileExtensions.some((ext) => file.endsWith(ext))) {
  64. const outputPath = getOutputPath(file, { inputDir, outputDir });
  65. const output = await swc.transformFile(file, {
  66. sourceFileName: path.relative(path.dirname(outputPath), file),
  67. sourceMaps: "inline",
  68. module: {
  69. type: "commonjs",
  70. },
  71. jsc: {
  72. parser: {
  73. syntax: "typescript",
  74. decorators: true,
  75. },
  76. transform: {
  77. decoratorMetadata: true,
  78. },
  79. },
  80. });
  81. await writeToOut(file, output.code, {
  82. inputDir,
  83. outputDir,
  84. targetExtension: ".js",
  85. });
  86. } else if (!ignoreExtensions.some((ext) => file.endsWith(ext))) {
  87. // Copy non-ts files
  88. await copyToOut(file, { inputDir, outputDir });
  89. }
  90. };
  91. // Compile all files
  92. await Promise.all(files.map(async (file) => medusaTransform(file)));
  93. console.log(`Compiled all files in ${Date.now() - started}ms`);