shared.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Note: You must restart bin/webpack-dev-server for changes to take effect
  2. const webpack = require('webpack');
  3. const { basename, dirname, join, relative, resolve } = require('path');
  4. const { sync } = require('glob');
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const AssetsManifestPlugin = require('webpack-assets-manifest');
  7. const extname = require('path-complete-extname');
  8. const { env, settings, themes, output, loadersDir } = require('./configuration.js');
  9. const localePackPaths = require('./generateLocalePacks');
  10. const extensionGlob = `**/*{${settings.extensions.join(',')}}*`;
  11. const entryPath = join(settings.source_path, settings.source_entry_path);
  12. const packPaths = sync(join(entryPath, extensionGlob));
  13. module.exports = {
  14. entry: Object.assign(
  15. packPaths.reduce((map, entry) => {
  16. const localMap = map;
  17. const namespace = relative(join(entryPath), dirname(entry));
  18. localMap[join(namespace, basename(entry, extname(entry)))] = resolve(entry);
  19. return localMap;
  20. }, {}),
  21. localePackPaths.reduce((map, entry) => {
  22. const localMap = map;
  23. localMap[basename(entry, extname(entry, extname(entry)))] = resolve(entry);
  24. return localMap;
  25. }, {}),
  26. Object.keys(themes).reduce((themePaths, name) => {
  27. themePaths[name] = resolve(join(settings.source_path, themes[name]));
  28. return themePaths;
  29. }, {})
  30. ),
  31. output: {
  32. filename: '[name].js',
  33. chunkFilename: '[name].js',
  34. path: output.path,
  35. publicPath: output.publicPath,
  36. },
  37. optimization: {
  38. runtimeChunk: {
  39. name: 'common',
  40. },
  41. splitChunks: {
  42. cacheGroups: {
  43. default: false,
  44. vendors: false,
  45. common: {
  46. name: 'common',
  47. chunks: 'all',
  48. minChunks: 2,
  49. minSize: 0,
  50. test: /^(?!.*[\\\/]node_modules[\\\/]react-intl[\\\/]).+$/,
  51. },
  52. },
  53. },
  54. occurrenceOrder: true,
  55. },
  56. module: {
  57. rules: sync(join(loadersDir, '*.js')).map(loader => require(loader)),
  58. },
  59. plugins: [
  60. new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
  61. new webpack.NormalModuleReplacementPlugin(
  62. /^history\//, (resource) => {
  63. // temporary fix for https://github.com/ReactTraining/react-router/issues/5576
  64. // to reduce bundle size
  65. resource.request = resource.request.replace(/^history/, 'history/es');
  66. }
  67. ),
  68. new MiniCssExtractPlugin({
  69. filename: env.NODE_ENV === 'production' ? '[name]-[contenthash].css' : '[name].css',
  70. }),
  71. new AssetsManifestPlugin({
  72. publicPath: true,
  73. writeToDisk: true,
  74. }),
  75. ],
  76. resolve: {
  77. extensions: settings.extensions,
  78. modules: [
  79. resolve(settings.source_path),
  80. 'node_modules',
  81. ],
  82. },
  83. resolveLoader: {
  84. modules: ['node_modules'],
  85. },
  86. node: {
  87. // Called by http-link-header in an API we never use, increases
  88. // bundle size unnecessarily
  89. Buffer: false,
  90. },
  91. };