You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.1 KiB

  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, sep } = require('path');
  4. const { sync } = require('glob');
  5. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  6. const ManifestPlugin = require('webpack-manifest-plugin');
  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. const entryPacks = [...packPaths, ...localePackPaths].filter(path => path !== join(entryPath, 'custom.js'));
  14. const themePaths = Object.keys(themes).reduce(
  15. (themePaths, name) => {
  16. themePaths[name] = resolve(join(settings.source_path, themes[name]));
  17. return themePaths;
  18. }, {});
  19. module.exports = {
  20. entry: Object.assign(
  21. entryPacks.reduce(
  22. (map, entry) => {
  23. const localMap = map;
  24. let namespace = relative(join(entryPath), dirname(entry));
  25. if (namespace === join('..', '..', '..', 'tmp', 'packs')) {
  26. namespace = ''; // generated by generateLocalePacks.js
  27. }
  28. localMap[join(namespace, basename(entry, extname(entry)))] = resolve(entry);
  29. return localMap;
  30. }, {}
  31. ), themePaths
  32. ),
  33. output: {
  34. filename: '[name].js',
  35. chunkFilename: '[name].js',
  36. path: output.path,
  37. publicPath: output.publicPath,
  38. },
  39. module: {
  40. rules: sync(join(loadersDir, '*.js')).map(loader => require(loader)),
  41. },
  42. plugins: [
  43. new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
  44. new webpack.NormalModuleReplacementPlugin(
  45. /^history\//, (resource) => {
  46. // temporary fix for https://github.com/ReactTraining/react-router/issues/5576
  47. // to reduce bundle size
  48. resource.request = resource.request.replace(/^history/, 'history/es');
  49. }
  50. ),
  51. new ExtractTextPlugin(env.NODE_ENV === 'production' ? '[name]-[contenthash].css' : '[name].css'),
  52. new ManifestPlugin({
  53. publicPath: output.publicPath,
  54. writeToFileEmit: true,
  55. }),
  56. new webpack.optimize.CommonsChunkPlugin({
  57. name: 'common',
  58. minChunks: (module, count) => {
  59. const reactIntlPathRegexp = new RegExp(`node_modules\\${sep}react-intl`);
  60. if (module.resource && reactIntlPathRegexp.test(module.resource)) {
  61. // skip react-intl because it's useless to put in the common chunk,
  62. // e.g. because "shared" modules between zh-TW and zh-CN will never
  63. // be loaded together
  64. return false;
  65. }
  66. return count >= 2;
  67. },
  68. }),
  69. ],
  70. resolve: {
  71. extensions: settings.extensions,
  72. modules: [
  73. resolve(settings.source_path),
  74. 'node_modules',
  75. ],
  76. },
  77. resolveLoader: {
  78. modules: ['node_modules'],
  79. },
  80. node: {
  81. // Called by http-link-header in an API we never use, increases
  82. // bundle size unnecessarily
  83. Buffer: false,
  84. },
  85. };