webpack.common.js 5.9 KB

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