plugins.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const webpack = require('webpack');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  4. const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
  5. //const WorkboxPlugin = require('workbox-webpack-plugin');
  6. const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
  7. const { TypedCssModulesPlugin } = require('typed-css-modules-webpack-plugin');
  8. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
  9. const CopyWebpackPlugin = require('copy-webpack-plugin');
  10. const { compilerHooks } = require('./custom-plugins');
  11. //const constants = require('./constants');
  12. const config = require('./config');
  13. const { resolve, assetsPath } = require('./utils');
  14. //const env = require('./env.json');
  15. // 加载.env.* 环境变量
  16. const env = require('./parseEnv');
  17. const basePlugins = [
  18. new MomentLocalesPlugin({
  19. localesToKeep: ['es-us', 'zh-cn'],
  20. }),
  21. new webpack.DefinePlugin({ 'process.env': env }),
  22. new TypedCssModulesPlugin({
  23. globPattern: 'src/!(styles)/**/*.scss',
  24. }),
  25. new ForkTsCheckerWebpackPlugin({
  26. typescript: { configFile: resolve('tsconfig.json') },
  27. eslint: { enabled: true, files: resolve('src/**/*.{ts,tsx}') },
  28. }),
  29. ];
  30. const devPlugins = [
  31. new webpack.HotModuleReplacementPlugin(),
  32. new HtmlWebpackPlugin({
  33. filename: 'index.html',
  34. template: 'public/index.html',
  35. inject: true,
  36. }),
  37. new CaseSensitivePathsPlugin(),
  38. ...compilerHooks,
  39. ];
  40. const prodPlugins = [
  41. new HtmlWebpackPlugin({
  42. filename: config.index,
  43. template: 'public/index.html',
  44. inject: true,
  45. minify: {
  46. removeComments: true,
  47. collapseWhitespace: true,
  48. // more options:
  49. // https://github.com/kangax/html-minifier#options-quick-reference
  50. },
  51. }),
  52. new MiniCssExtractPlugin({
  53. // Options similar to the same options in webpackOptions.output
  54. // both options are optional
  55. filename: assetsPath('css/[name].[contenthash].css'),
  56. chunkFilename: assetsPath('css/[name].[id].[contenthash].css'),
  57. ignoreOrder: true,
  58. }),
  59. new CopyWebpackPlugin({
  60. patterns: [
  61. {
  62. from: 'public',
  63. filter: (resourcePath) => {
  64. if (resourcePath.includes('index.html')) {
  65. return false;
  66. }
  67. return true;
  68. },
  69. to: '',
  70. },
  71. ],
  72. }),
  73. // new WorkboxPlugin.GenerateSW({
  74. // cacheId: 'ts-react-webpack',
  75. // clientsClaim: true,
  76. // skipWaiting: true,
  77. // offlineGoogleAnalytics: false,
  78. // inlineWorkboxRun·time: true,
  79. // // precache ignore
  80. // exclude: [/index\.html$/, /\.map$/],
  81. // // dynamic update
  82. // runtimeCaching: [
  83. // {
  84. // urlPattern: /this\\.is\\.a\\.regex/,
  85. // handler: 'NetworkFirst',
  86. // },
  87. // {
  88. // urlPattern: /this\\.is\\.a\\.regex/,
  89. // handler: 'StaleWhileRevalidate',
  90. // },
  91. // ],
  92. // }),
  93. ];
  94. if (config.bundleAnalyzerReport) {
  95. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  96. prodPlugins.push(new BundleAnalyzerPlugin());
  97. }
  98. module.exports = basePlugins.concat(env.NODE_ENV === 'development' ? devPlugins : prodPlugins);