webpack.common.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @author: Yuki Takei <yuki@weseek.co.jp>
  3. */
  4. const webpack = require('webpack');
  5. const helpers = require('./helpers');
  6. /*
  7. * Webpack Plugins
  8. */
  9. const WebpackAssetsManifest = require('webpack-assets-manifest');
  10. const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
  11. /*
  12. * Webpack configuration
  13. *
  14. * See: http://webpack.github.io/docs/configuration.html#cli
  15. */
  16. module.exports = (options) => {
  17. return {
  18. mode: options.mode,
  19. entry: Object.assign({
  20. 'js/app': './src/client/js/app',
  21. 'js/legacy': './src/client/js/legacy/crowi',
  22. 'js/legacy-admin': './src/client/js/legacy/crowi-admin',
  23. 'js/legacy-presentation': './src/client/js/legacy/crowi-presentation',
  24. 'js/plugin': './src/client/js/plugin',
  25. 'js/ie11-polyfill': './src/client/js/ie11-polyfill',
  26. 'js/hackmd-agent': './src/client/js/hackmd-agent',
  27. 'js/hackmd-styles': './src/client/js/hackmd-styles',
  28. // styles
  29. 'styles/style': './src/client/styles/scss/style.scss',
  30. 'styles/style-presentation': './src/client/styles/scss/style-presentation.scss',
  31. // themes
  32. 'styles/theme-default': './src/client/styles/scss/theme/default.scss',
  33. 'styles/theme-default-dark': './src/client/styles/scss/theme/default-dark.scss',
  34. 'styles/theme-nature': './src/client/styles/scss/theme/nature.scss',
  35. 'styles/theme-mono-blue': './src/client/styles/scss/theme/mono-blue.scss',
  36. 'styles/theme-future': './src/client/styles/scss/theme/future.scss',
  37. 'styles/theme-blue-night': './src/client/styles/scss/theme/blue-night.scss',
  38. // styles for external services
  39. 'styles/style-hackmd': './src/client/styles/hackmd/style.scss',
  40. }, options.entry || {}), // Merge with env dependent settings
  41. output: Object.assign({
  42. path: helpers.root('public'),
  43. publicPath: '/',
  44. filename: '[name].bundle.js',
  45. }, options.output || {}), // Merge with env dependent settings
  46. externals: {
  47. // require("jquery") is external and available
  48. // on the global var jQuery
  49. 'jquery': 'jQuery',
  50. 'emojione': 'emojione',
  51. 'hljs': 'hljs',
  52. },
  53. resolve: {
  54. extensions: ['.js', '.jsx', '.json'],
  55. modules: [helpers.root('node_modules')],
  56. alias: {
  57. '@root': helpers.root('/'),
  58. '@commons': helpers.root('lib'),
  59. '@tmp': helpers.root('tmp'),
  60. '@alias/logger': helpers.root('lib/service/logger'),
  61. '@alias/locales': helpers.root('lib/locales'),
  62. // replace bunyan
  63. 'bunyan': 'browser-bunyan',
  64. }
  65. },
  66. module: {
  67. rules: options.module.rules.concat([
  68. {
  69. test: /.jsx?$/,
  70. exclude: {
  71. test: helpers.root('node_modules'),
  72. exclude: [ // include as a result
  73. helpers.root('node_modules/string-width'),
  74. helpers.root('node_modules/is-fullwidth-code-point'), // depends from string-width
  75. ]
  76. },
  77. use: [{
  78. loader: 'babel-loader?cacheDirectory'
  79. }]
  80. },
  81. {
  82. test: /locales/,
  83. loader: '@alienfast/i18next-loader',
  84. options: {
  85. basenameAsNamespace: true,
  86. }
  87. },
  88. { // see https://github.com/abpetkov/switchery/issues/120
  89. test: /switchery\.js$/,
  90. loader: 'imports-loader?module=>false,exports=>false,define=>false,this=>window'
  91. },
  92. {
  93. test: /\.css$/,
  94. use: ['style-loader', 'css-loader'],
  95. exclude: [helpers.root('src/client/styles')]
  96. },
  97. {
  98. test: /\.scss$/,
  99. use: ['style-loader', 'css-loader', 'sass-loader'],
  100. exclude: [helpers.root('src/client/styles')]
  101. },
  102. /*
  103. * File loader for supporting images, for example, in CSS files.
  104. */
  105. {
  106. test: /\.(jpg|png|gif)$/,
  107. use: 'file-loader',
  108. },
  109. /* File loader for supporting fonts, for example, in CSS files.
  110. */
  111. {
  112. test: /\.(eot|woff2?|svg|ttf)([?]?.*)$/,
  113. use: 'null-loader',
  114. }
  115. ])
  116. },
  117. plugins: options.plugins.concat([
  118. new WebpackAssetsManifest({ publicPath: true }),
  119. new webpack.DefinePlugin({
  120. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  121. }),
  122. // ignore
  123. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  124. new LodashModuleReplacementPlugin({
  125. flattening: true
  126. }),
  127. new webpack.ProvidePlugin({ // refs externals
  128. jQuery: 'jquery',
  129. $: 'jquery',
  130. }),
  131. ]),
  132. devtool: options.devtool,
  133. target: 'web', // Make web variables accessible to webpack, e.g. window
  134. optimization: {
  135. namedModules: true,
  136. splitChunks: {
  137. cacheGroups: {
  138. commons: {
  139. test: /src/,
  140. chunks: 'initial',
  141. name: 'js/commons',
  142. minChunks: 2,
  143. minSize: 1,
  144. priority: 20
  145. },
  146. vendors: {
  147. test: /node_modules/,
  148. chunks: (chunk) => {
  149. // ignore patterns
  150. return chunk.name != null && !chunk.name.match(/legacy-presentation|ie11-polyfill|hackmd-/);
  151. },
  152. name: 'js/vendors',
  153. // minChunks: 2,
  154. minSize: 1,
  155. priority: 10,
  156. enforce: true
  157. }
  158. }
  159. },
  160. minimizer: options.optimization.minimizer || [],
  161. },
  162. performance: options.performance || {},
  163. stats: options.stats || {},
  164. };
  165. };