rules.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import babel from './babel.ts';
  2. import MiniCssExtractPlugin from 'mini-css-extract-plugin';
  3. import swc from './swc.ts';
  4. import {require} from './paths.ts';
  5. import {SWTEnv} from './env.ts';
  6. const isProduction = process.env.NODE_ENV === 'production';
  7. const enableSWC = (process.env as unknown as SWTEnv).ENABLE_SWC === 'true';
  8. const useSourceMap
  9. = (process.env as unknown as SWTEnv).ENABLE_SOURCE_MAP === 'true';
  10. const buildSourceMap = isProduction ? useSourceMap : true;
  11. const moduleCssOptions = {
  12. localIdentName: '[local]-[hash:base64:5]',
  13. exportLocalsConvention: 'dashes',
  14. };
  15. const cssRegex = /\.css$/;
  16. const cssModuleRegex = /\.module\.css$/;
  17. function getStyleLoaders(cssOptions: Record<string, unknown>) {
  18. return [
  19. isProduction
  20. ? MiniCssExtractPlugin.loader
  21. : require.resolve('style-loader'),
  22. {
  23. loader: require.resolve('css-loader'),
  24. options: cssOptions,
  25. },
  26. {
  27. loader: require.resolve('postcss-loader'),
  28. options: {
  29. sourceMap: !isProduction,
  30. postcssOptions: {
  31. ident: 'postcss',
  32. config: false,
  33. plugins: [
  34. 'postcss-nesting',
  35. 'postcss-flexbugs-fixes',
  36. [
  37. 'postcss-preset-env',
  38. {
  39. autoprefixer: {
  40. flexbox: 'no-2009',
  41. },
  42. stage: 3,
  43. },
  44. ],
  45. ],
  46. },
  47. },
  48. },
  49. ].filter(Boolean);
  50. }
  51. const rules = [
  52. {
  53. oneOf: [
  54. {
  55. test: cssRegex,
  56. exclude: cssModuleRegex,
  57. use: getStyleLoaders({
  58. importLoaders: 1,
  59. sourceMap: buildSourceMap,
  60. modules: {
  61. mode: 'icss',
  62. },
  63. }),
  64. sideEffects: true,
  65. },
  66. {
  67. test: cssModuleRegex,
  68. use: getStyleLoaders({
  69. importLoaders: 1,
  70. sourceMap: buildSourceMap,
  71. modules: moduleCssOptions,
  72. }),
  73. },
  74. {
  75. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.svg$/],
  76. type: 'asset',
  77. parser: {
  78. dataUrlCondition: {
  79. maxSize: 10 * 1024,
  80. },
  81. },
  82. },
  83. {
  84. exclude: [/^$/, /\.(js|jsx|ts|tsx|mjs)$/, /\.html$/, /\.json$/],
  85. type: 'asset/resource',
  86. },
  87. !enableSWC && babel(),
  88. enableSWC && swc(false),
  89. enableSWC && swc(true),
  90. ].filter(Boolean),
  91. },
  92. ];
  93. export default rules;