esbuild.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { build, context } from "esbuild";
  2. import { existsSync } from "fs";
  3. import { rm, copyFile, mkdir } from "fs/promises";
  4. const build_dir = "./build/";
  5. const isBuild = process.argv.includes("--build");
  6. const isServe = process.argv.includes("--serve");
  7. if (existsSync(build_dir)) await rm(build_dir, { recursive: true });
  8. await mkdir(build_dir, { recursive: true, mode: 0o755 });
  9. await copyFile("./src/index.html", `${build_dir}/index.html`);
  10. if (isBuild) {
  11. await build({
  12. bundle: true,
  13. entryPoints: ["./src/App.jsx"],
  14. logLevel: "info",
  15. minify: true,
  16. outfile: `${build_dir}/bundle.js`,
  17. sourcemap: false,
  18. target: ["es2019", "edge18", "firefox60", "chrome61", "safari11"],
  19. loader: {
  20. '.js': 'jsx',
  21. '.jpg': 'file',
  22. }
  23. });
  24. }
  25. if (isServe) {
  26. let ctx = await context({
  27. bundle: true,
  28. entryPoints: ["./src/App.jsx"],
  29. logLevel: "info",
  30. minify: false,
  31. outfile: `${build_dir}/bundle.js`,
  32. sourcemap: true,
  33. target: ["esnext"],
  34. loader: {
  35. '.js': 'jsx',
  36. '.jpg': 'file',
  37. }
  38. });
  39. const server = await ctx.serve({ servedir: build_dir });
  40. console.log(server);
  41. }