webpack.config.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. 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",
  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. "preview": "./base/src/index-preview.ts",
  169. },
  170. output: {
  171. filename: (arg) => {
  172. if (arg.chunk.name === "default") {
  173. return "taipy-gui-base.js";
  174. }
  175. return "[name].taipy-gui-base.js";
  176. },
  177. chunkFilename: "[name].taipy-gui-base.js",
  178. path: webAppPath,
  179. globalObject: "this",
  180. library: {
  181. name: taipyGuiBaseBundleName,
  182. type: "umd",
  183. },
  184. },
  185. optimization: {
  186. splitChunks: {
  187. chunks: 'all',
  188. name: "shared",
  189. },
  190. },
  191. module: {
  192. rules: [
  193. {
  194. test: /\.tsx?$/,
  195. use: "ts-loader",
  196. exclude: /node_modules/,
  197. },
  198. ],
  199. },
  200. resolve: {
  201. extensions: [".tsx", ".ts", ".js", ".tsx"],
  202. },
  203. // externals: {
  204. // "socket.io-client": {
  205. // commonjs: "socket.io-client",
  206. // commonjs2: "socket.io-client",
  207. // amd: "socket.io-client",
  208. // root: "_",
  209. // },
  210. // },
  211. },
  212. {
  213. entry: "./base/src/exports.ts",
  214. output: {
  215. filename: "taipy-gui-base.js",
  216. path: taipyGuiBaseExportPath,
  217. library: {
  218. name: taipyGuiBaseBundleName,
  219. type: "umd",
  220. },
  221. publicPath: "",
  222. },
  223. module: {
  224. rules: [
  225. {
  226. test: /\.tsx?$/,
  227. use: "ts-loader",
  228. exclude: /node_modules/,
  229. },
  230. ],
  231. },
  232. resolve: {
  233. extensions: [".tsx", ".ts", ".js", ".tsx"],
  234. },
  235. plugins: [
  236. new CopyWebpackPlugin({
  237. patterns: [
  238. { from: "./base/src/packaging", to: taipyGuiBaseExportPath },
  239. ],
  240. }),
  241. ],
  242. }];
  243. };