webpack.common.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. 'js/agent-for-hackmd': './resource/js/agent-for-hackmd',
  28. // styles
  29. 'styles/style': './resource/styles/scss/style.scss',
  30. 'styles/style-presentation': './resource/styles/scss/style-presentation.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. // styles for external services
  39. 'styles/style-hackmd': './resource/styles/hackmd/style.scss',
  40. }, options.entry || {}), // Merge with env dependent settings
  41. output: Object.assign({
  42. path: helpers.root('public'),
  43. publicPath: '/',
  44. filename: '[name].bundle.js',
  45. }, options.output || {}), // Merge with env dependent settings
  46. externals: {
  47. // require("jquery") is external and available
  48. // on the global var jQuery
  49. 'jquery': 'jQuery',
  50. 'emojione': 'emojione',
  51. 'hljs': 'hljs',
  52. },
  53. resolve: {
  54. extensions: ['.js', '.jsx', '.json'],
  55. modules: [helpers.root('src'), helpers.root('node_modules')],
  56. alias: {
  57. '@root': helpers.root('/'),
  58. '@alias/logger': helpers.root('lib/service/logger'),
  59. '@alias/locales': helpers.root('lib/locales'),
  60. // replace bunyan
  61. 'bunyan': 'browser-bunyan',
  62. }
  63. },
  64. module: {
  65. rules: options.module.rules.concat([
  66. {
  67. test: /.jsx?$/,
  68. exclude: {
  69. test: helpers.root('node_modules'),
  70. exclude: [ // include as a result
  71. helpers.root('node_modules/string-width'),
  72. helpers.root('node_modules/is-fullwidth-code-point'), // depends from string-width
  73. ]
  74. },
  75. use: [{
  76. loader: 'babel-loader?cacheDirectory'
  77. }]
  78. },
  79. {
  80. test: /locales/,
  81. loader: '@alienfast/i18next-loader',
  82. options: {
  83. basenameAsNamespace: true,
  84. }
  85. },
  86. { // see https://github.com/abpetkov/switchery/issues/120
  87. test: /switchery\.js$/,
  88. loader: 'imports-loader?module=>false,exports=>false,define=>false,this=>window'
  89. },
  90. {
  91. test: /\.css$/,
  92. use: ['style-loader', 'css-loader'],
  93. exclude: [helpers.root('resource/styles')]
  94. },
  95. {
  96. test: /\.scss$/,
  97. use: ['style-loader', 'css-loader', 'sass-loader'],
  98. exclude: [helpers.root('resource/styles')]
  99. },
  100. /*
  101. * File loader for supporting images, for example, in CSS files.
  102. */
  103. {
  104. test: /\.(jpg|png|gif)$/,
  105. use: 'file-loader',
  106. },
  107. /* File loader for supporting fonts, for example, in CSS files.
  108. */
  109. {
  110. test: /\.(eot|woff2?|svg|ttf)([?]?.*)$/,
  111. use: 'null-loader',
  112. }
  113. ])
  114. },
  115. plugins: options.plugins.concat([
  116. new WebpackAssetsManifest({ publicPath: true }),
  117. new webpack.DefinePlugin({
  118. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  119. }),
  120. // ignore
  121. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  122. new LodashModuleReplacementPlugin({
  123. flattening: true
  124. }),
  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: 'js/commons',
  140. minChunks: 2,
  141. minSize: 1,
  142. priority: 20
  143. },
  144. vendors: {
  145. test: /node_modules/,
  146. chunks: (chunk) => {
  147. // ignore patterns
  148. return chunk.name != null && !chunk.name.match(/legacy-presentation|ie11-polyfill|agent-for-hackmd/);
  149. },
  150. name: 'js/vendors',
  151. // minChunks: 2,
  152. minSize: 1,
  153. priority: 10,
  154. enforce: true
  155. }
  156. }
  157. },
  158. minimizer: options.optimization.minimizer || [],
  159. },
  160. performance: options.performance || {},
  161. stats: options.stats || {},
  162. };
  163. };