webpack.common.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /*
  11. * Webpack configuration
  12. *
  13. * See: http://webpack.github.io/docs/configuration.html#cli
  14. */
  15. module.exports = (options) => {
  16. return {
  17. mode: options.mode,
  18. entry: Object.assign({
  19. 'app': './resource/js/app',
  20. 'legacy': './resource/js/legacy/crowi',
  21. 'legacy-form': './resource/js/legacy/crowi-form',
  22. 'legacy-admin': './resource/js/legacy/crowi-admin',
  23. 'legacy-presentation': './resource/js/legacy/crowi-presentation',
  24. 'plugin': './resource/js/plugin',
  25. 'style': './resource/styles/scss/style.scss',
  26. 'style-presentation': './resource/styles/scss/style-presentation.scss',
  27. // themes
  28. 'style-theme-default': './resource/styles/scss/theme/default.scss',
  29. 'style-theme-default-dark': './resource/styles/scss/theme/default-dark.scss',
  30. 'style-theme-nature': './resource/styles/scss/theme/nature.scss',
  31. 'style-theme-mono-blue': './resource/styles/scss/theme/mono-blue.scss',
  32. 'style-theme-future': './resource/styles/scss/theme/future.scss',
  33. 'style-theme-blue-night': './resource/styles/scss/theme/blue-night.scss',
  34. }, options.entry || {}), // Merge with env dependent settings
  35. output: Object.assign({
  36. path: helpers.root('public/js'),
  37. publicPath: '/js/',
  38. filename: '[name]-[hash].js',
  39. chunkFilename: '[id]-[chunkhash].js',
  40. }, options.output || {}), // Merge with env dependent settings
  41. externals: {
  42. // require("jquery") is external and available
  43. // on the global var jQuery
  44. 'jquery': 'jQuery',
  45. 'emojione': 'emojione',
  46. 'hljs': 'hljs',
  47. },
  48. resolve: {
  49. extensions: ['.js', '.json'],
  50. modules: [helpers.root('src'), helpers.root('node_modules')],
  51. alias: {
  52. '@root': helpers.root('/'),
  53. '@alias/logger': helpers.root('lib/service/logger'),
  54. '@alias/locales': helpers.root('lib/locales'),
  55. // replace bunyan
  56. 'bunyan': 'browser-bunyan',
  57. }
  58. },
  59. module: {
  60. rules: options.module.rules.concat([
  61. {
  62. test: /.jsx?$/,
  63. exclude: {
  64. test: helpers.root('node_modules'),
  65. exclude: [ // include as a result
  66. helpers.root('node_modules/string-width'),
  67. helpers.root('node_modules/is-fullwidth-code-point'), // depends from string-width
  68. ]
  69. },
  70. use: [{
  71. loader: 'babel-loader?cacheDirectory'
  72. }]
  73. },
  74. {
  75. test: /locales/,
  76. loader: '@alienfast/i18next-loader',
  77. options: {
  78. basenameAsNamespace: true,
  79. }
  80. },
  81. {
  82. test: /\.css$/,
  83. use: ['style-loader', 'css-loader'],
  84. exclude: [helpers.root('resource/styles/scss')]
  85. },
  86. {
  87. test: /\.scss$/,
  88. use: ['style-loader', 'css-loader', 'sass-loader'],
  89. exclude: [helpers.root('resource/styles/scss')]
  90. },
  91. /*
  92. * File loader for supporting images, for example, in CSS files.
  93. */
  94. {
  95. test: /\.(jpg|png|gif)$/,
  96. use: 'file-loader',
  97. },
  98. /* File loader for supporting fonts, for example, in CSS files.
  99. */
  100. {
  101. test: /\.(eot|woff2?|svg|ttf)([?]?.*)$/,
  102. use: 'file-loader',
  103. }
  104. ])
  105. },
  106. plugins: options.plugins.concat([
  107. new WebpackAssetsManifest({ publicPath: true }),
  108. new webpack.DefinePlugin({
  109. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  110. }),
  111. // new CommonsChunkPlugin({
  112. // name: 'commons',
  113. // chunks: ['app', 'legacy', 'legacy-form', 'legacy-admin'],
  114. // minChunks: module => /node_modules/.test(module.resource),
  115. // }),
  116. // new CommonsChunkPlugin({
  117. // name: 'commons',
  118. // chunks: ['commons', 'legacy-presentation'],
  119. // }),
  120. // new CommonsChunkPlugin({
  121. // name: 'commons',
  122. // chunks: ['commons', 'plugin'],
  123. // }),
  124. // ignore
  125. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  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: 'commons',
  141. minChunks: 2,
  142. minSize: 1,
  143. priority: 20
  144. },
  145. vendors: {
  146. test: /node_modules/,
  147. chunks: (chunk) => {
  148. return chunk.name !== 'legacy-presentation';
  149. },
  150. name: 'vendors',
  151. // minChunks: 2,
  152. minSize: 1,
  153. priority: 10,
  154. enforce: true
  155. }
  156. }
  157. },
  158. minimizer: options.optimization.minimizer || [],
  159. },
  160. performance: options.performance || {},
  161. stats: options.stats || {},
  162. };
  163. };