rules.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const babel = require('./babel');
  2. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  3. const swc = require('./swc');
  4. const isProduction = process.env.NODE_ENV === 'production';
  5. const enableSWC = process.env.SWT_ENABLE_SWC === 'true';
  6. const useSourceMap = process.env.ENABLE_SOURCE_MAP === 'true';
  7. const buildSourceMap = isProduction ? useSourceMap : true;
  8. const enableSass = process.env.SWT_ENABLE_SASS === 'true';
  9. const moduleCssOptions = {
  10. localIdentName: '[local]-[hash:base64:5]',
  11. exportLocalsConvention: 'dashes',
  12. };
  13. const cssRegex = /\.css$/;
  14. const cssModuleRegex = /\.module\.css$/;
  15. const sassRegex = /\.(scss|sass)$/;
  16. const sassModuleRegex = /\.module\.(scss|sass)$/;
  17. function getStyleLoaders(cssOptions, preProcessor) {
  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. preProcessor && {
  50. loader: preProcessor,
  51. },
  52. ].filter(Boolean);
  53. }
  54. const rules = [
  55. {
  56. oneOf: [
  57. {
  58. test: cssRegex,
  59. exclude: cssModuleRegex,
  60. use: getStyleLoaders({
  61. importLoaders: 1,
  62. sourceMap: buildSourceMap,
  63. modules: {
  64. mode: 'icss',
  65. },
  66. }),
  67. sideEffects: true,
  68. },
  69. {
  70. test: cssModuleRegex,
  71. use: getStyleLoaders({
  72. importLoaders: 1,
  73. sourceMap: buildSourceMap,
  74. modules: moduleCssOptions,
  75. }),
  76. },
  77. {
  78. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  79. type: 'asset',
  80. parser: {
  81. dataUrlCondition: {
  82. maxSize: 10 * 1024,
  83. },
  84. },
  85. },
  86. {
  87. test: /\.svg$/,
  88. use: [
  89. {
  90. loader: require.resolve('@svgr/webpack'),
  91. options: {
  92. prettier: false,
  93. svgo: false,
  94. svgoConfig: {
  95. plugins: [{removeViewBox: false}],
  96. },
  97. titleProp: true,
  98. ref: true,
  99. },
  100. },
  101. {
  102. loader: require.resolve('file-loader'),
  103. options: {
  104. name: 'static/assets/[name].[hash].[ext]',
  105. },
  106. },
  107. ],
  108. issuer: {
  109. and: [/\.(ts|tsx|js|jsx|md|mdx)$/],
  110. },
  111. },
  112. {
  113. exclude: [/^$/, /\.(js|jsx|ts|tsx|mjs)$/, /\.html$/, /\.json$/],
  114. type: 'asset/resource',
  115. },
  116. !enableSWC && babel(),
  117. enableSWC && swc(false),
  118. enableSWC && swc(true),
  119. enableSass
  120. ? {
  121. test: sassRegex,
  122. exclude: sassModuleRegex,
  123. use: getStyleLoaders(
  124. {
  125. importLoaders: 3,
  126. sourceMap: buildSourceMap,
  127. modules: {
  128. mode: 'icss',
  129. },
  130. },
  131. 'sass-loader',
  132. ),
  133. sideEffects: true,
  134. }
  135. : void 0,
  136. enableSass
  137. ? {
  138. test: sassModuleRegex,
  139. use: getStyleLoaders(
  140. {
  141. importLoaders: 3,
  142. sourceMap: buildSourceMap,
  143. modules: moduleCssOptions,
  144. },
  145. 'sass-loader',
  146. ),
  147. }
  148. : void 0,
  149. ].filter(Boolean),
  150. },
  151. ];
  152. module.exports = rules;