buildPackage.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/common": path.resolve(__dirname, "../packages/common/src"),
  28. "@excalidraw/element": path.resolve(__dirname, "../packages/element/src"),
  29. "@excalidraw/excalidraw": path.resolve(__dirname, "../packages/excalidraw"),
  30. "@excalidraw/math": path.resolve(__dirname, "../packages/math/src"),
  31. "@excalidraw/utils": path.resolve(__dirname, "../packages/utils/src"),
  32. },
  33. loader: {
  34. ".woff2": "file",
  35. },
  36. });
  37. function buildDev(config) {
  38. return build({
  39. ...config,
  40. sourcemap: true,
  41. define: {
  42. "import.meta.env": JSON.stringify(ENV_VARS.development),
  43. },
  44. });
  45. }
  46. function buildProd(config) {
  47. return build({
  48. ...config,
  49. minify: true,
  50. define: {
  51. "import.meta.env": JSON.stringify(ENV_VARS.production),
  52. },
  53. });
  54. }
  55. const createESMRawBuild = async () => {
  56. const chunksConfig = {
  57. entryPoints: ["index.tsx", "**/*.chunk.ts"],
  58. entryNames: "[name]",
  59. };
  60. // development unminified build with source maps
  61. await buildDev({
  62. ...getConfig("dist/dev"),
  63. ...chunksConfig,
  64. });
  65. // production minified buld without sourcemaps
  66. await buildProd({
  67. ...getConfig("dist/prod"),
  68. ...chunksConfig,
  69. });
  70. };
  71. (async () => {
  72. await createESMRawBuild();
  73. })();