rsbuild-插件

实现一个 rsbuild的插件

当前版本rsbuild版本

json 复制代码
{
  "devDependencies": {
    "@rsbuild/core": "^1.3.1",
    "@rsbuild/plugin-vue": "^1.0.7"
  }
}

实现plugins

js 复制代码
// rsbuild.config.mjs

import { defineConfig } from '@rsbuild/core';
import { stylelintPlugin } from './plugins/stylelintPlugin';

export default defineConfig({
  plugins: [
    pluginVue(),
    stylelintPlugin({
      files: ['**/*.css', '**/*.scss'],
      exclude: /node_modules/,
      fix: true
    })
  ]
});
js 复制代码
// plugins/stylelintPlugin.js

export const stylelintPlugin = (options = {}) => ({
  name: 'stylelint-plugin',
  setup(api) {
    api.onBeforeStartDevServer(async () => {
      const { default: stylelint } = await import('stylelint');

      const result = await stylelint.lint({
        files: options.files || 'src/**/*.{css,scss,less}',
        fix: options.fix || false,
        formatter: 'string'
      });

      if (result.results.length > 0) {
        console.error(result.report);
      }
    });
  }
});

实现一个loader

js 复制代码
// rsbuild.config.mjs

import { defineConfig } from '@rsbuild/core';
import { pluginVue } from '@rsbuild/plugin-vue';
import path from 'path';
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export default defineConfig({
  plugins: [
    pluginVue(),
  ],
  tools: {
    rspack: (config) => {
      config.module.rules.push({
        test: /\.(js|vue)$/,
        exclude: /node_modules/,
        enforce: 'pre',
        use: [path.resolve(__dirname, 'custom-loader.js')]
      });
      return config;
    }
  }
});

同webpack-loader

js 复制代码
// custom-loader.js

export default function (source) {
  return source;
}
相关推荐
liliangcsdn几秒前
mac mlx大模型框架的安装和使用
java·前端·人工智能·python·macos
CssHero4 分钟前
基于vue3完成领域模型架构建设
前端
PanZonghui7 分钟前
用项目说话:我的React博客构建成果与经验复盘
前端·react.js·typescript
言兴10 分钟前
教你如何理解useContext加上useReducer
前端·javascript·面试
sunbyte13 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GoodCheapFast(Good - Cheap - Fast三选二开关)
前端·javascript·css·vue.js·tailwindcss
前端的日常15 分钟前
网页视频录制新技巧,代码实现超简单!
前端
前端的日常16 分钟前
什么是 TypeScript 中的泛型?请给出一个使用泛型的示例。
前端
今禾18 分钟前
一行代码引发的血案:new Array(5) 到底发生了什么?
前端·javascript·算法
ccc101820 分钟前
老师问我localhost和127.0.0.1,有什么区别?
前端
Struggler28127 分钟前
Chrome插件开发
前端