webpack.common.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /*
  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/boot': './src/client/boot',
  21. 'js/app': './src/client/app',
  22. 'js/admin': './src/client/admin',
  23. 'js/nologin': './src/client/nologin',
  24. 'js/legacy': './src/client/legacy/crowi',
  25. 'js/legacy-presentation': './src/client/legacy/crowi-presentation',
  26. 'js/plugin': './src/client/plugin',
  27. 'js/ie11-polyfill': './src/client/ie11-polyfill',
  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 for external services
  47. 'styles/style-hackmd': './src/styles-hackmd/style.scss',
  48. }, options.entry || {}), // Merge with env dependent settings
  49. output: Object.assign({
  50. path: path.resolve(__dirname, '../public'),
  51. publicPath: '/',
  52. filename: '[name].bundle.js',
  53. }, options.output || {}), // Merge with env dependent settings
  54. externals: {
  55. // require("jquery") is external and available
  56. // on the global var jQuery
  57. jquery: 'jQuery',
  58. emojione: 'emojione',
  59. hljs: 'hljs',
  60. },
  61. resolve: {
  62. extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
  63. // modules: ((options.resolve && options.resolve.modules) || []).concat([path.resolve(__dirname, 'node_modules')]),
  64. alias: {
  65. '~': path.resolve(__dirname, '../src'), // src
  66. '^': path.resolve(__dirname, '../'), // project root
  67. '@root': path.resolve(__dirname, '../'),
  68. '@commons': path.resolve(__dirname, '../src/lib'),
  69. '@client': path.resolve(__dirname, '../src/client'),
  70. '@alias/logger': path.resolve(__dirname, '../src/utils/logger'),
  71. },
  72. },
  73. node: {
  74. fs: 'empty',
  75. },
  76. module: {
  77. rules: options.module.rules.concat([
  78. {
  79. test: /.(jsx?|tsx?)$/,
  80. exclude: {
  81. test: path.resolve(__dirname, '../node_modules'),
  82. exclude: [ // include as a result
  83. { test: path.resolve(__dirname, '../node_modules/growi-plugin-') },
  84. path.resolve(__dirname, '../node_modules/growi-commons'),
  85. path.resolve(__dirname, '../node_modules/codemirror/src'),
  86. ],
  87. },
  88. use: [{
  89. loader: 'ts-loader',
  90. options: {
  91. transpileOnly: true,
  92. configFile: path.resolve(__dirname, '../tsconfig.build.json'),
  93. },
  94. }],
  95. },
  96. {
  97. test: /locales/,
  98. loader: '@alienfast/i18next-loader',
  99. options: {
  100. basenameAsNamespace: true,
  101. },
  102. },
  103. /*
  104. * File loader for supporting images, for example, in CSS files.
  105. */
  106. {
  107. test: /\.(jpg|png|gif)$/,
  108. use: 'file-loader',
  109. },
  110. /* File loader for supporting fonts, for example, in CSS files.
  111. */
  112. {
  113. test: /\.(eot|woff2?|svg|ttf)([?]?.*)$/,
  114. use: 'null-loader',
  115. },
  116. ]),
  117. },
  118. plugins: options.plugins.concat([
  119. new WebpackAssetsManifest({ publicPath: true }),
  120. new webpack.DefinePlugin({
  121. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  122. }),
  123. // ignore
  124. new webpack.IgnorePlugin(/^\.\/lib\/deflate\.js/, /markdown-it-plantuml/),
  125. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  126. new LodashModuleReplacementPlugin({
  127. flattening: true,
  128. }),
  129. new webpack.ProvidePlugin({ // refs externals
  130. jQuery: 'jquery',
  131. $: 'jquery',
  132. }),
  133. ]),
  134. devtool: options.devtool,
  135. target: 'web', // Make web variables accessible to webpack, e.g. window
  136. optimization: {
  137. namedModules: true,
  138. splitChunks: {
  139. cacheGroups: {
  140. style_commons: {
  141. test: /\.(sc|sa|c)ss$/,
  142. chunks: (chunk) => {
  143. // ignore patterns
  144. return chunk.name != null && !chunk.name.match(/style-|theme-|legacy-presentation/);
  145. },
  146. name: 'styles/style-commons',
  147. minSize: 1,
  148. priority: 30,
  149. enforce: true,
  150. },
  151. commons: {
  152. test: /(src|resource)[\\/].*\.(js|jsx|json)$/,
  153. chunks: (chunk) => {
  154. // ignore patterns
  155. return chunk.name != null && !chunk.name.match(/boot/);
  156. },
  157. name: 'js/commons',
  158. minChunks: 2,
  159. minSize: 1,
  160. priority: 20,
  161. },
  162. vendors: {
  163. test: /node_modules[\\/].*\.(js|jsx|json)$/,
  164. chunks: (chunk) => {
  165. // ignore patterns
  166. return chunk.name != null && !chunk.name.match(/boot|legacy-presentation|ie11-polyfill|hackmd-/);
  167. },
  168. name: 'js/vendors',
  169. minSize: 1,
  170. priority: 10,
  171. enforce: true,
  172. },
  173. },
  174. },
  175. minimizer: options.optimization.minimizer || [],
  176. },
  177. performance: options.performance || {},
  178. stats: options.stats || {},
  179. };
  180. };