babel-plugin-preval
babel-plugin-preval 介绍
问题
您需要做一些动态的事情,但是不想在运行时做。或者,也许您想做一些事情,例如读取文件系统以获取文件列表,而您无法在浏览器中进行操作。
解决方案
这允许您指定一些在Node中运行的代码,其中的任何内容都 module.exports将被交换。例如:
const x = preval`module.exports = 1`
// ↓ ↓ ↓ ↓ ↓ ↓
const x = 1
或者
const x = preval`
const fs = require('fs')
const val = fs.readFileSync(__dirname + '/fixture1.md','utf8')
module.exports = {
val,
getSplit: function(splitDelimiter) {
return x.val.split(splitDelimiter)
}
}
`
// ↓ ↓ ↓ ↓ ↓ ↓
const x = {
val: '# fixturennThis is some file thing...n',
getSplit: function getSplit(splitDelimiter) {
return x.val.split(splitDelimiter)
},
}
网站地址:https://npm.im/babel-plugin-preval
GitHub:https://github.com/kentcdodds/babel-plugin-preval
网站描述:在构建期预先评估代码