webpack.common.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. '@tmp': helpers.root('tmp'),
  65. '@alias/logger': helpers.root('src/lib/service/logger'),
  66. '@alias/locales': helpers.root('resource/locales'),
  67. // replace bunyan
  68. 'bunyan': 'browser-bunyan',
  69. }
  70. },
  71. module: {
  72. rules: options.module.rules.concat([
  73. {
  74. test: /.jsx?$/,
  75. exclude: {
  76. test: helpers.root('node_modules'),
  77. exclude: [ // include as a result
  78. { test: helpers.root('node_modules', 'growi-plugin-') },
  79. helpers.root('node_modules/codemirror/src'),
  80. helpers.root('node_modules/string-width'),
  81. helpers.root('node_modules/is-fullwidth-code-point'), // depends from string-width
  82. ]
  83. },
  84. use: [{
  85. loader: 'babel-loader?cacheDirectory'
  86. }]
  87. },
  88. {
  89. test: /locales/,
  90. loader: '@alienfast/i18next-loader',
  91. options: {
  92. basenameAsNamespace: true,
  93. }
  94. },
  95. { // see https://github.com/abpetkov/switchery/issues/120
  96. test: /switchery\.js$/,
  97. loader: 'imports-loader?module=>false,exports=>false,define=>false,this=>window'
  98. },
  99. {
  100. test: /\.css$/,
  101. use: ['style-loader', 'css-loader'],
  102. exclude: [helpers.root('src/client/styles')]
  103. },
  104. {
  105. test: /\.scss$/,
  106. use: ['style-loader', 'css-loader', 'sass-loader'],
  107. exclude: [helpers.root('src/client/styles')]
  108. },
  109. /*
  110. * File loader for supporting images, for example, in CSS files.
  111. */
  112. {
  113. test: /\.(jpg|png|gif)$/,
  114. use: 'file-loader',
  115. },
  116. /* File loader for supporting fonts, for example, in CSS files.
  117. */
  118. {
  119. test: /\.(eot|woff2?|svg|ttf)([?]?.*)$/,
  120. use: 'null-loader',
  121. }
  122. ])
  123. },
  124. plugins: options.plugins.concat([
  125. new WebpackAssetsManifest({ publicPath: true }),
  126. new webpack.DefinePlugin({
  127. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  128. }),
  129. // ignore
  130. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  131. new LodashModuleReplacementPlugin({
  132. flattening: true
  133. }),
  134. new webpack.ProvidePlugin({ // refs externals
  135. jQuery: 'jquery',
  136. $: 'jquery',
  137. }),
  138. ]),
  139. devtool: options.devtool,
  140. target: 'web', // Make web variables accessible to webpack, e.g. window
  141. optimization: {
  142. namedModules: true,
  143. splitChunks: {
  144. cacheGroups: {
  145. commons: {
  146. test: /src/,
  147. chunks: 'initial',
  148. name: 'js/commons',
  149. minChunks: 2,
  150. minSize: 1,
  151. priority: 20
  152. },
  153. vendors: {
  154. test: /node_modules/,
  155. chunks: (chunk) => {
  156. // ignore patterns
  157. return chunk.name != null && !chunk.name.match(/legacy-presentation|ie11-polyfill|hackmd-/);
  158. },
  159. name: 'js/vendors',
  160. // minChunks: 2,
  161. minSize: 1,
  162. priority: 10,
  163. enforce: true
  164. }
  165. }
  166. },
  167. minimizer: options.optimization.minimizer || [],
  168. },
  169. performance: options.performance || {},
  170. stats: options.stats || {},
  171. };
  172. };