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;
}
相关推荐
excel15 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel16 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼18 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping18 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙19 小时前
[译] Composition in CSS
前端·css
白水清风19 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix19 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户221520442780019 小时前
new、原型和原型链浅析
前端·javascript
阿星做前端19 小时前
coze源码解读: space develop 页面
前端·javascript
叫我小窝吧19 小时前
Promise 的使用
前端·javascript