webpack.common.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 'app': './resource/js/app',
  21. 'legacy': './resource/js/legacy/crowi',
  22. 'legacy-form': './resource/js/legacy/crowi-form',
  23. 'legacy-admin': './resource/js/legacy/crowi-admin',
  24. 'legacy-presentation': './resource/js/legacy/crowi-presentation',
  25. 'plugin': './resource/js/plugin',
  26. 'style': './resource/styles/scss/style.scss',
  27. 'style-presentation': './resource/styles/scss/style-presentation.scss',
  28. // themes
  29. 'style-theme-default': './resource/styles/scss/theme/default.scss',
  30. 'style-theme-default-dark': './resource/styles/scss/theme/default-dark.scss',
  31. 'style-theme-nature': './resource/styles/scss/theme/nature.scss',
  32. 'style-theme-mono-blue': './resource/styles/scss/theme/mono-blue.scss',
  33. 'style-theme-future': './resource/styles/scss/theme/future.scss',
  34. 'style-theme-blue-night': './resource/styles/scss/theme/blue-night.scss',
  35. }, options.entry || {}), // Merge with env dependent settings
  36. output: Object.assign({
  37. path: helpers.root('public/js'),
  38. publicPath: '/js/',
  39. filename: '[name].bundle.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. // ignore
  112. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  113. new LodashModuleReplacementPlugin,
  114. new webpack.ProvidePlugin({ // refs externals
  115. jQuery: 'jquery',
  116. $: 'jquery',
  117. }),
  118. ]),
  119. devtool: options.devtool,
  120. target: 'web', // Make web variables accessible to webpack, e.g. window
  121. optimization: {
  122. namedModules: true,
  123. splitChunks: {
  124. cacheGroups: {
  125. commons: {
  126. test: /resource/,
  127. chunks: 'initial',
  128. name: 'commons',
  129. minChunks: 2,
  130. minSize: 1,
  131. priority: 20
  132. },
  133. vendors: {
  134. test: /node_modules/,
  135. chunks: (chunk) => {
  136. return chunk.name !== 'legacy-presentation';
  137. },
  138. name: 'vendors',
  139. // minChunks: 2,
  140. minSize: 1,
  141. priority: 10,
  142. enforce: true
  143. }
  144. }
  145. },
  146. minimizer: options.optimization.minimizer || [],
  147. },
  148. performance: options.performance || {},
  149. stats: options.stats || {},
  150. };
  151. };