webpack.common.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/app': './src/client/js/app',
  22. 'js/admin': './src/client/js/admin',
  23. 'js/installer': './src/client/js/installer',
  24. 'js/legacy': './src/client/js/legacy/crowi',
  25. 'js/legacy-presentation': './src/client/js/legacy/crowi-presentation',
  26. 'js/plugin': './src/client/js/plugin',
  27. 'js/ie11-polyfill': './src/client/js/ie11-polyfill',
  28. 'js/hackmd-agent': './src/client/js/hackmd-agent',
  29. 'js/hackmd-styles': './src/client/js/hackmd-styles',
  30. // styles
  31. 'styles/style-app': './src/client/styles/scss/style-app.scss',
  32. 'styles/style-presentation': './src/client/styles/scss/style-presentation.scss',
  33. // themes
  34. 'styles/theme-default': './src/client/styles/scss/theme/default.scss',
  35. 'styles/theme-default-dark': './src/client/styles/scss/theme/default-dark.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-blue-night': './src/client/styles/scss/theme/blue-night.scss',
  40. 'styles/theme-kibela': './src/client/styles/scss/theme/kibela.scss',
  41. 'styles/theme-halloween': './src/client/styles/scss/theme/halloween.scss',
  42. 'styles/theme-wood': './src/client/styles/scss/theme/wood.scss',
  43. 'styles/theme-christmas': './src/client/styles/scss/theme/christmas.scss',
  44. 'styles/theme-island': './src/client/styles/scss/theme/island.scss',
  45. 'styles/theme-spring': './src/client/styles/scss/theme/spring.scss',
  46. 'styles/theme-antarctic': './src/client/styles/scss/theme/antarctic.scss',
  47. // styles for external services
  48. 'styles/style-hackmd': './src/client/styles/hackmd/style.scss',
  49. }, options.entry || {}), // Merge with env dependent settings
  50. output: Object.assign({
  51. path: helpers.root('public'),
  52. publicPath: '/',
  53. filename: '[name].bundle.js',
  54. }, options.output || {}), // Merge with env dependent settings
  55. externals: {
  56. // require("jquery") is external and available
  57. // on the global var jQuery
  58. jquery: 'jQuery',
  59. emojione: 'emojione',
  60. hljs: 'hljs',
  61. },
  62. resolve: {
  63. extensions: ['.js', '.jsx', '.json'],
  64. modules: ((options.resolve && options.resolve.modules) || []).concat([helpers.root('node_modules')]),
  65. alias: {
  66. '@root': helpers.root('/'),
  67. '@commons': helpers.root('src/lib'),
  68. '@client': helpers.root('src/client'),
  69. '@tmp': helpers.root('tmp'),
  70. '@alias/logger': helpers.root('src/lib/service/logger'),
  71. '@alias/locales': helpers.root('resource/locales'),
  72. // replace bunyan
  73. bunyan: 'browser-bunyan',
  74. },
  75. },
  76. module: {
  77. rules: options.module.rules.concat([
  78. {
  79. test: /.jsx?$/,
  80. exclude: {
  81. test: helpers.root('node_modules'),
  82. exclude: [ // include as a result
  83. { test: helpers.root('node_modules', 'growi-plugin-') },
  84. helpers.root('node_modules/growi-commons'),
  85. helpers.root('node_modules/codemirror/src'),
  86. ],
  87. },
  88. use: [{
  89. loader: 'babel-loader?cacheDirectory',
  90. }],
  91. },
  92. {
  93. test: /locales/,
  94. loader: '@alienfast/i18next-loader',
  95. options: {
  96. basenameAsNamespace: true,
  97. },
  98. },
  99. { // see https://github.com/abpetkov/switchery/issues/120
  100. test: /switchery\.js$/,
  101. loader: 'imports-loader?module=>false,exports=>false,define=>false,this=>window',
  102. },
  103. /*
  104. * File loader for supporting images, for example, in CSS files.
  105. */
  106. {
  107. test: /\.(jpg|png|gif)$/,
  108. use: 'file-loader',
  109. },
  110. /* File loader for supporting fonts, for example, in CSS files.
  111. */
  112. {
  113. test: /\.(eot|woff2?|svg|ttf)([?]?.*)$/,
  114. use: 'null-loader',
  115. },
  116. ]),
  117. },
  118. plugins: options.plugins.concat([
  119. new WebpackAssetsManifest({ publicPath: true }),
  120. new webpack.DefinePlugin({
  121. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  122. }),
  123. // ignore
  124. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  125. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  126. new HardSourceWebpackPlugin(),
  127. new HardSourceWebpackPlugin.ExcludeModulePlugin([
  128. {
  129. // see https://github.com/mzgoddard/hard-source-webpack-plugin/blob/master/README.md#excludemoduleplugin
  130. test: /mini-css-extract-plugin[\\/]dist[\\/]loader/,
  131. },
  132. ]),
  133. new LodashModuleReplacementPlugin({
  134. flattening: true,
  135. }),
  136. new webpack.ProvidePlugin({ // refs externals
  137. jQuery: 'jquery',
  138. $: 'jquery',
  139. }),
  140. ]),
  141. devtool: options.devtool,
  142. target: 'web', // Make web variables accessible to webpack, e.g. window
  143. optimization: {
  144. namedModules: true,
  145. splitChunks: {
  146. cacheGroups: {
  147. style_commons: {
  148. test: /\.(sc|sa|c)ss$/,
  149. chunks: (chunk) => {
  150. // ignore patterns
  151. return chunk.name != null && !chunk.name.match(/style-|theme-|legacy-presentation/);
  152. },
  153. name: 'styles/style-commons',
  154. minSize: 1,
  155. priority: 30,
  156. enforce: true,
  157. },
  158. commons: {
  159. test: /(src|resource)[\\/].*\.(js|jsx|json)$/,
  160. chunks: 'initial',
  161. name: 'js/commons',
  162. minChunks: 2,
  163. minSize: 1,
  164. priority: 20,
  165. },
  166. vendors: {
  167. test: /node_modules[\\/].*\.(js|jsx|json)$/,
  168. chunks: (chunk) => {
  169. // ignore patterns
  170. return chunk.name != null && !chunk.name.match(/legacy-presentation|ie11-polyfill|hackmd-/);
  171. },
  172. name: 'js/vendors',
  173. minSize: 1,
  174. priority: 10,
  175. enforce: true,
  176. },
  177. },
  178. },
  179. minimizer: options.optimization.minimizer || [],
  180. },
  181. performance: options.performance || {},
  182. stats: options.stats || {},
  183. };
  184. };