buildPackage.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const path = require("path");
  2. const { build } = require("esbuild");
  3. const { sassPlugin } = require("esbuild-sass-plugin");
  4. const { parseEnvVariables } = require("../packages/excalidraw/env.cjs");
  5. const ENV_VARS = {
  6. development: {
  7. ...parseEnvVariables(`${__dirname}/../.env.development`),
  8. DEV: true,
  9. },
  10. production: {
  11. ...parseEnvVariables(`${__dirname}/../.env.production`),
  12. PROD: true,
  13. },
  14. };
  15. // excludes all external dependencies and bundles only the source code
  16. const getConfig = (outdir) => ({
  17. outdir,
  18. bundle: true,
  19. splitting: true,
  20. format: "esm",
  21. packages: "external",
  22. plugins: [sassPlugin()],
  23. target: "es2020",
  24. assetNames: "[dir]/[name]",
  25. chunkNames: "[dir]/[name]-[hash]",
  26. alias: {
  27. "@excalidraw/excalidraw": path.resolve(__dirname, "../packages/excalidraw"),
  28. "@excalidraw/utils": path.resolve(__dirname, "../packages/utils"),
  29. "@excalidraw/math": path.resolve(__dirname, "../packages/math"),
  30. },
  31. loader: {
  32. ".woff2": "file",
  33. },
  34. });
  35. function buildDev(config) {
  36. return build({
  37. ...config,
  38. sourcemap: true,
  39. define: {
  40. "import.meta.env": JSON.stringify(ENV_VARS.development),
  41. },
  42. });
  43. }
  44. function buildProd(config) {
  45. return build({
  46. ...config,
  47. minify: true,
  48. define: {
  49. "import.meta.env": JSON.stringify(ENV_VARS.production),
  50. },
  51. });
  52. }
  53. const createESMRawBuild = async () => {
  54. const chunksConfig = {
  55. entryPoints: ["index.tsx", "**/*.chunk.ts"],
  56. entryNames: "[name]",
  57. };
  58. // development unminified build with source maps
  59. await buildDev({
  60. ...getConfig("dist/dev"),
  61. ...chunksConfig,
  62. });
  63. // production minified buld without sourcemaps
  64. await buildProd({
  65. ...getConfig("dist/prod"),
  66. ...chunksConfig,
  67. });
  68. };
  69. (async () => {
  70. await createESMRawBuild();
  71. })();