在我的webpack开发案例中,我需要通过function.toString()
将函数输出为字符串。但是webpack可能会将一些语句转换为模块样式。
const myFunc = function{
this.methods.log(Date.now())
}
myFunc.toString()
"myFunc(){this.methods.log(_Users_xxx_Desktop_designer_web_node_modules_babel_runtime_corejs2_core_js_date_now__WEBPACK_IMPORTED_MODULE_3___default()());}"
我需要的:
"myFunc(){this.methods.log(Date.now())}"
我处理this
指针以纠正对象。我只是想让webkit忽略这个功能。
对于这个特殊情况,转换首先由babel完成,使用transform-runtime
插件,它决定Date
API需要来自core-js
。 Webpack稍后会介入,因为它看到Date
现在引用了一个导入的lib,因此它将import语句转换为WEBPACK_IMPORT_MODULE
。
你要么调整babel禁止它变换Date
,要么你可以反过来。你编写一个函数字符串,然后评估它以获得一个真正的函数。
const myFuncStr = `function (){
this.methods.log(Date.now())
}`
const myFunc = eval(myFuncStr)