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;
}
相关推荐
华仔啊6 小时前
图片标签用 img 还是 picture?很多人彻底弄混了!
前端·html
lichong9516 小时前
XLog debug 开启打印日志,release 关闭打印日志
android·java·前端
烟袅6 小时前
作用域链 × 闭包:三段代码,看懂 JavaScript 的套娃人生
前端·javascript
风止何安啊7 小时前
收到字节的短信:Trae SOLO上线了?尝尝鲜,浅浅做个音乐播放器
前端·html·trae
抱琴_7 小时前
大屏性能优化终极方案:请求合并+智能缓存双剑合璧
前端·javascript
用户463989754327 小时前
Harmony os——长时任务(Continuous Task,ArkTS)
前端
fruge7 小时前
低版本浏览器兼容方案:IE11 适配 ES6 语法与 CSS 新特性
前端·css·es6
颜酱7 小时前
开发工具链-构建、测试、代码质量校验常用包的比较
前端·javascript·node.js
颜酱8 小时前
package.json 配置指南
前端·javascript·node.js
todoitbo8 小时前
基于 DevUI MateChat 搭建前端编程学习智能助手:从痛点到解决方案
前端·学习·ai·状态模式·devui·matechat