webpack-config.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* eslint-disable @typescript-eslint/no-require-imports */
  2. const path = require("path");
  3. function withNCoreUIKitWebpack(config, webpack) {
  4. if(!config.resolve) {
  5. config.resolve = {};
  6. }
  7. if(!config.resolve.fallback) {
  8. config.resolve.fallback = {};
  9. }
  10. config.resolve.fallback = {
  11. ...config.resolve.fallback,
  12. crypto: require.resolve("crypto-browserify"),
  13. stream: require.resolve("stream-browserify"),
  14. process: require.resolve("process/browser"),
  15. vm: require.resolve("vm-browserify"),
  16. buffer: require.resolve("buffer/"),
  17. util: require.resolve("util/")
  18. };
  19. if(!config.resolve.alias) {
  20. config.resolve.alias = {};
  21. }
  22. config.resolve.alias = {
  23. ...config.resolve.alias,
  24. "react-native$": "react-native-web"
  25. };
  26. if(!config.resolve.extensions) {
  27. config.resolve.extensions = [];
  28. }
  29. const webExtensions = [
  30. ".web.tsx",
  31. ".web.ts",
  32. ".web.js",
  33. ".tsx",
  34. ".ts",
  35. ".js",
  36. ".mjs"
  37. ];
  38. webExtensions.forEach(ext => {
  39. if(!config.resolve.extensions.includes(ext)) {
  40. config.resolve.extensions.push(ext);
  41. }
  42. });
  43. if(!config.plugins) {
  44. config.plugins = [];
  45. }
  46. if(webpack) {
  47. config.plugins.push(
  48. new webpack.ProvidePlugin({
  49. process: "process/browser",
  50. Buffer: [
  51. "buffer",
  52. "Buffer"
  53. ]
  54. })
  55. );
  56. }
  57. return config;
  58. }
  59. function createDefaultWebpackConfig({
  60. HtmlWebpackPlugin,
  61. appDirectory,
  62. ESLintPlugin,
  63. CopyPlugin,
  64. webpack
  65. }) {
  66. const babelLoaderConfiguration = {
  67. use: {
  68. options: {
  69. presets: [
  70. "module:@react-native/babel-preset",
  71. "@babel/preset-typescript"
  72. ],
  73. plugins: [
  74. "react-native-web"
  75. ],
  76. cacheDirectory: true
  77. },
  78. loader: "babel-loader"
  79. },
  80. include: [
  81. path.resolve(appDirectory, "index.tsx"),
  82. path.resolve(appDirectory, "src")
  83. ],
  84. test: /\.(tsx|ts|js|jsx)$/
  85. };
  86. const imageLoaderConfiguration = {
  87. test: /\.(gif|jpe?g|png|svg)$/,
  88. use: {
  89. options: {
  90. name: "[name].[ext]",
  91. esModule: false
  92. },
  93. loader: "url-loader"
  94. }
  95. };
  96. let config = {
  97. plugins: [
  98. new webpack.DefinePlugin({
  99. "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || "development"),
  100. __DEV__: JSON.stringify(process.env.NODE_ENV !== "production"),
  101. "__REACT_DEVTOOLS_GLOBAL_HOOK__": "({ isDisabled: true })",
  102. "process.env.EXPO_OS": JSON.stringify("web"),
  103. "process.env": JSON.stringify({})
  104. })
  105. ],
  106. module: {
  107. rules: [
  108. babelLoaderConfiguration,
  109. imageLoaderConfiguration,
  110. {
  111. include: /node_modules\/@react-navigation/,
  112. resolve: {
  113. fullySpecified: false
  114. },
  115. test: /\.(js|mjs|jsx)$/,
  116. type: "javascript/auto"
  117. },
  118. {
  119. include: /node_modules/,
  120. type: "javascript/auto",
  121. test: /\.mjs$/
  122. },
  123. {
  124. generator: {
  125. filename: "assets/fonts/[name][ext]"
  126. },
  127. test: /\.(ttf|otf|eot|woff|woff2)$/,
  128. type: "asset/resource"
  129. }
  130. ]
  131. },
  132. resolve: {
  133. modules: [
  134. path.resolve(appDirectory, "node_modules"),
  135. "node_modules"
  136. ],
  137. symlinks: false
  138. },
  139. output: {
  140. path: path.resolve(appDirectory, "dist"),
  141. filename: "bundle.web.js",
  142. publicPath: "/"
  143. },
  144. entry: [
  145. path.resolve(appDirectory, "index.tsx")
  146. ],
  147. ignoreWarnings: [
  148. {
  149. module: /lucide-react-native/
  150. },
  151. {
  152. module: /@react-navigation/
  153. },
  154. {
  155. module: /ncore-ui-kit/
  156. }
  157. ],
  158. devServer: {
  159. historyApiFallback: true,
  160. client: {
  161. overlay: {
  162. warnings: false,
  163. errors: true
  164. },
  165. logging: "none"
  166. },
  167. port: 3000
  168. },
  169. devtool: "source-map",
  170. mode: "development"
  171. };
  172. if(HtmlWebpackPlugin) {
  173. config.plugins.push(
  174. new HtmlWebpackPlugin({
  175. template: path.resolve(appDirectory, "./public/index.html"),
  176. filename: "index.html"
  177. })
  178. );
  179. }
  180. if(ESLintPlugin) {
  181. config.plugins.push(
  182. new ESLintPlugin({
  183. overrideConfigFile: path.resolve(appDirectory, "eslint.config.mjs"),
  184. failOnError: process.env.NODE_ENV === "production",
  185. exclude: [
  186. path.resolve(appDirectory, "node_modules"),
  187. path.resolve(appDirectory, "dist")
  188. ],
  189. eslintPath: "eslint/use-at-your-own-risk",
  190. configType: "flat",
  191. emitWarning: true,
  192. extensions: [
  193. "tsx",
  194. "jsx",
  195. "ts",
  196. "js"
  197. ]
  198. })
  199. );
  200. }
  201. if(CopyPlugin) {
  202. config.plugins.push(
  203. new CopyPlugin({
  204. patterns: [
  205. {
  206. globOptions: {
  207. ignore: [
  208. "**/index.html"
  209. ],
  210. dot: true
  211. },
  212. noErrorOnMissing: true,
  213. from: "public",
  214. to: "."
  215. }
  216. ]
  217. })
  218. );
  219. }
  220. config = withNCoreUIKitWebpack(config, webpack);
  221. return config;
  222. }
  223. module.exports = {
  224. createDefaultWebpackConfig,
  225. withNCoreUIKitWebpack
  226. };