craco.config.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. const path = require('path');
  2. const fs = require('fs');
  3. const WebpackBar = require('webpackbar');
  4. const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
  5. // const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  6. const {
  7. when,
  8. whenDev,
  9. whenProd,
  10. whenTest,
  11. ESLINT_MODES,
  12. POSTCSS_MODES,
  13. } = require('@craco/craco');
  14. const rewireEntries = [
  15. {
  16. name: 'shareChart',
  17. entry: path.resolve(__dirname, './src/shareChart.entry.ts'),
  18. outPath: 'shareChart.html',
  19. },
  20. {
  21. name: 'shareDashboard',
  22. entry: path.resolve(__dirname, './src/shareDashboard.entry.ts'),
  23. outPath: 'shareDashboard.html',
  24. },
  25. {
  26. name: 'shareStoryPlayer',
  27. entry: path.resolve(__dirname, './src/shareStoryPlayer.entry.ts'),
  28. outPath: 'shareStoryPlayer.html',
  29. },
  30. ];
  31. const defaultEntryName = 'main';
  32. const appIndexes = ['js', 'tsx', 'ts', 'jsx'].map(ext =>
  33. path.resolve(__dirname, `src/index.${ext}`),
  34. );
  35. module.exports = {
  36. babel: {
  37. plugins: ['babel-plugin-styled-components'],
  38. },
  39. webpack: {
  40. alias: {},
  41. plugins: [
  42. new WebpackBar(),
  43. new MonacoWebpackPlugin({ languages: [''] }),
  44. // new BundleAnalyzerPlugin(),
  45. ],
  46. configure: (webpackConfig, { env, paths }) => {
  47. // paths.appPath='public'
  48. // paths.appBuild = 'dist'; // 配合输出打包修改文件目录
  49. // webpackConfig中可以解构出你想要的参数比如mode、devtool、entry等等,更多信息请查看webpackConfig.json文件
  50. /**
  51. * 修改 output
  52. */
  53. webpackConfig.output = {
  54. ...webpackConfig.output,
  55. ...{
  56. filename: whenDev(() => 'static/js/bundle.js', 'static/js/[name].js'),
  57. chunkFilename: 'static/js/[name].[contenthash:8].js',
  58. },
  59. // path: path.resolve(__dirname, 'dist'), // 修改输出文件目录
  60. publicPath: `${process.env.PUBLIC_URL || ''}/`,
  61. };
  62. /**
  63. * webpack split chunks
  64. */
  65. webpackConfig.optimization.splitChunks = {
  66. ...webpackConfig.optimization.splitChunks,
  67. ...{
  68. chunks: 'all',
  69. name: true,
  70. cacheGroups: {
  71. chartGraph: {
  72. name: 'ChartGraph',
  73. test: /[\\/]app[\\/]components[\\/]ChartGraph[\\/]/,
  74. priority: 100,
  75. enforce: true,
  76. },
  77. antdDesign: {
  78. name: 'antdDesign',
  79. test: /[\\/]node_modules[\\/](@ant-design|antd)[\\/]/,
  80. priority: 100,
  81. enforce: true,
  82. },
  83. videoReact: {
  84. name: 'videoReact',
  85. test: /[\\/]node_modules[\\/](video-react)[\\/]/,
  86. priority: 100,
  87. enforce: true,
  88. },
  89. reactGridLayout: {
  90. name: 'reactGridLayout',
  91. test: /[\\/]node_modules[\\/]react-grid-layout[\\/]/,
  92. priority: 100,
  93. enforce: true,
  94. },
  95. rc: {
  96. name: 'rc',
  97. test: /[\\/]node_modules[\\/](rc-tree|react-color|rc-menu|rc-slider|rc-table|rc-util|rc-picker|rc-select|rc-field-form|rc-motion|@babel|@emotion|react-i18next|redux|react-draggable|lodash-es|dnd-core|rc-virtual-list|rc-tabs|rc-trigger|react-dnd|echarts-wordcloud|tinycolor|rc-pagination|rc-drawer|react-quill|rc-input-number|gl-matrix|react-resizable|redux-undo|rc-cascader|rc-image|rc-upload|resize-obserer-polyfill|d3-color|rc-dialog|rc-textarea|rc-overflow)[\\/]/,
  98. priority: 100,
  99. enforce: true,
  100. },
  101. echarts: {
  102. name: 'echarts',
  103. test: /[\\/]node_modules[\\/](echarts|zrender)[\\/]/,
  104. priority: 100,
  105. enforce: true,
  106. },
  107. quill: {
  108. name: 'quill',
  109. test: /[\\/]node_modules[\\/]quill[\\/]/,
  110. priority: 100,
  111. enforce: true,
  112. },
  113. lodash: {
  114. name: 'lodash',
  115. test: /[\\/]node_modules[\\/](lodash|lodash-es)[\\/]/,
  116. priority: 100,
  117. enforce: true,
  118. },
  119. reactdnd: {
  120. name: 'react',
  121. test: /[\\/]node_modules[\\/](react-beautiful-dnd|react-dnd)[\\/]/,
  122. priority: 100,
  123. enforce: true,
  124. },
  125. version: {
  126. name: 'version',
  127. test: /[\\/]node_modules[\\/](moment|react-dom|core-js|i18next|rc-field-form|buffer|axios|react-redux)[\\/]/,
  128. priority: 100,
  129. enforce: true,
  130. },
  131. },
  132. },
  133. };
  134. const instanceOfMiniCssExtractPlugin = webpackConfig.plugins.filter(
  135. plugin => plugin.constructor.name === 'MiniCssExtractPlugin',
  136. )[0];
  137. if (instanceOfMiniCssExtractPlugin) {
  138. instanceOfMiniCssExtractPlugin.options.ignoreOrder = true;
  139. }
  140. const defaultEntryHTMLPlugin = webpackConfig.plugins.filter(plugin => {
  141. return plugin.constructor.name === 'HtmlWebpackPlugin';
  142. })[0];
  143. defaultEntryHTMLPlugin.options.chunks = [defaultEntryName];
  144. // config.entry is not an array in Create React App 4
  145. if (!Array.isArray(webpackConfig.entry)) {
  146. webpackConfig.entry = [webpackConfig.entry];
  147. }
  148. // If there is only one entry file then it should not be necessary for the rest of the entries
  149. const necessaryEntry =
  150. webpackConfig.entry.length === 1
  151. ? []
  152. : webpackConfig.entry.filter(file => !appIndexes.includes(file));
  153. const multipleEntry = {};
  154. multipleEntry[defaultEntryName] = webpackConfig.entry;
  155. rewireEntries.forEach(entry => {
  156. multipleEntry[entry.name] = necessaryEntry.concat(entry.entry);
  157. // Multiple Entry HTML Plugin
  158. webpackConfig.plugins.unshift(
  159. new defaultEntryHTMLPlugin.constructor(
  160. Object.assign({}, defaultEntryHTMLPlugin.options, {
  161. filename: entry.outPath,
  162. // template: entry.template,
  163. chunks: [entry.name],
  164. }),
  165. ),
  166. );
  167. });
  168. webpackConfig.entry = multipleEntry;
  169. // Multiple Entry Output File
  170. let names = webpackConfig.output.filename.split('/').reverse();
  171. if (names[0].indexOf('[name]') === -1) {
  172. names[0] = '[name].' + names[0];
  173. webpackConfig.output.filename = names.reverse().join('/');
  174. }
  175. // 返回重写后的新配置
  176. return webpackConfig;
  177. },
  178. },
  179. jest: {
  180. configure: (jestConfig, { env, paths, resolve, rootDir }) => {
  181. return Object.assign(jestConfig, {
  182. setupFiles: ['jest-canvas-mock'],
  183. });
  184. },
  185. modulePaths: ['../'],
  186. },
  187. devServer: {
  188. before: function (app, server, compiler) {
  189. app.get('/api/v1/plugins/custom/charts', function (req, res) {
  190. const pluginPath = 'custom-chart-plugins';
  191. const dir = fs.readdirSync(`./public/${pluginPath}`);
  192. res.json({
  193. data: (dir || [])
  194. .filter(file => path.extname(file) === '.js')
  195. .map(file => `${pluginPath}/${file}`),
  196. errCode: 0,
  197. success: true,
  198. });
  199. });
  200. },
  201. hot: true,
  202. port: 8081,
  203. proxy: {
  204. '/api/v1': {
  205. changeOrigin: true,
  206. target: 'http://116.153.88.3:18081/',
  207. //target: 'http://192.168.2.11:8080/',
  208. },
  209. '/resources': {
  210. changeOrigin: true,
  211. target: 'http://192.168.3.114:8080/',
  212. },
  213. '/login/oauth2': {
  214. changeOrigin: true,
  215. target: 'http://192.168.3.114:8080/',
  216. },
  217. '/oauth2': {
  218. changeOrigin: true,
  219. target: 'http://192.168.3.114:8080/',
  220. },
  221. },
  222. historyApiFallback: {
  223. rewrites: [
  224. { from: /^\/$/, to: '/index.html' },
  225. { from: /^\/shareChart\/\w/, to: '/shareChart.html' },
  226. { from: /^\/shareDashboard\/\w/, to: '/shareDashboard.html' },
  227. { from: /^\/shareStoryPlayer\/\w/, to: '/shareStoryPlayer.html' },
  228. ],
  229. },
  230. },
  231. };