实现一个 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;
}