buildPackage.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 rawConfigCommon = {
  93. bundle: true,
  94. format: "esm",
  95. plugins: [sassPlugin()],
  96. assetNames: "[dir]/[name]-[hash]",
  97. loader: {
  98. ".json": "copy",
  99. ".woff2": "file",
  100. },
  101. packages: "external",
  102. // chunks are always external, so they are not bundled within and get build separately
  103. external: ["*.chunk"],
  104. };
  105. const rawConfigIndex = {
  106. ...rawConfigCommon,
  107. entryPoints: ["index.tsx"],
  108. };
  109. const rawConfigChunks = {
  110. ...rawConfigCommon,
  111. // create a separate chunk for each
  112. entryPoints: ["**/*.chunk.ts"],
  113. };
  114. function buildDev(chunkConfig) {
  115. const config = {
  116. ...chunkConfig,
  117. sourcemap: true,
  118. define: {
  119. "import.meta.env": JSON.stringify({ DEV: true }),
  120. },
  121. outdir: "dist/dev",
  122. };
  123. return build(config);
  124. }
  125. function buildProd(chunkConfig) {
  126. const config = {
  127. ...chunkConfig,
  128. minify: true,
  129. define: {
  130. "import.meta.env": JSON.stringify({ PROD: true }),
  131. },
  132. outdir: "dist/prod",
  133. };
  134. return build(config);
  135. }
  136. const createESMRawBuild = async () => {
  137. // development unminified build with source maps
  138. await buildDev(rawConfigIndex);
  139. await buildDev(rawConfigChunks);
  140. // production minified buld without sourcemaps
  141. await buildProd(rawConfigIndex);
  142. await buildProd(rawConfigChunks);
  143. };
  144. // otherwise throws "ERROR: Could not resolve "./subset-worker.chunk"
  145. (async () => {
  146. await createESMRawBuild();
  147. await createESMBrowserBuild();
  148. })();