UNPKG

@aniyajs/rotor

Version:

基于webpack5开发的一款专注于打包、运行的工具

38 lines (32 loc) 1.26 kB
const paths = require('./paths'); class WatchUnoConfigPlugin { apply(compiler) { // 在每次构建前(无论是首次还是监听模式下的重新构建) compiler.hooks.beforeRun.tap('WatchUnoConfigPlugin', (compiler) => { this.addDependency(compiler); }); // 在监听模式下,每次触发重新构建前 compiler.hooks.watchRun.tap('WatchUnoConfigPlugin', (compiler) => { this.addDependency(compiler); }); } addDependency(compiler) { try { const dependency = paths.unocssConfigPath; // Webpack 5 正确的添加文件依赖方式 if (compiler.modifiedFiles && compiler.modifiedFiles.has(dependency)) { // 如果文件已修改,清除缓存以确保重新加载配置 delete require.cache[dependency]; } // 使用 Webpack 5 推荐的 API 添加文件依赖 compiler.hooks.compilation.tap('WatchUnoConfigPlugin', (compilation) => { if (compilation.fileDependencies.add) { compilation.fileDependencies.add(dependency); } }); } catch (e) { console.warn('Failed to add uno.config.ts to watch:', e.message); } } } module.exports = WatchUnoConfigPlugin;