webpack.common.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @author: Yuki Takei <yuki@weseek.co.jp>
  3. */
  4. const webpack = require('webpack');
  5. const helpers = require('../src/lib/util/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/installer': './src/client/js/installer',
  22. 'js/legacy': './src/client/js/legacy/crowi',
  23. 'js/legacy-admin': './src/client/js/legacy/crowi-admin',
  24. 'js/legacy-presentation': './src/client/js/legacy/crowi-presentation',
  25. 'js/plugin': './src/client/js/plugin',
  26. 'js/ie11-polyfill': './src/client/js/ie11-polyfill',
  27. 'js/hackmd-agent': './src/client/js/hackmd-agent',
  28. 'js/hackmd-styles': './src/client/js/hackmd-styles',
  29. // styles
  30. 'styles/style': './src/client/styles/scss/style.scss',
  31. 'styles/style-presentation': './src/client/styles/scss/style-presentation.scss',
  32. // themes
  33. 'styles/theme-default': './src/client/styles/scss/theme/default.scss',
  34. 'styles/theme-default-dark': './src/client/styles/scss/theme/default-dark.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-blue-night': './src/client/styles/scss/theme/blue-night.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-wood': './src/client/styles/scss/theme/wood.scss',
  42. 'styles/theme-island': './src/client/styles/scss/theme/island.scss',
  43. // styles for external services
  44. 'styles/style-hackmd': './src/client/styles/hackmd/style.scss',
  45. }, options.entry || {}), // Merge with env dependent settings
  46. output: Object.assign({
  47. path: helpers.root('public'),
  48. publicPath: '/',
  49. filename: '[name].bundle.js',
  50. }, options.output || {}), // Merge with env dependent settings
  51. externals: {
  52. // require("jquery") is external and available
  53. // on the global var jQuery
  54. 'jquery': 'jQuery',
  55. 'emojione': 'emojione',
  56. 'hljs': 'hljs',
  57. },
  58. resolve: {
  59. extensions: ['.js', '.jsx', '.json'],
  60. modules: ((options.resolve && options.resolve.modules) || []).concat([helpers.root('node_modules')]),
  61. alias: {
  62. '@root': helpers.root('/'),
  63. '@commons': helpers.root('src/lib'),
  64. '@client': helpers.root('src/client'),
  65. '@tmp': helpers.root('tmp'),
  66. '@alias/logger': helpers.root('src/lib/service/logger'),
  67. '@alias/locales': helpers.root('resource/locales'),
  68. // replace bunyan
  69. 'bunyan': 'browser-bunyan',
  70. }
  71. },
  72. module: {
  73. rules: options.module.rules.concat([
  74. {
  75. test: /.jsx?$/,
  76. exclude: {
  77. test: helpers.root('node_modules'),
  78. exclude: [ // include as a result
  79. { test: helpers.root('node_modules', 'growi-plugin-') },
  80. helpers.root('node_modules/codemirror/src'),
  81. helpers.root('node_modules/string-width'),
  82. helpers.root('node_modules/is-fullwidth-code-point'), // depends from string-width
  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. { // see https://github.com/abpetkov/switchery/issues/120
  97. test: /switchery\.js$/,
  98. loader: 'imports-loader?module=>false,exports=>false,define=>false,this=>window'
  99. },
  100. {
  101. test: /\.css$/,
  102. use: ['style-loader', 'css-loader'],
  103. exclude: [helpers.root('src/client/styles')]
  104. },
  105. {
  106. test: /\.scss$/,
  107. use: ['style-loader', 'css-loader', 'sass-loader'],
  108. exclude: [helpers.root('src/client/styles')]
  109. },
  110. /*
  111. * File loader for supporting images, for example, in CSS files.
  112. */
  113. {
  114. test: /\.(jpg|png|gif)$/,
  115. use: 'file-loader',
  116. },
  117. /* File loader for supporting fonts, for example, in CSS files.
  118. */
  119. {
  120. test: /\.(eot|woff2?|svg|ttf)([?]?.*)$/,
  121. use: 'null-loader',
  122. }
  123. ])
  124. },
  125. plugins: options.plugins.concat([
  126. new WebpackAssetsManifest({ publicPath: true }),
  127. new webpack.DefinePlugin({
  128. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  129. }),
  130. // ignore
  131. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  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. commons: {
  147. test: /src/,
  148. chunks: 'initial',
  149. name: 'js/commons',
  150. minChunks: 2,
  151. minSize: 1,
  152. priority: 20
  153. },
  154. vendors: {
  155. test: /node_modules/,
  156. chunks: (chunk) => {
  157. // ignore patterns
  158. return chunk.name != null && !chunk.name.match(/legacy-presentation|ie11-polyfill|hackmd-/);
  159. },
  160. name: 'js/vendors',
  161. // minChunks: 2,
  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. };