import fs from 'fs'; import path from 'path'; // Final patch: guards ALL async paths in form-label-wrap to prevent // errors during vxe-table expand row teardown export default function patchElFormNan() { return { name: 'patch-el-form-nan', enforce: 'pre', buildStart() { const target = path.resolve( process.cwd(), 'node_modules/element-plus/es/components/form/src/form-label-wrap.mjs' ); if (!fs.existsSync(target)) return; const code = fs.readFileSync(target, 'utf-8'); if (code.includes('_isMounted')) return; // already patched const patched = code .replace('return Math.ceil(Number.parseFloat(width))', 'return Math.ceil(Number.parseFloat(width)) || 0') .replace('const updateLabelWidth = (action = \"update\") => {', 'let _isMounted = true;\n\tconst updateLabelWidth = (action = \"update\") => {') .replace('nextTick(() => {', 'nextTick(() => {\n\t\t\t\tif (!_isMounted) return;\n\t\t\t\ttry {') .replace('else if (action === \"remove\") formContext?.deregisterLabelWidth(computedWidth.value);', 'else if (action === \"remove\") formContext?.deregisterLabelWidth(computedWidth.value);\n\t\t\t\t} catch (e) { /* teardown race */ }') .replace('onBeforeUnmount(() => {', 'onBeforeUnmount(() => {\n\t\t\t_isMounted = false;') .replace('onUpdated(() => updateLabelWidthFn())', 'onUpdated(() => { if (_isMounted) updateLabelWidthFn(); })') .replace('if (props.updateAll) formContext?.registerLabelWidth(val, oldVal);', 'if (_isMounted && props.updateAll) formContext?.registerLabelWidth(val, oldVal);') .replace('return () => {', 'return () => {\n\t\t\tif (!_isMounted) return null;'); if (patched !== code) { fs.writeFileSync(target, patched, 'utf-8'); console.log('[patch-el-form-nan] Patched form-label-wrap.mjs'); } } }; }