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.

93 lines
3.0 KiB

  1. // Note: You must restart bin/webpack-dev-server for changes to take effect
  2. const { existsSync } = require('fs');
  3. const webpack = require('webpack');
  4. const { basename, dirname, join, relative, resolve, sep } = require('path');
  5. const { sync } = require('glob');
  6. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  7. const ManifestPlugin = require('webpack-manifest-plugin');
  8. const extname = require('path-complete-extname');
  9. const { env, settings, output, loadersDir } = require('./configuration.js');
  10. const localePackPaths = require('./generateLocalePacks');
  11. const extensionGlob = `**/*{${settings.extensions.join(',')}}*`;
  12. const entryPath = join(settings.source_path, settings.source_entry_path);
  13. const packPaths = sync(join(entryPath, extensionGlob));
  14. const entryPacks = [...packPaths, ...localePackPaths].filter(path => path !== join(entryPath, 'custom.js'));
  15. const customApplicationStyle = resolve(join(settings.source_path, 'styles/custom.scss'));
  16. const originalApplicationStyle = resolve(join(settings.source_path, 'styles/application.scss'));
  17. module.exports = {
  18. entry: entryPacks.reduce(
  19. (map, entry) => {
  20. const localMap = map;
  21. let namespace = relative(join(entryPath), dirname(entry));
  22. if (namespace === join('..', '..', '..', 'tmp', 'packs')) {
  23. namespace = ''; // generated by generateLocalePacks.js
  24. }
  25. localMap[join(namespace, basename(entry, extname(entry)))] = resolve(entry);
  26. return localMap;
  27. }, {}
  28. ),
  29. output: {
  30. filename: '[name].js',
  31. chunkFilename: '[name]-[chunkhash].js',
  32. path: output.path,
  33. publicPath: output.publicPath,
  34. },
  35. module: {
  36. rules: sync(join(loadersDir, '*.js')).map(loader => require(loader)),
  37. },
  38. plugins: [
  39. new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
  40. new ExtractTextPlugin({
  41. filename: env.NODE_ENV === 'production' ? '[name]-[hash].css' : '[name].css',
  42. allChunks: true,
  43. }),
  44. new ManifestPlugin({
  45. publicPath: output.publicPath,
  46. writeToFileEmit: true,
  47. }),
  48. new webpack.optimize.CommonsChunkPlugin({
  49. name: 'common',
  50. minChunks: (module, count) => {
  51. const reactIntlPathRegexp = new RegExp(`node_modules\\${sep}react-intl`);
  52. if (module.resource && reactIntlPathRegexp.test(module.resource)) {
  53. // skip react-intl because it's useless to put in the common chunk,
  54. // e.g. because "shared" modules between zh-TW and zh-CN will never
  55. // be loaded together
  56. return false;
  57. }
  58. return count >= 2;
  59. },
  60. }),
  61. ],
  62. resolve: {
  63. alias: {
  64. 'mastodon-application-style': existsSync(customApplicationStyle) ?
  65. customApplicationStyle : originalApplicationStyle,
  66. },
  67. extensions: settings.extensions,
  68. modules: [
  69. resolve(settings.source_path),
  70. 'node_modules',
  71. ],
  72. },
  73. resolveLoader: {
  74. modules: ['node_modules'],
  75. },
  76. node: {
  77. // Called by http-link-header in an API we never use, increases
  78. // bundle size unnecessarily
  79. Buffer: false,
  80. },
  81. };