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

fix(ui5-tooling-modules): usage of estree-walker for modern ES support #1020

Merged
merged 1 commit into from
Jun 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
const estraverse = require("estraverse");
const escodegen = require("@javascript-obfuscator/escodegen"); // escodegen itself isn't released anymore since 2020 => using fork

/* eslint-disable no-unused-vars */
module.exports = function ({ log } = {}) {
module.exports = function ({ log, walk } = {}) {
return {
name: "transform-top-level-this",
transform: function (code, id) {
const ast = this.parse(code);
let hasTopLevelThis = false;
estraverse.traverse(ast, {
walk(ast, {
enter(node, parent) {
if (node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
this.skip();
} else if (node.type === "ImportDeclaration" || node.type === "ExportDeclaration" || node.type === "ExportDefaultDeclaration" || node.type === "ExportNamedDeclaration") {
hasTopLevelThis = false; // for ESM we ignore top level this
this.break();
this.skip();
}
},
leave: function (node, parent) {
Expand All @@ -25,18 +24,18 @@ module.exports = function ({ log } = {}) {
});
if (hasTopLevelThis) {
log.info(`Transforming top-level "this" to "exports" in non ES module ${id}!`);
estraverse.replace(ast, {
walk(ast, {
enter(node, parent) {
if (node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
this.skip();
}
},
leave: function (node, parent) {
if (node?.type === "ThisExpression" && parent?.type === "MemberExpression") {
return {
this.replace({
name: "exports",
type: "Identifier",
};
});
}
},
});
Expand Down
3 changes: 2 additions & 1 deletion packages/ui5-tooling-modules/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ module.exports = function (log) {
* @returns {rollup.RollupOutput} the build output of rollup
*/
createBundle: async function createBundle(moduleNames, { cwd, depPaths, mainFields, beforePlugins, afterPlugins, generatedCode, inject, isMiddleware } = {}) {
const { walk } = await import("estree-walker");
const bundle = await rollup.rollup({
input: moduleNames,
//context: "exports" /* this is normally converted to undefined, but should be exports in our case! */,
Expand Down Expand Up @@ -679,7 +680,7 @@ module.exports = function (log) {
return that.resolveModule(moduleName, { cwd, depPaths, mainFields });
},
}),
transformTopLevelThis({ log }),
transformTopLevelThis({ log, walk }),
...(afterPlugins || []),
],
onwarn: function ({ loc, frame, code, message }) {
Expand Down
Loading