plugins.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const {
  2. outputPath,
  3. publicPath,
  4. srcPath,
  5. rootPath,
  6. } = require('./paths');
  7. const {getEnv} = require('./env');
  8. const {resolve} = require('path');
  9. const ESLintWebpackPlugin = require('eslint-webpack-plugin');
  10. const HtmlWebpackPlugin = require('html-webpack-plugin');
  11. const CopyPlugin = require('copy-webpack-plugin');
  12. const WebpackBar = require('webpackbar');
  13. const {DefinePlugin} = require('webpack');
  14. const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
  15. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  16. const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
  17. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
  18. const pkg = require('../../../package.json');
  19. const isProduction = process.env.NODE_ENV === 'production';
  20. const isDevelopment = !isProduction;
  21. const plugins = [
  22. new WebpackBar(),
  23. new ForkTsCheckerWebpackPlugin({
  24. async: isDevelopment,
  25. typescript: {
  26. configFile: resolve(rootPath, './tsconfig.json'),
  27. },
  28. issue: {
  29. include: [
  30. {file: '../**/src/**/*.{ts,tsx}'},
  31. {file: '**/src/**/*.{ts,tsx}'},
  32. ],
  33. exclude: [
  34. {file: '**/src/setupProxy.*'},
  35. {file: '**/src/setupTests.*'},
  36. ],
  37. },
  38. }),
  39. new ESLintWebpackPlugin({
  40. extensions: ['js', 'mjs', 'jsx', 'ts', 'tsx'],
  41. context: srcPath,
  42. exclude: 'node_modules',
  43. cache: true,
  44. cacheLocation: resolve(
  45. rootPath,
  46. '.temp-cache/.eslintcache',
  47. ),
  48. }),
  49. new HtmlWebpackPlugin(
  50. Object.assign(
  51. {
  52. inject: true,
  53. template: resolve(publicPath, 'index.html'),
  54. title: pkg.name,
  55. },
  56. isProduction
  57. ? {
  58. minify: {
  59. removeComments: true,
  60. collapseWhitespace: true,
  61. removeRedundantAttributes: true,
  62. useShortDoctype: true,
  63. removeEmptyAttributes: true,
  64. removeStyleLinkTypeAttributes: true,
  65. keepClosingSlash: true,
  66. minifyJS: true,
  67. minifyCSS: true,
  68. minifyURLs: true,
  69. },
  70. }
  71. : void 0,
  72. ),
  73. ),
  74. new DefinePlugin({
  75. 'process.env': {
  76. ...getEnv(),
  77. },
  78. }),
  79. new CopyPlugin({
  80. patterns: [
  81. {
  82. from: publicPath,
  83. to: outputPath,
  84. toType: 'dir',
  85. noErrorOnMissing: true,
  86. globOptions: {
  87. ignore: ['**/index.html', '**/pLogo.svg'],
  88. },
  89. info: {
  90. minimized: true,
  91. },
  92. },
  93. ],
  94. }),
  95. ];
  96. const fileName = 'static/css/[name].[contenthash:8].css';
  97. if (isProduction)
  98. plugins.push(
  99. new MiniCssExtractPlugin({
  100. filename: fileName,
  101. chunkFilename: fileName,
  102. }),
  103. new BundleAnalyzerPlugin({
  104. analyzerMode: 'static',
  105. openAnalyzer: false,
  106. reportFilename: resolve(rootPath, 'analyzer/index.html'),
  107. }),
  108. );
  109. else
  110. plugins.push(
  111. new ReactRefreshWebpackPlugin({overlay: false}),
  112. );
  113. module.exports = plugins;