shared.js 2.9 KB

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