webpack.base.babel.js 6.2 KB

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