From 323c67d8fc7fe6c385bb8ecac350664aafd98fe6 Mon Sep 17 00:00:00 2001 From: Steel Brain Date: Mon, 17 Sep 2018 01:46:13 -0700 Subject: [PATCH] :new: Make state of babel plugin scoped to that execution --- .../src/index.js | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/babel-plugin-inject-node-globals/src/index.js b/packages/babel-plugin-inject-node-globals/src/index.js index ecca4c27..eb3c23f1 100644 --- a/packages/babel-plugin-inject-node-globals/src/index.js +++ b/packages/babel-plugin-inject-node-globals/src/index.js @@ -23,34 +23,39 @@ const getTimersInjection = makeTemplate( const getBufferInjection = makeTemplate(`var Buffer = require('buffer').Buffer`) const getProcessInjection = makeTemplate(`var process = require('process')`) -export default function getPluginInjectNodeGlobals({ template }: Object) { - const injectionNames = new Set() +const INJECTIONS = Symbol('Node Global Injections') +export default function getPluginInjectNodeGlobals({ template }: Object) { return { visitor: { Identifier(path: Object) { const { node } = path if ( INJECTION_NAMES.has(node.name) && - !injectionNames.has(node.name) && + !path.hub[INJECTIONS].has(node.name) && path.isReferencedIdentifier() && !path.scope.hasBinding(node.name) ) { - injectionNames.add(node.name) + path.hub[INJECTIONS].add(node.name) } }, Program: { + enter(path: Object) { + path.hub[INJECTIONS] = new Set() + }, exit(path: Object) { - if (!injectionNames.size) return + const injections = path.hub[INJECTIONS] + + if (!injections.size) return const statements = [] - if (injectionNames.has('setImmediate') || injectionNames.has('clearImmediate')) { + if (injections.has('setImmediate') || injections.has('clearImmediate')) { statements.push(getTimersInjection({ template })) } - if (injectionNames.has('Buffer')) { + if (injections.has('Buffer')) { statements.push(getBufferInjection({ template })) } - if (injectionNames.has('process')) { + if (injections.has('process')) { statements.push(getProcessInjection({ template })) } path.node.body = statements.concat(path.node.body)