webpack.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin');
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5. const Dotenv = require('dotenv-webpack');
  6. const __DEV__ = process.env.NODE_ENV === 'development';
  7. function sassLoader() {
  8. const common = [
  9. 'css-loader',
  10. {
  11. loader: 'postcss-loader',
  12. options: {
  13. config: {
  14. path: path.join(__dirname, 'src/sass/postcss.config.js')
  15. }
  16. }
  17. },
  18. 'sass-loader',
  19. {
  20. loader: '@epegzz/sass-vars-loader',
  21. options: {
  22. syntax: 'scss',
  23. files: [
  24. path.join(__dirname, 'src/constants/styles.js')
  25. ]
  26. }
  27. }
  28. ];
  29. if (__DEV__) {
  30. return ['style-loader', ...common];
  31. }
  32. return [MiniCssExtractPlugin.loader, ...common];
  33. }
  34. function getPlugins() {
  35. const common = [
  36. new HtmlWebpackPlugin({
  37. hash: true,
  38. template: './src/index.html',
  39. filename: 'index.html'
  40. }),
  41. new webpack.DefinePlugin({
  42. 'process.env': {
  43. NODE_ENV: JSON.stringify(process.env.NODE_ENV)
  44. }
  45. })
  46. ];
  47. if (__DEV__) {
  48. return [
  49. ...common,
  50. new webpack.HotModuleReplacementPlugin(),
  51. new Dotenv()
  52. ];
  53. }
  54. return [
  55. ...common,
  56. new MiniCssExtractPlugin({
  57. filename: '[name].css',
  58. chunkFilename: '[id].css'
  59. })
  60. ];
  61. }
  62. function getEntry() {
  63. const common = ['./src/index.js'];
  64. if (__DEV__) {
  65. return [
  66. 'webpack/hot/only-dev-server',
  67. 'webpack-hot-middleware/client?reload=true',
  68. 'react-hot-loader/patch',
  69. ...common
  70. ];
  71. }
  72. return common;
  73. }
  74. module.exports = {
  75. entry: getEntry(),
  76. devtool: __DEV__
  77. ? 'cheap-module-eval-source-map'
  78. : false,
  79. mode: process.env.NODE_ENV,
  80. output: {
  81. path: path.join(__dirname, 'static'),
  82. publicPath: process.env.PUBLIC_PATH || '/',
  83. filename: '[name].bundle.js?v=[hash]',
  84. globalObject: 'this'
  85. },
  86. module: {
  87. rules: [
  88. {
  89. test: /\.html$/,
  90. use: {
  91. loader: 'html-loader',
  92. options: { minimize: true }
  93. }
  94. },
  95. {
  96. test: /\.js$/,
  97. exclude: /node_modules/,
  98. use: 'babel-loader'
  99. },
  100. {
  101. test: /workers\/(.*)\.js$/,
  102. exclude: /node_modules/,
  103. use: [
  104. {
  105. loader: 'worker-loader',
  106. options: {
  107. name: '[name].worker.js',
  108. inline: true
  109. }
  110. },
  111. 'babel-loader'
  112. ]
  113. },
  114. {
  115. test: /\.scss$/,
  116. exclude: /node_modules/,
  117. use: sassLoader()
  118. },
  119. {
  120. test: /\.css$/,
  121. use: 'css-loader'
  122. },
  123. {
  124. test: /\.(png|jpg)$/,
  125. exclude: [/node_modules/, /favicon\.png/],
  126. use: 'file-loader'
  127. },
  128. {
  129. test: /\.svg$/,
  130. exclude: /node_modules/,
  131. use: 'svg-react-loader'
  132. },
  133. {
  134. test: /favicon\.png/,
  135. use: {
  136. loader: 'file-loader',
  137. options: {
  138. name: 'favicon.ico',
  139. publicPath: '../'
  140. }
  141. }
  142. }
  143. ]
  144. },
  145. resolve: {
  146. alias: {
  147. actions: path.resolve(__dirname, 'src/actions'),
  148. components: path.resolve(__dirname, 'src/components'),
  149. constants: path.resolve(__dirname, 'src/constants'),
  150. containers: path.resolve(__dirname, 'src/containers'),
  151. reducers: path.resolve(__dirname, 'src/reducers'),
  152. sagas: path.resolve(__dirname, 'src/sagas'),
  153. selectors: path.resolve(__dirname, 'src/selectors'),
  154. variables: path.resolve(__dirname, 'src/sass/variables.scss'),
  155. sprite: path.resolve(__dirname, 'src/images/sprite/sprite.scss')
  156. }
  157. },
  158. optimization: {
  159. minimize: !__DEV__,
  160. splitChunks: {
  161. chunks: 'all',
  162. name: 'common'
  163. }
  164. },
  165. plugins: getPlugins()
  166. };