webpack.common.js 5.2 KB

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