闭社主体 forked from https://github.com/tootsuite/mastodon
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.

90 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(env.NODE_ENV === 'production' ? '[name]-[hash].css' : '[name].css'),
  41. new ManifestPlugin({
  42. publicPath: output.publicPath,
  43. writeToFileEmit: true,
  44. }),
  45. new webpack.optimize.CommonsChunkPlugin({
  46. name: 'common',
  47. minChunks: (module, count) => {
  48. const reactIntlPathRegexp = new RegExp(`node_modules\\${sep}react-intl`);
  49. if (module.resource && reactIntlPathRegexp.test(module.resource)) {
  50. // skip react-intl because it's useless to put in the common chunk,
  51. // e.g. because "shared" modules between zh-TW and zh-CN will never
  52. // be loaded together
  53. return false;
  54. }
  55. return count >= 2;
  56. },
  57. }),
  58. ],
  59. resolve: {
  60. alias: {
  61. 'mastodon-application-style': existsSync(customApplicationStyle) ?
  62. customApplicationStyle : originalApplicationStyle,
  63. },
  64. extensions: settings.extensions,
  65. modules: [
  66. resolve(settings.source_path),
  67. 'node_modules',
  68. ],
  69. },
  70. resolveLoader: {
  71. modules: ['node_modules'],
  72. },
  73. node: {
  74. // Called by http-link-header in an API we never use, increases
  75. // bundle size unnecessarily
  76. Buffer: false,
  77. },
  78. };