webpack-config.js 6.6 KB

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