UNPKG

@uiw-admin/plugins

Version:
71 lines (69 loc) 2.16 kB
/** * 对项目入口文件进行自动生成 * */ import fs from 'fs'; import path from 'path'; import { getInitIndexTemp, getInitCssTemp } from './temp'; import chokidar from 'chokidar'; class InitIndexWebpackPlugin { constructor(props) { this.globalCss = false; this.swr = false; this.SWRConfig = undefined; this.routeType = undefined; if (Reflect.has(props || {}, 'swr')) { if (typeof Reflect.get(props || {}, 'swr') === 'boolean') { this.swr = props == null ? void 0 : props.swr; } else if (typeof Reflect.get(props || {}, 'swr') === 'object') { this.swr = true; this.SWRConfig = props == null ? void 0 : props.swr; } } if (Reflect.has(props || {}, 'routeType')) { this.routeType = props == null ? void 0 : props.routeType; } } init() { if (fs.existsSync(path.resolve(process.cwd(), 'src/global.css'))) { this.globalCss = true; } else { this.globalCss = false; } var initIndexs = getInitIndexTemp({ swr: this.swr, SWRConfig: this.SWRConfig, routeType: this.routeType, globalCss: this.globalCss }); if (!fs.existsSync(path.resolve(process.cwd(), 'src/.uiw'))) { fs.mkdirSync(path.resolve(process.cwd(), 'src/.uiw')); } fs.writeFileSync(path.resolve(process.cwd(), 'src/.uiw/index.tsx'), initIndexs, { encoding: 'utf-8', flag: 'w+' }); fs.writeFileSync(path.resolve(process.cwd(), 'src/.uiw/index.css'), getInitCssTemp(), { encoding: 'utf-8', flag: 'w+' }); } apply(compiler) { compiler.hooks.initialize.tap('InitIndexWebpackPlugin', () => { this.init(); this.listenGlobalCss(); }); } // 监听 global.css 文件是否删除或添加 listenGlobalCss() { if (process.env.NODE_ENV === 'development') { chokidar.watch(path.resolve(process.cwd(), 'src/global.css'), { cwd: path.resolve(process.cwd(), 'src') }).on('all', (event, path) => { if (['add', 'unlink'].includes(event) && /global.css/.test(path)) { this.init(); } }); } } } export default InitIndexWebpackPlugin;