webpack.base.babel.js 6.4 KB

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