mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-16 10:54:49 +00:00
23 lines
665 B
JavaScript
23 lines
665 B
JavaScript
|
|
||
|
import serialize from 'serialize-javascript'
|
||
|
|
||
|
export function serializeFunction(func) {
|
||
|
let open = false
|
||
|
return serialize(func)
|
||
|
.replace(serializeFunction.assignmentRE, (_, spaces) => {
|
||
|
return `${spaces}:function(`
|
||
|
})
|
||
|
.replace(serializeFunction.internalFunctionRE, (_, spaces, name, args) => {
|
||
|
if (open) {
|
||
|
return `${spaces}${name}:function(${args}) {`
|
||
|
} else {
|
||
|
open = true
|
||
|
return _
|
||
|
}
|
||
|
})
|
||
|
.replace(`${func.name}(`, 'function(')
|
||
|
}
|
||
|
|
||
|
serializeFunction.internalFunctionRE = /^(\s*)(?!(?:if)|(?:for)|(?:while)|(?:switch))(\w+)\s*\((.*?)\)\s*\{/gm
|
||
|
serializeFunction.assignmentRE = /^(\s*):(\w+)\(/gm
|