Nuxt3 实战 :配置 Eslint、Prettierrc、Husky等项目提交规范
warning:
这篇文章距离上次修改已过192天,其中的内容可能已经有所变动。
// nuxt.config.js
export default defineNuxtConfig({
// 其他配置...
build: {
// 添加 ESLint 插件
extend(config, { isDev, isClient }) {
if (isDev && isClient) {
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /node_modules/,
options: {
cache: true,
},
});
}
},
},
// 添加 ESLint 检查入口
generate: {
interval: 2000,
},
// 添加 ESLint 检查出口
hooks: {
'pages:route': async (page) => {
if (page.path === '/') {
await runEslint('src');
}
},
},
});
// 辅助函数,用于运行 ESLint 检查
async function runEslint(path) {
const { npx } = await import('node:child_process');
const { promisify } = await import('node:util');
const exec = promisify(npx.exec);
try {
const { stdout } = await exec(`eslint --ext .js,.vue ${path}`);
console.log(stdout);
} catch (error) {
console.error(error.stderr);
process.exit(1);
}
}
这个代码实例展示了如何在 Nuxt3 项目的 nuxt.config.js
文件中配置 ESLint,并且使用一个辅助函数 runEslint
来执行 ESLint 检查。这是一个简化的示例,仅包含核心配置和函数,以教学为目的。在实际项目中,你可能需要更复杂的配置和错误处理。
评论已关闭