扫一扫分享
您需要做一些动态的事情,但是不想在运行时做。或者,也许您想做一些事情,例如读取文件系统以获取文件列表,而您无法在浏览器中进行操作。
这允许您指定一些在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: '# fixture\n\nThis is some file thing...\n',
getSplit: function getSplit(splitDelimiter) {
return x.val.split(splitDelimiter)
},
}
手机预览