buildPackage.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const { build } = require("esbuild");
  2. const { sassPlugin } = require("esbuild-sass-plugin");
  3. const { externalGlobalPlugin } = require("esbuild-plugin-external-global");
  4. // Will be used later for treeshaking
  5. //const fs = require("fs");
  6. // const path = require("path");
  7. // function getFiles(dir, files = []) {
  8. // const fileList = fs.readdirSync(dir);
  9. // for (const file of fileList) {
  10. // const name = `${dir}/${file}`;
  11. // if (
  12. // name.includes("node_modules") ||
  13. // name.includes("config") ||
  14. // name.includes("package.json") ||
  15. // name.includes("main.js") ||
  16. // name.includes("index-node.ts") ||
  17. // name.endsWith(".d.ts")
  18. // ) {
  19. // continue;
  20. // }
  21. // if (fs.statSync(name).isDirectory()) {
  22. // getFiles(name, files);
  23. // } else if (
  24. // !(
  25. // name.match(/\.(sa|sc|c)ss$/) ||
  26. // name.match(/\.(woff|woff2|eot|ttf|otf)$/) ||
  27. // name.match(/locales\/[^/]+\.json$/)
  28. // )
  29. // ) {
  30. // continue;
  31. // } else {
  32. // files.push(name);
  33. // }
  34. // }
  35. // return files;
  36. // }
  37. const browserConfig = {
  38. entryPoints: ["index.tsx"],
  39. bundle: true,
  40. format: "esm",
  41. plugins: [
  42. sassPlugin(),
  43. externalGlobalPlugin({
  44. react: "React",
  45. "react-dom": "ReactDOM",
  46. }),
  47. ],
  48. splitting: true,
  49. loader: {
  50. ".woff2": "file",
  51. },
  52. };
  53. const createESMBrowserBuild = async () => {
  54. // Development unminified build with source maps
  55. await build({
  56. ...browserConfig,
  57. outdir: "dist/browser/dev",
  58. sourcemap: true,
  59. chunkNames: "excalidraw-assets-dev/[name]-[hash]",
  60. assetNames: "excalidraw-assets-dev/[name]-[hash]",
  61. define: {
  62. "import.meta.env": JSON.stringify({ DEV: true }),
  63. },
  64. });
  65. // production minified build without sourcemaps
  66. await build({
  67. ...browserConfig,
  68. outdir: "dist/browser/prod",
  69. minify: true,
  70. chunkNames: "excalidraw-assets/[name]-[hash]",
  71. assetNames: "excalidraw-assets/[name]-[hash]",
  72. define: {
  73. "import.meta.env": JSON.stringify({ PROD: true }),
  74. },
  75. });
  76. };
  77. // const BASE_PATH = `${path.resolve(`${__dirname}/..`)}`;
  78. // const filesinExcalidrawPackage = [
  79. // ...getFiles(`${BASE_PATH}/packages/excalidraw`),
  80. // `${BASE_PATH}/packages/utils/export.ts`,
  81. // `${BASE_PATH}/packages/utils/bbox.ts`,
  82. // ...getFiles(`${BASE_PATH}/public/fonts`),
  83. // ];
  84. // const filesToTransform = filesinExcalidrawPackage.filter((file) => {
  85. // return !(
  86. // file.includes("/__tests__/") ||
  87. // file.includes(".test.") ||
  88. // file.includes("/tests/") ||
  89. // file.includes("example")
  90. // );
  91. // });
  92. const rawConfig = {
  93. entryPoints: ["index.tsx"],
  94. bundle: true,
  95. format: "esm",
  96. plugins: [sassPlugin()],
  97. loader: {
  98. ".json": "copy",
  99. ".woff2": "file",
  100. },
  101. packages: "external",
  102. };
  103. const createESMRawBuild = async () => {
  104. // Development unminified build with source maps
  105. await build({
  106. ...rawConfig,
  107. sourcemap: true,
  108. outdir: "dist/dev",
  109. define: {
  110. "import.meta.env": JSON.stringify({ DEV: true }),
  111. },
  112. });
  113. // production minified build without sourcemaps
  114. await build({
  115. ...rawConfig,
  116. minify: true,
  117. outdir: "dist/prod",
  118. define: {
  119. "import.meta.env": JSON.stringify({ PROD: true }),
  120. },
  121. });
  122. };
  123. createESMRawBuild();
  124. createESMBrowserBuild();