webpack.common.js 5.0 KB

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