Skip to content

Commit

Permalink
add allowTopLevelThis option to transform-modules-systemjs (#10780)
Browse files Browse the repository at this point in the history
* feat: expose rewriteThis method

* feat: add allowTopLevelThis option to transform-modules-systemjs
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Jan 10, 2020
1 parent 04354d1 commit 44f9d85
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/babel-helper-module-transforms/src/index.js
Expand Up @@ -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
Expand Down
Expand Up @@ -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"
},
Expand Down
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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();
Expand Down
@@ -0,0 +1 @@
export var v = this;
@@ -0,0 +1,7 @@
{
"plugins": [
["transform-modules-systemjs", {
"allowTopLevelThis": false
}]
]
}
@@ -0,0 +1,11 @@
System.register([], function (_export, _context) {
"use strict";

var v;
return {
setters: [],
execute: function () {
_export("v", v = void 0);
}
};
});
@@ -0,0 +1 @@
export var v = this;
@@ -0,0 +1,7 @@
{
"plugins": [
["transform-modules-systemjs", {
"allowTopLevelThis": true
}]
]
}
@@ -0,0 +1,11 @@
System.register([], function (_export, _context) {
"use strict";

var v;
return {
setters: [],
execute: function () {
_export("v", v = this);
}
};
});

0 comments on commit 44f9d85

Please sign in to comment.