styleRules.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  2. const config = require('./../config');
  3. const { resolve } = require('./../utils');
  4. const theme = require('./../../theme');
  5. const { cacheLoader } = require('./loaders');
  6. const cssLoader = (modules) => ({
  7. loader: 'css-loader',
  8. options: {
  9. modules: modules
  10. ? {
  11. mode: 'local',
  12. localIdentName: '[local]--[contenthash:base64:8]',
  13. }
  14. : false,
  15. },
  16. });
  17. const sassLoader = {
  18. loader: 'sass-loader',
  19. options: {
  20. // `dart-sass` 是首选
  21. implementation: require('sass'),
  22. //additionalData: `@import "${resolve('src/styles')}/_base.scss";`,
  23. sassOptions: {
  24. includePaths: [require('bourbon').includePaths[0]],
  25. },
  26. },
  27. };
  28. const lessLoader = {
  29. loader: 'less-loader',
  30. options: {
  31. lessOptions: {
  32. javascriptEnabled: true,
  33. modifyVars: theme,
  34. },
  35. },
  36. };
  37. const baseLoaders = (modules) => [
  38. config.extractCss ? MiniCssExtractPlugin.loader : 'style-loader',
  39. cacheLoader,
  40. cssLoader(modules),
  41. 'postcss-loader',
  42. ];
  43. module.exports = [
  44. {
  45. test: /\.css$/,
  46. use: ['style-loader', 'css-loader'],
  47. },
  48. {
  49. test: /\.scss$/,
  50. include: [resolve('src')],
  51. use: [...baseLoaders(true), sassLoader],
  52. },
  53. {
  54. // for ant design
  55. test: /\.less$/,
  56. // less do not use threadLoader
  57. // https://github.com/webpack-contrib/thread-loader/issues/10
  58. use: [...baseLoaders(false), lessLoader],
  59. },
  60. ];