webpack.common.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @author: Yuki Takei <yuki@weseek.co.jp>
  3. */
  4. const path = require('path');
  5. const webpack = require('webpack');
  6. /*
  7. * Webpack Plugins
  8. */
  9. const WebpackAssetsManifest = require('webpack-assets-manifest');
  10. const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
  11. const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
  12. /*
  13. * Webpack configuration
  14. *
  15. * See: http://webpack.github.io/docs/configuration.html#cli
  16. */
  17. module.exports = (options) => {
  18. return {
  19. mode: options.mode,
  20. entry: Object.assign({
  21. 'js/boot': './src/client/boot',
  22. 'js/app': './src/client/app',
  23. 'js/admin': './src/client/admin',
  24. 'js/nologin': './src/client/nologin',
  25. 'js/legacy': './src/client/legacy/crowi',
  26. 'js/legacy-presentation': './src/client/legacy/crowi-presentation',
  27. 'js/plugin': './src/client/plugin',
  28. 'js/hackmd-agent': './src/client/hackmd-agent',
  29. 'js/hackmd-styles': './src/client/hackmd-styles',
  30. // styles
  31. 'styles/style-app': './src/styles/style-app.scss',
  32. 'styles/style-presentation': './src/styles/style-presentation.scss',
  33. // themes
  34. 'styles/theme-default': './src/styles/theme/default.scss',
  35. 'styles/theme-nature': './src/styles/theme/nature.scss',
  36. 'styles/theme-mono-blue': './src/styles/theme/mono-blue.scss',
  37. 'styles/theme-future': './src/styles/theme/future.scss',
  38. 'styles/theme-kibela': './src/styles/theme/kibela.scss',
  39. 'styles/theme-halloween': './src/styles/theme/halloween.scss',
  40. 'styles/theme-christmas': './src/styles/theme/christmas.scss',
  41. 'styles/theme-wood': './src/styles/theme/wood.scss',
  42. 'styles/theme-island': './src/styles/theme/island.scss',
  43. 'styles/theme-antarctic': './src/styles/theme/antarctic.scss',
  44. 'styles/theme-spring': './src/styles/theme/spring.scss',
  45. 'styles/theme-hufflepuff': './src/styles/theme/hufflepuff.scss',
  46. 'styles/theme-fire-red': './src/styles/theme/fire-red.scss',
  47. 'styles/theme-jade-green': './src/styles/theme/jade-green.scss',
  48. 'styles/theme-blackboard': './src/styles/theme/blackboard.scss',
  49. // styles for external services
  50. 'styles/style-hackmd': './src/styles-hackmd/style.scss',
  51. }, options.entry || {}), // Merge with env dependent settings
  52. output: Object.assign({
  53. path: path.resolve(__dirname, '../public'),
  54. publicPath: '/',
  55. filename: '[name].bundle.js',
  56. }, options.output || {}), // Merge with env dependent settings
  57. externals: {
  58. // require("jquery") is external and available
  59. // on the global var jQuery
  60. jquery: 'jQuery',
  61. emojione: 'emojione',
  62. hljs: 'hljs',
  63. 'dtrace-provider': 'dtrace-provider',
  64. },
  65. resolve: {
  66. extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
  67. plugins: [
  68. new TsconfigPathsPlugin({
  69. configFile: path.resolve(__dirname, '../tsconfig.build.client.json'),
  70. extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
  71. }),
  72. ],
  73. },
  74. node: {
  75. fs: 'empty',
  76. },
  77. module: {
  78. rules: options.module.rules.concat([
  79. {
  80. test: /.(jsx?|tsx?)$/,
  81. exclude: {
  82. test: /node_modules/,
  83. exclude: [ // include as a result
  84. /node_modules\/codemirror/,
  85. ],
  86. },
  87. use: [{
  88. loader: 'ts-loader',
  89. options: {
  90. transpileOnly: true,
  91. configFile: path.resolve(__dirname, '../tsconfig.build.client.json'),
  92. },
  93. }],
  94. },
  95. {
  96. test: /locales/,
  97. loader: '@alienfast/i18next-loader',
  98. options: {
  99. basenameAsNamespace: true,
  100. },
  101. },
  102. /*
  103. * File loader for supporting images, for example, in CSS files.
  104. */
  105. {
  106. test: /\.(jpg|png|gif)$/,
  107. use: 'file-loader',
  108. },
  109. /* File loader for supporting fonts, for example, in CSS files.
  110. */
  111. {
  112. test: /\.(eot|woff2?|svg|ttf)([?]?.*)$/,
  113. use: 'null-loader',
  114. },
  115. ]),
  116. },
  117. plugins: options.plugins.concat([
  118. new WebpackAssetsManifest({ publicPath: true }),
  119. new webpack.DefinePlugin({
  120. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  121. }),
  122. // ignore
  123. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  124. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  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. style_commons: {
  140. test: /\.(sc|sa|c)ss$/,
  141. chunks: (chunk) => {
  142. // ignore patterns
  143. return chunk.name != null && !chunk.name.match(/style-|theme-|legacy-presentation/);
  144. },
  145. name: 'styles/style-commons',
  146. minSize: 1,
  147. priority: 30,
  148. enforce: true,
  149. },
  150. commons: {
  151. test: /(src|resource)[\\/].*\.(js|jsx|json)$/,
  152. chunks: (chunk) => {
  153. // ignore patterns
  154. return chunk.name != null && !chunk.name.match(/boot/);
  155. },
  156. name: 'js/commons',
  157. minChunks: 2,
  158. minSize: 1,
  159. priority: 20,
  160. },
  161. vendors: {
  162. test: /node_modules[\\/].*\.(js|jsx|json)$/,
  163. chunks: (chunk) => {
  164. // ignore patterns
  165. return chunk.name != null && !chunk.name.match(/boot|legacy-presentation|hackmd-/);
  166. },
  167. name: 'js/vendors',
  168. minSize: 1,
  169. priority: 10,
  170. enforce: true,
  171. },
  172. },
  173. },
  174. minimizer: options.optimization.minimizer || [],
  175. },
  176. performance: options.performance || {},
  177. stats: options.stats || {},
  178. };
  179. };