plugins.ts 3.0 KB

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