diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js index 30c0c5c8c3..769259cd6b 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js @@ -125,6 +125,11 @@ export default function () { visitor: { ThisExpression(path, state) { + // If other plugins run after this plugin's Program#exit handler, we allow them to + // insert top-level `this` values. This allows the AMD and UMD plugins to + // function properly. + if (this.ranCommonJS) return; + if ( state.opts.allowTopLevelThis !== true && !path.findParent((path) => !path.is("shadow") && @@ -136,6 +141,8 @@ export default function () { Program: { exit(path) { + this.ranCommonJS = true; + let strict = !!this.opts.strict; let { scope } = path; diff --git a/packages/babel-traverse/src/path/context.js b/packages/babel-traverse/src/path/context.js index fcd06d5ca0..2c2c0ed366 100644 --- a/packages/babel-traverse/src/path/context.js +++ b/packages/babel-traverse/src/path/context.js @@ -208,10 +208,22 @@ export function setKey(key) { this.type = this.node && this.node.type; } -export function requeue(path = this) { - if (path.removed) return; +export function requeue(pathToQueue = this) { + if (pathToQueue.removed) return; - for (let context of this.contexts) { - context.maybeQueue(path); + let contexts = this._getQueueContexts(); + + for (let context of contexts) { + context.maybeQueue(pathToQueue); } } + +export function _getQueueContexts(){ + let path = this; + let contexts = this.contexts; + while (!contexts.length) { + path = path.parentPath; + contexts = path.contexts; + } + return contexts; +} diff --git a/packages/babel-traverse/src/path/modification.js b/packages/babel-traverse/src/path/modification.js index ce141aa6c5..5811a19514 100644 --- a/packages/babel-traverse/src/path/modification.js +++ b/packages/babel-traverse/src/path/modification.js @@ -60,12 +60,7 @@ export function _containerInsert(from, nodes) { } } - let contexts = this.contexts; - let path = this; - while (!contexts.length) { - path = path.parentPath; - contexts = path.contexts; - } + let contexts = this._getQueueContexts(); for (let path of paths) { path.setScope();