webpack.common.js 6.1 KB

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