rule.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { RuleSetRule } from 'webpack';
  2. import { SOURCE_SUFFIX_LOADER, THEME_LOADER, OMIT_CSS_LOADER, PREFIX_LOADER, WEB_COMPONENT_LOADER, EXTRACT_CSS_LOADER } from './constants';
  3. import { SemiWebpackPluginOptions, SemiThemeOptions } from './types';
  4. import { stringifyVariableRecord } from './utils';
  5. export function createSourceSuffixLoaderRule(_opts?: SemiWebpackPluginOptions) {
  6. return {
  7. test: /@douyinfe(\/|\\)+semi-(ui|icons)(\/|\\)+.+\.js$/,
  8. use: [{ loader: SOURCE_SUFFIX_LOADER }],
  9. };
  10. }
  11. export function createThemeLoaderRule(opts?: SemiWebpackPluginOptions) {
  12. const themeOptions: SemiThemeOptions = {};
  13. if (typeof opts.theme === 'object') {
  14. Object.assign(themeOptions, opts.theme);
  15. } else {
  16. themeOptions.name = opts.theme;
  17. }
  18. const options = {
  19. ...themeOptions,
  20. prefixCls: opts.prefixCls,
  21. variables: stringifyVariableRecord(opts.variables),
  22. include: opts.include,
  23. cssLayer: opts.cssLayer
  24. };
  25. const loaderInfo = {
  26. test: /@douyinfe(\/|\\)+semi-(ui|icons|foundation)(\/|\\)+lib(\/|\\)+.+\.scss$/,
  27. use: [{ loader: THEME_LOADER, options }],
  28. };
  29. if (opts.webComponentPath) {
  30. const commonLoader = [
  31. { loader: "raw-loader" },
  32. { loader: EXTRACT_CSS_LOADER },
  33. {
  34. loader: 'css-loader',
  35. options: { sourceMap: false }
  36. },
  37. { loader: 'sass-loader' }
  38. ];
  39. loaderInfo.use = [
  40. ...commonLoader,
  41. ...loaderInfo.use
  42. ] as any;
  43. }
  44. return loaderInfo;
  45. }
  46. export function createOmitCssLoaderRule(_opts?: SemiWebpackPluginOptions) {
  47. return {
  48. test: /@douyinfe(\/|\\)+semi-[^/]+(\/|\\)+.+env\.js$/,
  49. use: [{ loader: OMIT_CSS_LOADER }],
  50. };
  51. }
  52. export function createPrefixLoaderRule(opts?: SemiWebpackPluginOptions) {
  53. const options = {
  54. replacers: { BASE_CLASS_PREFIX: opts.prefixCls },
  55. };
  56. return {
  57. test: /@douyinfe(\/|\\)+semi-[^/]+(\/|\\)+.+env\.js$/,
  58. use: [{ loader: PREFIX_LOADER, options }],
  59. };
  60. }
  61. export function createWebComponentLoaderRule(opts?: SemiWebpackPluginOptions) {
  62. return {
  63. test: opts.webComponentPath instanceof RegExp ? opts.webComponentPath : /src\/([^/]+\/)*[^/]+\.(ts|tsx|js|jsx)$/,
  64. type: 'javascript/auto',
  65. exclude: /node_modules/,
  66. use: [{ loader: WEB_COMPONENT_LOADER }],
  67. };
  68. }
  69. export function applySemiRules(opts?: SemiWebpackPluginOptions) {
  70. const rules: RuleSetRule[] = [];
  71. if (opts.omitCss) {
  72. rules.push(createOmitCssLoaderRule(opts));
  73. if (!opts.webComponentPath) {
  74. return rules;
  75. }
  76. }
  77. rules.push(createSourceSuffixLoaderRule(opts));
  78. rules.push(createThemeLoaderRule(opts));
  79. if (opts.prefixCls) {
  80. rules.push(createPrefixLoaderRule(opts));
  81. }
  82. if (opts.webComponentPath) {
  83. rules.push(createWebComponentLoaderRule(opts));
  84. }
  85. return rules;
  86. }