rules.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 ? MiniCssExtractPlugin.loader : require.resolve('style-loader'),
  20. {
  21. loader: require.resolve('css-loader'),
  22. options: cssOptions,
  23. },
  24. {
  25. loader: require.resolve('postcss-loader'),
  26. options: {
  27. sourceMap: !isProduction,
  28. postcssOptions: {
  29. ident: 'postcss',
  30. config: false,
  31. plugins: [
  32. 'postcss-nesting',
  33. 'postcss-flexbugs-fixes',
  34. [
  35. 'postcss-preset-env',
  36. {
  37. autoprefixer: {
  38. flexbox: 'no-2009',
  39. },
  40. stage: 3,
  41. },
  42. ],
  43. ],
  44. },
  45. },
  46. },
  47. preProcessor && {
  48. loader: preProcessor,
  49. },
  50. ].filter(Boolean);
  51. }
  52. const rules = [
  53. {
  54. oneOf: [
  55. {
  56. test: cssRegex,
  57. exclude: cssModuleRegex,
  58. use: getStyleLoaders({
  59. importLoaders: 1,
  60. sourceMap: buildSourceMap,
  61. modules: {
  62. mode: 'icss',
  63. },
  64. }),
  65. sideEffects: true,
  66. },
  67. {
  68. test: cssModuleRegex,
  69. use: getStyleLoaders({
  70. importLoaders: 1,
  71. sourceMap: buildSourceMap,
  72. modules: moduleCssOptions,
  73. }),
  74. },
  75. {
  76. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  77. type: 'asset',
  78. parser: {
  79. dataUrlCondition: {
  80. maxSize: 10 * 1024,
  81. },
  82. },
  83. },
  84. {
  85. test: /\.svg$/,
  86. use: [
  87. {
  88. loader: require.resolve('@svgr/webpack'),
  89. options: {
  90. prettier: false,
  91. svgo: false,
  92. svgoConfig: {
  93. plugins: [{removeViewBox: false}],
  94. },
  95. titleProp: true,
  96. ref: true,
  97. },
  98. },
  99. {
  100. loader: require.resolve('file-loader'),
  101. options: {
  102. name: 'static/assets/[name].[hash].[ext]',
  103. },
  104. },
  105. ],
  106. issuer: {
  107. and: [/\.(ts|tsx|js|jsx|md|mdx)$/],
  108. },
  109. },
  110. {
  111. exclude: [/^$/, /\.(js|jsx|ts|tsx|mjs)$/, /\.html$/, /\.json$/],
  112. type: 'asset/resource',
  113. },
  114. !enableSWC && babel(),
  115. enableSWC && swc(false),
  116. enableSWC && swc(true),
  117. enableSass ? {
  118. test: sassRegex,
  119. exclude: sassModuleRegex,
  120. use: getStyleLoaders(
  121. {
  122. importLoaders: 3,
  123. sourceMap: buildSourceMap,
  124. modules: {
  125. mode: 'icss',
  126. },
  127. },
  128. 'sass-loader',
  129. ),
  130. sideEffects: true,
  131. } : void 0,
  132. enableSass ? {
  133. test: sassModuleRegex,
  134. use: getStyleLoaders(
  135. {
  136. importLoaders: 3,
  137. sourceMap: buildSourceMap,
  138. modules: moduleCssOptions,
  139. },
  140. 'sass-loader',
  141. ),
  142. } : void 0,
  143. ].filter(Boolean),
  144. },
  145. ];
  146. module.exports = rules;