webpack.common.js 6.5 KB

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