webpack.config.js 6.4 KB

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