From db2d0bbe077eb93dc01fb11fff9287c62c0d2870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 29 Nov 2019 16:12:20 -0500 Subject: [PATCH 1/2] feat: expose rewriteThis method --- packages/babel-helper-module-transforms/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-helper-module-transforms/src/index.js b/packages/babel-helper-module-transforms/src/index.js index 5735ad951e8d..fdc4359614e7 100644 --- a/packages/babel-helper-module-transforms/src/index.js +++ b/packages/babel-helper-module-transforms/src/index.js @@ -12,7 +12,7 @@ import normalizeAndLoadModuleMetadata, { isSideEffectImport, } from "./normalize-and-load-metadata"; -export { hasExports, isSideEffectImport, isModule }; +export { hasExports, isSideEffectImport, isModule, rewriteThis }; /** * Perform all of the generic ES6 module rewriting needed to handle initial From f791f72594a84b41ac384ef518647e332fc8eeb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 29 Nov 2019 16:12:55 -0500 Subject: [PATCH 2/2] feat: add allowTopLevelThis option to transform-modules-systemjs --- .../package.json | 1 + .../src/index.js | 6 +++++- .../fixtures/allow-top-level-this/false/input.mjs | 1 + .../fixtures/allow-top-level-this/false/options.json | 7 +++++++ .../fixtures/allow-top-level-this/false/output.mjs | 11 +++++++++++ .../test/fixtures/allow-top-level-this/true/input.mjs | 1 + .../fixtures/allow-top-level-this/true/options.json | 7 +++++++ .../fixtures/allow-top-level-this/true/output.mjs | 11 +++++++++++ 8 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/input.mjs create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/options.json create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/output.mjs create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/input.mjs create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/options.json create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/output.mjs diff --git a/packages/babel-plugin-transform-modules-systemjs/package.json b/packages/babel-plugin-transform-modules-systemjs/package.json index e00b97eec6aa..0ab4deee2d9f 100644 --- a/packages/babel-plugin-transform-modules-systemjs/package.json +++ b/packages/babel-plugin-transform-modules-systemjs/package.json @@ -10,6 +10,7 @@ "main": "lib/index.js", "dependencies": { "@babel/helper-hoist-variables": "^7.7.4", + "@babel/helper-module-transforms": "^7.7.4", "@babel/helper-plugin-utils": "^7.0.0", "babel-plugin-dynamic-import-node": "^2.3.0" }, diff --git a/packages/babel-plugin-transform-modules-systemjs/src/index.js b/packages/babel-plugin-transform-modules-systemjs/src/index.js index c09a438262bc..fa322f434cc2 100644 --- a/packages/babel-plugin-transform-modules-systemjs/src/index.js +++ b/packages/babel-plugin-transform-modules-systemjs/src/index.js @@ -2,6 +2,7 @@ import { declare } from "@babel/helper-plugin-utils"; import hoistVariables from "@babel/helper-hoist-variables"; import { template, types as t } from "@babel/core"; import { getImportSource } from "babel-plugin-dynamic-import-node/utils"; +import { rewriteThis } from "@babel/helper-module-transforms"; const buildTemplate = template(` SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) { @@ -106,7 +107,7 @@ function constructExportCall( export default declare((api, options) => { api.assertVersion(7); - const { systemGlobal = "System" } = options; + const { systemGlobal = "System", allowTopLevelThis = false } = options; const IGNORE_REASSIGNMENT_SYMBOL = Symbol(); const reassignmentVisitor = { @@ -227,6 +228,9 @@ export default declare((api, options) => { Program: { enter(path, state) { state.contextIdent = path.scope.generateUid("context"); + if (!allowTopLevelThis) { + rewriteThis(path); + } }, exit(path, state) { const undefinedIdent = path.scope.buildUndefinedNode(); diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/input.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/input.mjs new file mode 100644 index 000000000000..91f171590120 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/input.mjs @@ -0,0 +1 @@ +export var v = this; diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/options.json b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/options.json new file mode 100644 index 000000000000..51c1aaefdc1d --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + ["transform-modules-systemjs", { + "allowTopLevelThis": false + }] + ] +} diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/output.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/output.mjs new file mode 100644 index 000000000000..de6449909d41 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/false/output.mjs @@ -0,0 +1,11 @@ +System.register([], function (_export, _context) { + "use strict"; + + var v; + return { + setters: [], + execute: function () { + _export("v", v = void 0); + } + }; +}); diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/input.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/input.mjs new file mode 100644 index 000000000000..91f171590120 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/input.mjs @@ -0,0 +1 @@ +export var v = this; diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/options.json b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/options.json new file mode 100644 index 000000000000..066522d40d09 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + ["transform-modules-systemjs", { + "allowTopLevelThis": true + }] + ] +} diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/output.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/output.mjs new file mode 100644 index 000000000000..032cb659bdc2 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/allow-top-level-this/true/output.mjs @@ -0,0 +1,11 @@ +System.register([], function (_export, _context) { + "use strict"; + + var v; + return { + setters: [], + execute: function () { + _export("v", v = this); + } + }; +});