webpack.common.js 5.3 KB

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