编译时为OakPage注入相应的path

This commit is contained in:
Xu Chang 2022-09-09 19:45:16 +08:00
parent c67a349cd6
commit 18138d6d48
1 changed files with 28 additions and 0 deletions

View File

@ -166,6 +166,34 @@ module.exports = (babel) => {
}
}
},
Identifier(path, state) {
const { cwd, filename } = state;
const rel = relative(cwd, filename).replace(/\\/g, '/');
const { node, parent } = path;
if (node.name === 'OakPage' && /pages[\w|\W]+index\.ts$/.test(rel)) {
console.log(rel);
const relativePath = rel.slice(9, rel.length - 9);
console.log(relativePath);
assert(t.isCallExpression(parent));
const { arguments } = parent;
assert(arguments.length === 1 && t.isObjectExpression(arguments[0]));
const { properties } = arguments[0];
const pathProperty = properties.find(
ele => t.isObjectProperty(ele) && t.isIdentifier(ele.key) && ele.key.name === 'path'
);
if (pathProperty) {
console.warn(`${rel}页面的OakPage中还是定义了path可以删除掉了`);
pathProperty.value = t.stringLiteral(relativePath);
}
else {
properties.push(
t.objectProperty(t.identifier('path'), t.stringLiteral(relativePath))
);
}
}
},
},
};
};