Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add allowTopLevelThis option to transform-modules-systemjs #10780

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
};
});