webpack.config.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2021-2024 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. module.exports = (env, options) => {
  33. const envVariables = {
  34. frontend_version: require(resolveApp('package.json')).version,
  35. frontend_build_date: new Date().toISOString(),
  36. frontend_build_mode: options.mode
  37. };
  38. return [{
  39. mode: options.mode, //'development', //'production',
  40. name: reactBundleName,
  41. entry: ["react", "react-dom",
  42. "@emotion/react","@emotion/styled",
  43. "@mui/icons-material","@mui/material","@mui/x-date-pickers", "@mui/x-tree-view"],
  44. output: {
  45. filename: reactBundle + ".dll.js",
  46. path: webAppPath,
  47. library: reactBundleName,
  48. publicPath: "",
  49. },
  50. plugins: [
  51. new webpack.DllPlugin({
  52. name: reactBundleName,
  53. path: reactManifestPath
  54. })
  55. ]
  56. },
  57. {
  58. mode: options.mode, //'development', //'production',
  59. name: taipyBundleName,
  60. entry: ["./src/extensions/exports.ts"],
  61. output: {
  62. filename: taipyBundle + ".js",
  63. path: webAppPath,
  64. library: {
  65. name: taipyBundleName,
  66. type: "umd"
  67. },
  68. publicPath: "",
  69. },
  70. dependencies: [reactBundleName],
  71. devtool: options.mode === "development" && "inline-source-map",
  72. resolve: {
  73. // Add '.ts' and '.tsx' as resolvable extensions.
  74. extensions: [".ts", ".tsx", ".js"],
  75. },
  76. module: {
  77. rules: [
  78. {
  79. test: /\.tsx?$/,
  80. use: "ts-loader",
  81. exclude: /node_modules/,
  82. },
  83. {
  84. // added to resolve apache-arrow library (don't really understand the problem tbh)
  85. // Reference: https://github.com/graphql/graphql-js/issues/2721
  86. test: /\.m?js/,
  87. resolve: {
  88. fullySpecified: false,
  89. },
  90. },
  91. ]
  92. },
  93. plugins: [
  94. new ESLintPlugin({
  95. extensions: [`ts`, `tsx`],
  96. exclude: [`/node_modules/`],
  97. eslintPath: require.resolve("eslint"),
  98. }),
  99. new webpack.DllReferencePlugin({
  100. name: reactBundleName,
  101. manifest: reactManifestPath
  102. })
  103. ]
  104. },
  105. {
  106. mode: options.mode, //'development', //'production',
  107. context: resolveApp("dom"),
  108. entry: ["./src/index.tsx"],
  109. output: {
  110. filename: "taipy-gui-dom.js",
  111. path: webAppPath,
  112. publicPath: "",
  113. },
  114. dependencies: [taipyBundleName, reactBundleName],
  115. externals: {"taipy-gui": taipyBundleName},
  116. // Enable sourcemaps for debugging webpack's output.
  117. devtool: options.mode === "development" && "inline-source-map",
  118. resolve: {
  119. // Add '.ts' and '.tsx' as resolvable extensions.
  120. extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"],
  121. },
  122. module: {
  123. rules: [
  124. {
  125. test: /\.tsx?$/,
  126. use: "ts-loader",
  127. exclude: /node_modules/,
  128. },
  129. ]
  130. },
  131. plugins: [
  132. new CopyWebpackPlugin({
  133. patterns: [
  134. { from: "../public", filter: (name) => !name.endsWith(".html") },
  135. { from: "../packaging", filter: (name) => !name.includes(".gen.") }
  136. ],
  137. }),
  138. new HtmlWebpackPlugin({
  139. template: "../public/index.html",
  140. hash: true,
  141. ...envVariables
  142. }),
  143. new GenerateJsonPlugin("taipy.status.json", envVariables),
  144. new ESLintPlugin({
  145. extensions: [`ts`, `tsx`],
  146. exclude: [`/node_modules/`],
  147. eslintPath: require.resolve("eslint"),
  148. }),
  149. new webpack.DllReferencePlugin({
  150. name: reactBundleName,
  151. manifest: reactManifestPath
  152. }),
  153. new AddAssetHtmlPlugin([{
  154. filepath: reactDllPath,
  155. hash: true
  156. },{
  157. filepath: taipyDllPath,
  158. hash: true
  159. }]),
  160. ],
  161. },
  162. {
  163. mode: options.mode,
  164. target: "web",
  165. entry: {
  166. "default": "./base/src/index.ts",
  167. "preview": "./base/src/index-preview.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. },
  188. },
  189. module: {
  190. rules: [
  191. {
  192. test: /\.tsx?$/,
  193. use: "ts-loader",
  194. exclude: /node_modules/,
  195. },
  196. ],
  197. },
  198. resolve: {
  199. extensions: [".tsx", ".ts", ".js", ".tsx"],
  200. },
  201. // externals: {
  202. // "socket.io-client": {
  203. // commonjs: "socket.io-client",
  204. // commonjs2: "socket.io-client",
  205. // amd: "socket.io-client",
  206. // root: "_",
  207. // },
  208. // },
  209. }];
  210. };