buildPackage.js 1.7 KB

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