webpack.common.js 6.1 KB

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