rules.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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: /\.ldicons\.js$/,
  56. type: 'asset/resource',
  57. },
  58. {
  59. test: cssRegex,
  60. exclude: cssModuleRegex,
  61. use: getStyleLoaders({
  62. importLoaders: 1,
  63. sourceMap: buildSourceMap,
  64. modules: {
  65. mode: 'icss',
  66. },
  67. }),
  68. sideEffects: true,
  69. },
  70. {
  71. test: cssModuleRegex,
  72. use: getStyleLoaders({
  73. importLoaders: 1,
  74. sourceMap: buildSourceMap,
  75. modules: moduleCssOptions,
  76. }),
  77. },
  78. {
  79. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.svg$/],
  80. type: 'asset',
  81. parser: {
  82. dataUrlCondition: {
  83. maxSize: 10 * 1024,
  84. },
  85. },
  86. },
  87. {
  88. exclude: [/^$/, /\.(js|jsx|ts|tsx|mjs)$/, /\.html$/, /\.json$/],
  89. type: 'asset/resource',
  90. },
  91. !enableSWC && babel(),
  92. enableSWC && swc(false),
  93. enableSWC && swc(true),
  94. ].filter(Boolean),
  95. },
  96. ];
  97. export default rules;