webpack.common.js 5.1 KB

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