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 9c3dd8e181..ffc45ed476 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js @@ -121,8 +121,12 @@ export default function () { inherits: require("babel-plugin-transform-strict-mode"), visitor: { - ThisExpression(path) { - if (!path.findParent((path) => !path.is("shadow") && THIS_BREAK_KEYS.indexOf(path.type) >= 0)) { + ThisExpression(path, state) { + if ( + state.opts.allowTopLevelThis !== true && + !path.findParent((path) => !path.is("shadow") && + THIS_BREAK_KEYS.indexOf(path.type) >= 0) + ) { path.replaceWith(t.identifier("undefined")); } },