webpack.common.js 5.7 KB

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