buildPackage.js 3.3 KB

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