webpack.common.js 6.3 KB

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