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": "copy",
  51. ".ttf": "copy",
  52. },
  53. };
  54. const createESMBrowserBuild = async () => {
  55. // Development unminified build with source maps
  56. await build({
  57. ...browserConfig,
  58. outdir: "dist/browser/dev",
  59. sourcemap: true,
  60. chunkNames: "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. define: {
  72. "import.meta.env": JSON.stringify({ PROD: true }),
  73. },
  74. });
  75. };
  76. // const BASE_PATH = `${path.resolve(`${__dirname}/..`)}`;
  77. // const filesinExcalidrawPackage = [
  78. // ...getFiles(`${BASE_PATH}/packages/excalidraw`),
  79. // `${BASE_PATH}/packages/utils/export.ts`,
  80. // `${BASE_PATH}/packages/utils/bbox.ts`,
  81. // ...getFiles(`${BASE_PATH}/public/fonts`),
  82. // ];
  83. // const filesToTransform = filesinExcalidrawPackage.filter((file) => {
  84. // return !(
  85. // file.includes("/__tests__/") ||
  86. // file.includes(".test.") ||
  87. // file.includes("/tests/") ||
  88. // file.includes("example")
  89. // );
  90. // });
  91. const rawConfig = {
  92. entryPoints: ["index.tsx"],
  93. bundle: true,
  94. format: "esm",
  95. plugins: [sassPlugin()],
  96. loader: {
  97. ".woff2": "copy",
  98. ".ttf": "copy",
  99. ".json": "copy",
  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();