webpack.common.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @author: Yuki Takei <yuki@weseek.co.jp>
  3. */
  4. const webpack = require('webpack');
  5. const helpers = require('./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': './resource/js/app',
  21. 'js/legacy': './resource/js/legacy/crowi',
  22. 'js/legacy-form': './resource/js/legacy/crowi-form',
  23. 'js/legacy-admin': './resource/js/legacy/crowi-admin',
  24. 'js/legacy-presentation': './resource/js/legacy/crowi-presentation',
  25. 'js/plugin': './resource/js/plugin',
  26. 'js/ie11-polyfill': './resource/js/ie11-polyfill',
  27. 'js/hackmd-agent': './resource/js/hackmd-agent',
  28. 'js/hackmd-styles': './resource/js/hackmd-styles',
  29. // styles
  30. 'styles/style': './resource/styles/scss/style.scss',
  31. 'styles/style-presentation': './resource/styles/scss/style-presentation.scss',
  32. // themes
  33. 'styles/theme-default': './resource/styles/scss/theme/default.scss',
  34. 'styles/theme-default-dark': './resource/styles/scss/theme/default-dark.scss',
  35. 'styles/theme-nature': './resource/styles/scss/theme/nature.scss',
  36. 'styles/theme-mono-blue': './resource/styles/scss/theme/mono-blue.scss',
  37. 'styles/theme-future': './resource/styles/scss/theme/future.scss',
  38. 'styles/theme-blue-night': './resource/styles/scss/theme/blue-night.scss',
  39. // styles for external services
  40. 'styles/style-hackmd': './resource/styles/hackmd/style.scss',
  41. }, options.entry || {}), // Merge with env dependent settings
  42. output: Object.assign({
  43. path: helpers.root('public'),
  44. publicPath: '/',
  45. filename: '[name].bundle.js',
  46. }, options.output || {}), // Merge with env dependent settings
  47. externals: {
  48. // require("jquery") is external and available
  49. // on the global var jQuery
  50. 'jquery': 'jQuery',
  51. 'emojione': 'emojione',
  52. 'hljs': 'hljs',
  53. },
  54. resolve: {
  55. extensions: ['.js', '.jsx', '.json'],
  56. modules: [helpers.root('src'), helpers.root('node_modules')],
  57. alias: {
  58. '@root': helpers.root('/'),
  59. '@alias/logger': helpers.root('lib/service/logger'),
  60. '@alias/locales': helpers.root('lib/locales'),
  61. // replace bunyan
  62. 'bunyan': 'browser-bunyan',
  63. }
  64. },
  65. module: {
  66. rules: options.module.rules.concat([
  67. {
  68. test: /.jsx?$/,
  69. exclude: {
  70. test: helpers.root('node_modules'),
  71. exclude: [ // include as a result
  72. helpers.root('node_modules/string-width'),
  73. helpers.root('node_modules/is-fullwidth-code-point'), // depends from string-width
  74. ]
  75. },
  76. use: [{
  77. loader: 'babel-loader?cacheDirectory'
  78. }]
  79. },
  80. {
  81. test: /locales/,
  82. loader: '@alienfast/i18next-loader',
  83. options: {
  84. basenameAsNamespace: true,
  85. }
  86. },
  87. { // see https://github.com/abpetkov/switchery/issues/120
  88. test: /switchery\.js$/,
  89. loader: 'imports-loader?module=>false,exports=>false,define=>false,this=>window'
  90. },
  91. {
  92. test: /\.css$/,
  93. use: ['style-loader', 'css-loader'],
  94. exclude: [helpers.root('resource/styles')]
  95. },
  96. {
  97. test: /\.scss$/,
  98. use: ['style-loader', 'css-loader', 'sass-loader'],
  99. exclude: [helpers.root('resource/styles')]
  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 LodashModuleReplacementPlugin({
  124. flattening: true
  125. }),
  126. new webpack.ProvidePlugin({ // refs externals
  127. jQuery: 'jquery',
  128. $: 'jquery',
  129. }),
  130. ]),
  131. devtool: options.devtool,
  132. target: 'web', // Make web variables accessible to webpack, e.g. window
  133. optimization: {
  134. namedModules: true,
  135. splitChunks: {
  136. cacheGroups: {
  137. commons: {
  138. test: /resource/,
  139. chunks: 'initial',
  140. name: 'js/commons',
  141. minChunks: 2,
  142. minSize: 1,
  143. priority: 20
  144. },
  145. vendors: {
  146. test: /node_modules/,
  147. chunks: (chunk) => {
  148. // ignore patterns
  149. return chunk.name != null && !chunk.name.match(/legacy-presentation|ie11-polyfill|hackmd-/);
  150. },
  151. name: 'js/vendors',
  152. // minChunks: 2,
  153. minSize: 1,
  154. priority: 10,
  155. enforce: true
  156. }
  157. }
  158. },
  159. minimizer: options.optimization.minimizer || [],
  160. },
  161. performance: options.performance || {},
  162. stats: options.stats || {},
  163. };
  164. };