webpack.config.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 Dotenv()
  51. ];
  52. }
  53. return [
  54. ...common,
  55. new MiniCssExtractPlugin({
  56. filename: '[name].css',
  57. chunkFilename: '[id].css'
  58. })
  59. ];
  60. }
  61. function getEntry() {
  62. const common = ['./src/index.js'];
  63. if (__DEV__) {
  64. return [
  65. 'react-hot-loader/patch',
  66. ...common
  67. ];
  68. }
  69. return common;
  70. }
  71. module.exports = {
  72. entry: getEntry(),
  73. devtool: __DEV__
  74. ? 'cheap-module-eval-source-map'
  75. : false,
  76. mode: process.env.NODE_ENV,
  77. output: {
  78. path: path.join(__dirname, 'static'),
  79. publicPath: process.env.PUBLIC_PATH || '/',
  80. filename: '[name].bundle.js?v=[hash]',
  81. globalObject: 'this'
  82. },
  83. module: {
  84. rules: [
  85. {
  86. test: /\.html$/,
  87. use: {
  88. loader: 'html-loader',
  89. options: { minimize: true }
  90. }
  91. },
  92. {
  93. test: /\.js$/,
  94. exclude: /node_modules/,
  95. use: 'babel-loader'
  96. },
  97. {
  98. test: /workers\/(.*)\.js$/,
  99. exclude: /node_modules/,
  100. use: [
  101. {
  102. loader: 'worker-loader',
  103. options: {
  104. name: '[name].worker.js',
  105. inline: true
  106. }
  107. },
  108. 'babel-loader'
  109. ]
  110. },
  111. {
  112. test: /\.scss$/,
  113. exclude: /node_modules/,
  114. use: sassLoader()
  115. },
  116. {
  117. test: /\.css$/,
  118. use: 'css-loader'
  119. },
  120. {
  121. test: /\.(png|jpg)$/,
  122. exclude: [/node_modules/, /favicon\.png/],
  123. use: 'file-loader'
  124. },
  125. {
  126. test: /\.svg$/,
  127. exclude: /node_modules/,
  128. use: 'svg-react-loader'
  129. },
  130. {
  131. test: /favicon\.png/,
  132. use: {
  133. loader: 'file-loader',
  134. options: {
  135. name: 'favicon.ico',
  136. publicPath: '../'
  137. }
  138. }
  139. }
  140. ]
  141. },
  142. resolve: {
  143. alias: {
  144. actions: path.resolve(__dirname, 'src/actions'),
  145. components: path.resolve(__dirname, 'src/components'),
  146. constants: path.resolve(__dirname, 'src/constants'),
  147. containers: path.resolve(__dirname, 'src/containers'),
  148. reducers: path.resolve(__dirname, 'src/reducers'),
  149. sprite: path.resolve(__dirname, 'src/images/sprite/sprite.scss')
  150. }
  151. },
  152. optimization: {
  153. minimize: !__DEV__,
  154. splitChunks: {
  155. chunks: 'all',
  156. name: 'common'
  157. }
  158. },
  159. plugins: getPlugins()
  160. };