plugins.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. mode: 'write-references',
  28. tsBuildInfoFile: resolve(rootPath, '.temp-cache/tsconfig.tsbuildinfo'),
  29. diagnosticOptions: {
  30. syntactic: true,
  31. },
  32. },
  33. issue: {
  34. include: [
  35. {file: '../**/src/**/*.{ts,tsx}'},
  36. {file: '**/src/**/*.{ts,tsx}'},
  37. ],
  38. exclude: [
  39. {file: '**/src/setupProxy.*'},
  40. {file: '**/src/setupTests.*'},
  41. ],
  42. },
  43. }),
  44. new ESLintWebpackPlugin({
  45. extensions: ['js', 'mjs', 'jsx', 'ts', 'tsx'],
  46. context: srcPath,
  47. exclude: 'node_modules',
  48. cache: true,
  49. cacheLocation: resolve(
  50. rootPath,
  51. '.temp-cache/.eslintcache',
  52. ),
  53. }),
  54. new HtmlWebpackPlugin(
  55. Object.assign(
  56. {
  57. inject: true,
  58. template: resolve(publicPath, 'index.html'),
  59. title: pkg.name,
  60. },
  61. isProduction
  62. ? {
  63. minify: {
  64. removeComments: true,
  65. collapseWhitespace: true,
  66. removeRedundantAttributes: true,
  67. useShortDoctype: true,
  68. removeEmptyAttributes: true,
  69. removeStyleLinkTypeAttributes: true,
  70. keepClosingSlash: true,
  71. minifyJS: true,
  72. minifyCSS: true,
  73. minifyURLs: true,
  74. },
  75. }
  76. : void 0,
  77. ),
  78. ),
  79. new DefinePlugin({
  80. 'process.env': Object.assign(
  81. {},
  82. {
  83. ...getEnv(),
  84. },
  85. process.env.IS_E2E ? {IS_E2E: 'true'} : void 0,
  86. ),
  87. }),
  88. new CopyPlugin({
  89. patterns: [
  90. {
  91. from: publicPath,
  92. to: outputPath,
  93. toType: 'dir',
  94. noErrorOnMissing: true,
  95. globOptions: {
  96. ignore: ['**/index.html', '**/pLogo.svg'],
  97. },
  98. info: {
  99. minimized: true,
  100. },
  101. },
  102. ],
  103. }),
  104. ];
  105. const fileName = 'static/css/[name].[contenthash:8].css';
  106. if (isProduction)
  107. plugins.push(
  108. new MiniCssExtractPlugin({
  109. filename: fileName,
  110. chunkFilename: fileName,
  111. }),
  112. new BundleAnalyzerPlugin({
  113. analyzerMode: 'static',
  114. openAnalyzer: false,
  115. reportFilename: resolve(rootPath, 'analyzer/index.html'),
  116. }),
  117. );
  118. else
  119. plugins.push(
  120. new ReactRefreshWebpackPlugin({overlay: false}),
  121. );
  122. module.exports = plugins;