webpack.common.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/codemirror/src'),
  79. helpers.root('node_modules/string-width'),
  80. helpers.root('node_modules/is-fullwidth-code-point'), // depends from string-width
  81. ]
  82. },
  83. use: [{
  84. loader: 'babel-loader?cacheDirectory'
  85. }]
  86. },
  87. {
  88. test: /locales/,
  89. loader: '@alienfast/i18next-loader',
  90. options: {
  91. basenameAsNamespace: true,
  92. }
  93. },
  94. { // see https://github.com/abpetkov/switchery/issues/120
  95. test: /switchery\.js$/,
  96. loader: 'imports-loader?module=>false,exports=>false,define=>false,this=>window'
  97. },
  98. {
  99. test: /\.css$/,
  100. use: ['style-loader', 'css-loader'],
  101. exclude: [helpers.root('src/client/styles')]
  102. },
  103. {
  104. test: /\.scss$/,
  105. use: ['style-loader', 'css-loader', 'sass-loader'],
  106. exclude: [helpers.root('src/client/styles')]
  107. },
  108. /*
  109. * File loader for supporting images, for example, in CSS files.
  110. */
  111. {
  112. test: /\.(jpg|png|gif)$/,
  113. use: 'file-loader',
  114. },
  115. /* File loader for supporting fonts, for example, in CSS files.
  116. */
  117. {
  118. test: /\.(eot|woff2?|svg|ttf)([?]?.*)$/,
  119. use: 'null-loader',
  120. }
  121. ])
  122. },
  123. plugins: options.plugins.concat([
  124. new WebpackAssetsManifest({ publicPath: true }),
  125. new webpack.DefinePlugin({
  126. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  127. }),
  128. // ignore
  129. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  130. new LodashModuleReplacementPlugin({
  131. flattening: true
  132. }),
  133. new webpack.ProvidePlugin({ // refs externals
  134. jQuery: 'jquery',
  135. $: 'jquery',
  136. }),
  137. ]),
  138. devtool: options.devtool,
  139. target: 'web', // Make web variables accessible to webpack, e.g. window
  140. optimization: {
  141. namedModules: true,
  142. splitChunks: {
  143. cacheGroups: {
  144. commons: {
  145. test: /src/,
  146. chunks: 'initial',
  147. name: 'js/commons',
  148. minChunks: 2,
  149. minSize: 1,
  150. priority: 20
  151. },
  152. vendors: {
  153. test: /node_modules/,
  154. chunks: (chunk) => {
  155. // ignore patterns
  156. return chunk.name != null && !chunk.name.match(/legacy-presentation|ie11-polyfill|hackmd-/);
  157. },
  158. name: 'js/vendors',
  159. // minChunks: 2,
  160. minSize: 1,
  161. priority: 10,
  162. enforce: true
  163. }
  164. }
  165. },
  166. minimizer: options.optimization.minimizer || [],
  167. },
  168. performance: options.performance || {},
  169. stats: options.stats || {},
  170. };
  171. };