|
|
const { getRefreshGlobalScope } = require('../globals'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getRefreshGlobal( |
|
|
Template, |
|
|
RuntimeGlobals = {}, |
|
|
RuntimeTemplate = { |
|
|
basicFunction(args, body) { |
|
|
return `function(${args}) {\n${Template.indent(body)}\n}`; |
|
|
}, |
|
|
supportsConst() { |
|
|
return false; |
|
|
}, |
|
|
returningFunction(returnValue, args = '') { |
|
|
return `function(${args}) { return ${returnValue}; }`; |
|
|
}, |
|
|
} |
|
|
) { |
|
|
const declaration = RuntimeTemplate.supportsConst() ? 'const' : 'var'; |
|
|
const refreshGlobal = getRefreshGlobalScope(RuntimeGlobals); |
|
|
return Template.asString([ |
|
|
`${refreshGlobal} = {`, |
|
|
Template.indent([ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
`register: ${RuntimeTemplate.returningFunction('undefined')},`, |
|
|
`signature: ${RuntimeTemplate.returningFunction( |
|
|
RuntimeTemplate.returningFunction('type', 'type') |
|
|
)},`, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'runtime: {', |
|
|
Template.indent([ |
|
|
`createSignatureFunctionForTransform: ${RuntimeTemplate.returningFunction( |
|
|
RuntimeTemplate.returningFunction('type', 'type') |
|
|
)},`, |
|
|
`register: ${RuntimeTemplate.returningFunction('undefined')}`, |
|
|
]), |
|
|
'},', |
|
|
|
|
|
|
|
|
`setup: ${RuntimeTemplate.basicFunction('currentModuleId', [ |
|
|
// Store all previous values for fields on `refreshGlobal` - |
|
|
// this allows proper restoration in the `cleanup` phase. |
|
|
`${declaration} prevModuleId = ${refreshGlobal}.moduleId;`, |
|
|
`${declaration} prevRegister = ${refreshGlobal}.register;`, |
|
|
`${declaration} prevSignature = ${refreshGlobal}.signature;`, |
|
|
`${declaration} prevCleanup = ${refreshGlobal}.cleanup;`, |
|
|
'', |
|
|
`${refreshGlobal}.moduleId = currentModuleId;`, |
|
|
'', |
|
|
`${refreshGlobal}.register = ${RuntimeTemplate.basicFunction('type, id', [ |
|
|
`${declaration} typeId = currentModuleId + " " + id;`, |
|
|
`${refreshGlobal}.runtime.register(type, typeId);`, |
|
|
])}`, |
|
|
'', |
|
|
`${refreshGlobal}.signature = ${RuntimeTemplate.returningFunction( |
|
|
`${refreshGlobal}.runtime.createSignatureFunctionForTransform()` |
|
|
)};`, |
|
|
'', |
|
|
`${refreshGlobal}.cleanup = ${RuntimeTemplate.basicFunction('cleanupModuleId', [ |
|
|
// Only cleanup if the module IDs match. |
|
|
// In rare cases, it might get called in another module's `cleanup` phase. |
|
|
'if (currentModuleId === cleanupModuleId) {', |
|
|
Template.indent([ |
|
|
`${refreshGlobal}.moduleId = prevModuleId;`, |
|
|
`${refreshGlobal}.register = prevRegister;`, |
|
|
`${refreshGlobal}.signature = prevSignature;`, |
|
|
`${refreshGlobal}.cleanup = prevCleanup;`, |
|
|
]), |
|
|
'}', |
|
|
])}`, |
|
|
])}`, |
|
|
]), |
|
|
'};', |
|
|
]); |
|
|
} |
|
|
|
|
|
module.exports = getRefreshGlobal; |
|
|
|