From 8c4869e9f3da3713a24b4eba0218ad618ce31081 Mon Sep 17 00:00:00 2001 From: Peter Muessig Date: Mon, 10 Jun 2024 17:40:17 +0200 Subject: [PATCH] fix(ui5-tooling-modules): usage of estree-walker for modern ES support --- .../lib/rollup-plugin-transform-top-level-this.js | 13 ++++++------- packages/ui5-tooling-modules/lib/util.js | 3 ++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/ui5-tooling-modules/lib/rollup-plugin-transform-top-level-this.js b/packages/ui5-tooling-modules/lib/rollup-plugin-transform-top-level-this.js index 21f17aa3..e714bef4 100644 --- a/packages/ui5-tooling-modules/lib/rollup-plugin-transform-top-level-this.js +++ b/packages/ui5-tooling-modules/lib/rollup-plugin-transform-top-level-this.js @@ -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) { @@ -25,7 +24,7 @@ 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(); @@ -33,10 +32,10 @@ module.exports = function ({ log } = {}) { }, leave: function (node, parent) { if (node?.type === "ThisExpression" && parent?.type === "MemberExpression") { - return { + this.replace({ name: "exports", type: "Identifier", - }; + }); } }, }); diff --git a/packages/ui5-tooling-modules/lib/util.js b/packages/ui5-tooling-modules/lib/util.js index bb4cc977..7e98d0f2 100755 --- a/packages/ui5-tooling-modules/lib/util.js +++ b/packages/ui5-tooling-modules/lib/util.js @@ -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! */, @@ -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 }) {