webpack.common.js 7.0 KB

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