plugins.js 3.1 KB

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