webpack.common.js 5.2 KB

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