webpack.common.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @author: Yuki Takei <yuki@weseek.co.jp>
  3. */
  4. const webpack = require('webpack');
  5. /*
  6. * Webpack Plugins
  7. */
  8. const WebpackAssetsManifest = require('webpack-assets-manifest');
  9. const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
  10. const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
  11. const helpers = require('../src/lib/util/helpers');
  12. /*
  13. * Webpack configuration
  14. *
  15. * See: http://webpack.github.io/docs/configuration.html#cli
  16. */
  17. module.exports = (options) => {
  18. return {
  19. mode: options.mode,
  20. entry: Object.assign({
  21. 'js/boot': './src/client/js/boot',
  22. 'js/app': './src/client/js/app',
  23. 'js/admin': './src/client/js/admin',
  24. 'js/nologin': './src/client/js/nologin',
  25. 'js/legacy': './src/client/js/legacy/crowi',
  26. 'js/legacy-presentation': './src/client/js/legacy/crowi-presentation',
  27. 'js/plugin': './src/client/js/plugin',
  28. 'js/ie11-polyfill': './src/client/js/ie11-polyfill',
  29. 'js/hackmd-agent': './src/client/js/hackmd-agent',
  30. 'js/hackmd-styles': './src/client/js/hackmd-styles',
  31. // styles
  32. 'styles/style-app': './src/client/styles/scss/style-app.scss',
  33. 'styles/style-presentation': './src/client/styles/scss/style-presentation.scss',
  34. // themes
  35. 'styles/theme-default': './src/client/styles/scss/theme/default.scss',
  36. 'styles/theme-nature': './src/client/styles/scss/theme/nature.scss',
  37. 'styles/theme-mono-blue': './src/client/styles/scss/theme/mono-blue.scss',
  38. 'styles/theme-future': './src/client/styles/scss/theme/future.scss',
  39. 'styles/theme-kibela': './src/client/styles/scss/theme/kibela.scss',
  40. 'styles/theme-halloween': './src/client/styles/scss/theme/halloween.scss',
  41. 'styles/theme-christmas': './src/client/styles/scss/theme/christmas.scss',
  42. 'styles/theme-wood': './src/client/styles/scss/theme/wood.scss',
  43. 'styles/theme-island': './src/client/styles/scss/theme/island.scss',
  44. 'styles/theme-antarctic': './src/client/styles/scss/theme/antarctic.scss',
  45. 'styles/theme-spring': './src/client/styles/scss/theme/spring.scss',
  46. // styles for external services
  47. 'styles/style-hackmd': './src/client/styles/hackmd/style.scss',
  48. }, options.entry || {}), // Merge with env dependent settings
  49. output: Object.assign({
  50. path: helpers.root('public'),
  51. publicPath: '/',
  52. filename: '[name].bundle.js',
  53. }, options.output || {}), // Merge with env dependent settings
  54. externals: {
  55. // require("jquery") is external and available
  56. // on the global var jQuery
  57. jquery: 'jQuery',
  58. emojione: 'emojione',
  59. hljs: 'hljs',
  60. },
  61. resolve: {
  62. extensions: ['.js', '.jsx', '.json'],
  63. modules: ((options.resolve && options.resolve.modules) || []).concat([helpers.root('node_modules')]),
  64. alias: {
  65. '@root': helpers.root('/'),
  66. '@commons': helpers.root('src/lib'),
  67. '@client': helpers.root('src/client'),
  68. '@tmp': helpers.root('tmp'),
  69. '@alias/logger': helpers.root('src/lib/service/logger'),
  70. '@alias/locales': helpers.root('resource/locales'),
  71. // replace bunyan
  72. bunyan: 'browser-bunyan',
  73. },
  74. },
  75. module: {
  76. rules: options.module.rules.concat([
  77. {
  78. test: /.jsx?$/,
  79. exclude: {
  80. test: helpers.root('node_modules'),
  81. exclude: [ // include as a result
  82. { test: helpers.root('node_modules', 'growi-plugin-') },
  83. helpers.root('node_modules/growi-commons'),
  84. helpers.root('node_modules/codemirror/src'),
  85. ],
  86. },
  87. use: [{
  88. loader: 'babel-loader?cacheDirectory',
  89. }],
  90. },
  91. {
  92. test: /locales/,
  93. loader: '@alienfast/i18next-loader',
  94. options: {
  95. basenameAsNamespace: true,
  96. },
  97. },
  98. { // see https://github.com/abpetkov/switchery/issues/120
  99. test: /switchery\.js$/,
  100. loader: 'imports-loader?module=>false,exports=>false,define=>false,this=>window',
  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 webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  125. new HardSourceWebpackPlugin(),
  126. new HardSourceWebpackPlugin.ExcludeModulePlugin([
  127. {
  128. // see https://github.com/mzgoddard/hard-source-webpack-plugin/blob/master/README.md#excludemoduleplugin
  129. test: /mini-css-extract-plugin[\\/]dist[\\/]loader/,
  130. },
  131. ]),
  132. new LodashModuleReplacementPlugin({
  133. flattening: true,
  134. }),
  135. new webpack.ProvidePlugin({ // refs externals
  136. jQuery: 'jquery',
  137. $: 'jquery',
  138. }),
  139. ]),
  140. devtool: options.devtool,
  141. target: 'web', // Make web variables accessible to webpack, e.g. window
  142. optimization: {
  143. namedModules: true,
  144. splitChunks: {
  145. cacheGroups: {
  146. style_commons: {
  147. test: /\.(sc|sa|c)ss$/,
  148. chunks: (chunk) => {
  149. // ignore patterns
  150. return chunk.name != null && !chunk.name.match(/style-|theme-|legacy-presentation/);
  151. },
  152. name: 'styles/style-commons',
  153. minSize: 1,
  154. priority: 30,
  155. enforce: true,
  156. },
  157. commons: {
  158. test: /(src|resource)[\\/].*\.(js|jsx|json)$/,
  159. chunks: (chunk) => {
  160. // ignore patterns
  161. return chunk.name != null && !chunk.name.match(/boot/);
  162. },
  163. name: 'js/commons',
  164. minChunks: 2,
  165. minSize: 1,
  166. priority: 20,
  167. },
  168. vendors: {
  169. test: /node_modules[\\/].*\.(js|jsx|json)$/,
  170. chunks: (chunk) => {
  171. // ignore patterns
  172. return chunk.name != null && !chunk.name.match(/boot|legacy-presentation|ie11-polyfill|hackmd-/);
  173. },
  174. name: 'js/vendors',
  175. minSize: 1,
  176. priority: 10,
  177. enforce: true,
  178. },
  179. },
  180. },
  181. minimizer: options.optimization.minimizer || [],
  182. },
  183. performance: options.performance || {},
  184. stats: options.stats || {},
  185. };
  186. };