webpack.common.js 6.4 KB

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