webpack.config.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright 2021-2025 Avaiga Private Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. // webpack should be in the node_modules directory, install if not.
  14. const path = require("path");
  15. const webpack = require("webpack");
  16. const CopyWebpackPlugin = require("copy-webpack-plugin");
  17. const HtmlWebpackPlugin = require("html-webpack-plugin");
  18. const AddAssetHtmlPlugin = require("add-asset-html-webpack-plugin");
  19. const ESLintPlugin = require("eslint-webpack-plugin");
  20. const GenerateJsonPlugin = require("generate-json-webpack-plugin");
  21. const resolveApp = relativePath => path.resolve(__dirname, relativePath);
  22. const reactBundle = "taipy-gui-deps";
  23. const taipyBundle = "taipy-gui";
  24. const reactBundleName = "TaipyGuiDependencies";
  25. const taipyBundleName = "TaipyGui";
  26. const taipyGuiBaseBundleName = "TaipyGuiBase";
  27. const basePath = "../../taipy/gui/webapp";
  28. const webAppPath = resolveApp(basePath);
  29. const reactManifestPath = resolveApp(basePath + "/" + reactBundle + "-manifest.json");
  30. const reactDllPath = resolveApp(basePath + "/" + reactBundle + ".dll.js");
  31. const taipyDllPath = resolveApp(basePath + "/" + taipyBundle + ".js");
  32. const taipyGuiBaseExportPath = resolveApp(basePath + "/taipy-gui-base-export");
  33. module.exports = (env, options) => {
  34. const envVariables = {
  35. frontend_version: require(resolveApp("package.json")).version,
  36. frontend_build_date: new Date().toISOString(),
  37. frontend_build_mode: options.mode,
  38. };
  39. return [{
  40. mode: options.mode, //'development', //'production',
  41. name: reactBundleName,
  42. entry: ["react", "react-dom", "date-fns",
  43. "@emotion/react","@emotion/styled",
  44. "@mui/icons-material","@mui/material","@mui/x-date-pickers", "@mui/x-tree-view"],
  45. output: {
  46. filename: reactBundle + ".dll.js",
  47. path: webAppPath,
  48. library: reactBundleName,
  49. publicPath: "",
  50. },
  51. plugins: [
  52. new webpack.DllPlugin({
  53. name: reactBundleName,
  54. path: reactManifestPath
  55. })
  56. ]
  57. },
  58. {
  59. mode: options.mode, //'development', //'production',
  60. name: taipyBundleName,
  61. entry: ["./src/extensions/exports.ts"],
  62. output: {
  63. filename: taipyBundle + ".js",
  64. path: webAppPath,
  65. library: {
  66. name: taipyBundleName,
  67. type: "umd",
  68. },
  69. publicPath: "",
  70. },
  71. dependencies: [reactBundleName],
  72. devtool: options.mode === "development" && "inline-source-map",
  73. resolve: {
  74. // Add '.ts' and '.tsx' as resolvable extensions.
  75. extensions: [".ts", ".tsx", ".js"],
  76. },
  77. module: {
  78. rules: [
  79. {
  80. test: /\.tsx?$/,
  81. use: "ts-loader",
  82. exclude: /node_modules/,
  83. },
  84. {
  85. // added to resolve apache-arrow library (don't really understand the problem tbh)
  86. // Reference: https://github.com/graphql/graphql-js/issues/2721
  87. test: /\.m?js/,
  88. resolve: {
  89. fullySpecified: false,
  90. },
  91. },
  92. ],
  93. },
  94. plugins: [
  95. new ESLintPlugin({
  96. extensions: [`ts`, `tsx`],
  97. exclude: [`/node_modules/`],
  98. eslintPath: require.resolve("eslint"),
  99. }),
  100. new webpack.DllReferencePlugin({
  101. name: reactBundleName,
  102. manifest: reactManifestPath,
  103. }),
  104. ],
  105. },
  106. {
  107. mode: options.mode, //'development', //'production',
  108. context: resolveApp("dom"),
  109. entry: ["./src/index.tsx"],
  110. output: {
  111. filename: "taipy-gui-dom.js",
  112. path: webAppPath,
  113. publicPath: "",
  114. },
  115. dependencies: [taipyBundleName, reactBundleName],
  116. externals: { "taipy-gui": taipyBundleName },
  117. // Enable sourcemaps for debugging webpack's output.
  118. devtool: options.mode === "development" && "inline-source-map",
  119. resolve: {
  120. // Add '.ts' and '.tsx' as resolvable extensions.
  121. extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"],
  122. },
  123. module: {
  124. rules: [
  125. {
  126. test: /\.tsx?$/,
  127. use: "ts-loader",
  128. exclude: /node_modules/,
  129. },
  130. ],
  131. },
  132. plugins: [
  133. new CopyWebpackPlugin({
  134. patterns: [
  135. { from: "../public", filter: (name) => !name.endsWith(".html") },
  136. { from: "../packaging", filter: (name) => !name.includes(".gen.") },
  137. ],
  138. }),
  139. new HtmlWebpackPlugin({
  140. template: "../public/index.html",
  141. hash: true,
  142. ...envVariables,
  143. }),
  144. new GenerateJsonPlugin("taipy.status.json", envVariables),
  145. new ESLintPlugin({
  146. extensions: [`ts`, `tsx`],
  147. exclude: [`/node_modules/`],
  148. eslintPath: require.resolve("eslint"),
  149. }),
  150. new webpack.DllReferencePlugin({
  151. name: reactBundleName,
  152. manifest: reactManifestPath,
  153. }),
  154. new AddAssetHtmlPlugin([{
  155. filepath: reactDllPath,
  156. hash: true,
  157. }, {
  158. filepath: taipyDllPath,
  159. hash: true,
  160. }]),
  161. ],
  162. },
  163. {
  164. mode: options.mode,
  165. target: "web",
  166. entry: {
  167. "default": "./base/src/index.ts",
  168. },
  169. output: {
  170. filename: (arg) => {
  171. if (arg.chunk.name === "default") {
  172. return "taipy-gui-base.js";
  173. }
  174. return "[name].taipy-gui-base.js";
  175. },
  176. chunkFilename: "[name].taipy-gui-base.js",
  177. path: webAppPath,
  178. globalObject: "this",
  179. library: {
  180. name: taipyGuiBaseBundleName,
  181. type: "umd",
  182. },
  183. },
  184. optimization: {
  185. splitChunks: {
  186. chunks: "all",
  187. name: "shared",
  188. },
  189. },
  190. module: {
  191. rules: [
  192. {
  193. test: /\.tsx?$/,
  194. use: "ts-loader",
  195. exclude: /node_modules/,
  196. },
  197. ],
  198. },
  199. resolve: {
  200. extensions: [".tsx", ".ts", ".js", ".tsx"],
  201. },
  202. // externals: {
  203. // "socket.io-client": {
  204. // commonjs: "socket.io-client",
  205. // commonjs2: "socket.io-client",
  206. // amd: "socket.io-client",
  207. // root: "_",
  208. // },
  209. // },
  210. },
  211. {
  212. entry: "./base/src/exports.ts",
  213. output: {
  214. filename: "taipy-gui-base.js",
  215. path: taipyGuiBaseExportPath,
  216. library: {
  217. name: taipyGuiBaseBundleName,
  218. type: "umd",
  219. },
  220. publicPath: "",
  221. },
  222. module: {
  223. rules: [
  224. {
  225. test: /\.tsx?$/,
  226. use: "ts-loader",
  227. exclude: /node_modules/,
  228. },
  229. ],
  230. },
  231. resolve: {
  232. extensions: [".tsx", ".ts", ".js", ".tsx"],
  233. },
  234. plugins: [
  235. new CopyWebpackPlugin({
  236. patterns: [
  237. { from: "./base/src/packaging", to: taipyGuiBaseExportPath },
  238. ],
  239. }),
  240. ],
  241. }];
  242. };