webpack.base.babel.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * COMMON WEBPACK CONFIGURATION
  3. */
  4. const os = require('os')
  5. const path = require('path')
  6. const webpack = require('webpack')
  7. const HappyPack = require('happypack')
  8. const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length })
  9. const overrideLessVariables = require('../../app/assets/override/lessVariables')
  10. // Remove this line once the following warning goes away (it was meant for webpack loader authors not users):
  11. // 'DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic,
  12. // see https://github.com/webpack/loader-utils/issues/56 parseQuery() will be replaced with getOptions()
  13. // in the next major version of loader-utils.'
  14. process.noDeprecation = true
  15. module.exports = (options) => ({
  16. mode: options.mode,
  17. entry: options.entry,
  18. devServer: {
  19. proxy: {
  20. // '/api': 'http://davinci.xt.wenhq.top:8083/'
  21. '/api': 'http://thgateway.xt.wenhq.top:8083/taihu-analysis/',
  22. '/taihu-auth': 'http://thgateway.xt.wenhq.top:8083/'
  23. }
  24. },
  25. output: Object.assign(
  26. {
  27. // Compile into js/build.js
  28. path: path.resolve(process.cwd(), 'build'),
  29. publicPath: '/'
  30. },
  31. options.output
  32. ), // Merge with env dependent settings
  33. optimization: options.optimization,
  34. module: {
  35. rules: [
  36. {
  37. test: /\.tsx?$/,
  38. exclude: /node_modules/,
  39. use: 'happypack/loader?id=typescript'
  40. },
  41. {
  42. test: /\.js$/, // Transform all .js files required somewhere with Babel
  43. use: 'happypack/loader?id=js'
  44. },
  45. {
  46. // Do not transform vendor's CSS with CSS-modules
  47. // The point is that they remain in global scope.
  48. // Since we require these CSS files in our JS or CSS files,
  49. // they will be a part of our compilation either way.
  50. // So, no need for ExtractTextPlugin here.
  51. test: /\.css$/,
  52. include: /node_modules|libs/,
  53. use: 'happypack/loader?id=css'
  54. },
  55. {
  56. test: /\.css$/,
  57. include: [/app[\\\/]assets/],
  58. use: 'happypack/loader?id=assets-css'
  59. },
  60. {
  61. test: /\.less$/,
  62. include: /node_modules/,
  63. use: 'happypack/loader?id=less'
  64. },
  65. {
  66. test: /\.less$/,
  67. exclude: /node_modules/,
  68. use: 'happypack/loader?id=assets-less'
  69. },
  70. {
  71. test: /\.(eot|otf|ttf|woff|woff2)$/,
  72. use: 'file-loader'
  73. },
  74. {
  75. test: /\.svg$/,
  76. use: [
  77. {
  78. loader: 'svg-url-loader',
  79. options: {
  80. // Inline files smaller than 10 kB
  81. limit: 10 * 1024,
  82. noquotes: true
  83. }
  84. }
  85. ]
  86. },
  87. {
  88. test: /\.(jpg|png|gif)$/,
  89. use: [
  90. {
  91. loader: 'url-loader',
  92. options: {
  93. // Inline files smaller than 10 kB
  94. limit: 10 * 1024
  95. }
  96. },
  97. // {
  98. // loader: 'image-webpack-loader',
  99. // options: {
  100. // mozjpeg: {
  101. // enabled: false
  102. // // NOTE: mozjpeg is disabled as it causes errors in some Linux environments
  103. // // Try enabling it in your environment by switching the config to:
  104. // // enabled: true,
  105. // // progressive: true,
  106. // },
  107. // // gifsicle: {
  108. // // interlaced: false
  109. // // },
  110. // optipng: {
  111. // optimizationLevel: 7
  112. // },
  113. // pngquant: {
  114. // quality: '65-90',
  115. // speed: 4
  116. // }
  117. // }
  118. // }
  119. ]
  120. },
  121. {
  122. test: /\.html$/,
  123. use: 'html-loader'
  124. },
  125. {
  126. test: /\.(mp4|webm)$/,
  127. use: {
  128. loader: 'url-loader',
  129. options: {
  130. limit: 10000
  131. }
  132. }
  133. }
  134. ]
  135. },
  136. plugins: options.plugins.concat([
  137. // Always expose NODE_ENV to webpack, in order to use `process.env.NODE_ENV`
  138. // inside your code for any environment checks; UglifyJS will automatically
  139. // drop any unreachable code.
  140. new webpack.DefinePlugin({
  141. 'process.env': {
  142. NODE_ENV: JSON.stringify(process.env.NODE_ENV)
  143. }
  144. }),
  145. new webpack.ContextReplacementPlugin(/^\.\/locale$/, (context) => {
  146. if (!/\/moment\//.test(context.context)) return;
  147. Object.assign(context, {
  148. regExp: /^\.\/\w+/,
  149. request: '../../locale', // resolved relatively
  150. });
  151. }),
  152. new HappyPack({
  153. id: 'typescript',
  154. loaders: options.tsLoaders,
  155. threadPool: happyThreadPool,
  156. verbose: true
  157. }),
  158. new HappyPack({
  159. id: 'js',
  160. loaders: ['babel-loader'],
  161. threadPool: happyThreadPool,
  162. verbose: true
  163. }),
  164. new HappyPack({
  165. id: 'css',
  166. loaders: ['style-loader', 'css-loader'],
  167. threadPool: happyThreadPool,
  168. verbose: true
  169. }),
  170. new HappyPack({
  171. id: 'assets-css',
  172. loaders: ['style-loader', 'css-loader', 'postcss-loader'],
  173. threadPool: happyThreadPool,
  174. verbose: true
  175. }),
  176. new HappyPack({
  177. id: 'less',
  178. loaders: [
  179. 'style-loader',
  180. 'css-loader',
  181. {
  182. loader: 'less-loader',
  183. options: {
  184. sourceMap: true,
  185. javascriptEnabled: true,
  186. modifyVars: overrideLessVariables
  187. }
  188. }
  189. ],
  190. threadPool: happyThreadPool,
  191. verbose: true
  192. }),
  193. new HappyPack({
  194. id: 'assets-less',
  195. loaders: [
  196. 'style-loader',
  197. {
  198. loader: 'css-loader',
  199. options: {
  200. modules: true,
  201. importLoaders: 1
  202. }
  203. },
  204. 'postcss-loader',
  205. {
  206. loader: 'less-loader',
  207. options: {
  208. sourceMap: true,
  209. javascriptEnabled: true
  210. }
  211. }
  212. ],
  213. threadPool: happyThreadPool,
  214. verbose: true
  215. }),
  216. ]),
  217. resolve: {
  218. modules: ['node_modules', 'app'],
  219. extensions: ['.js', '.jsx', '.ts', '.tsx', '.react.js'],
  220. mainFields: ['browser', 'jsnext:main', 'main'],
  221. alias: {
  222. 'react-resizable': path.resolve(process.cwd(), 'libs/react-resizable'),
  223. app: path.resolve(process.cwd(), 'app'),
  224. share: path.resolve(process.cwd(), 'share'),
  225. libs: path.resolve(process.cwd(), 'libs'),
  226. assets: path.resolve(process.cwd(), 'app/assets')
  227. // fonts: path.resolve(process.cwd(), 'app/assets/fonts')
  228. }
  229. },
  230. devtool: options.devtool,
  231. target: 'web', // Make web variables accessible to webpack, e.g. window
  232. performance: options.performance || {}
  233. })