From 18ec2a2edac44eadcc0edd8fba946860d6767e60 Mon Sep 17 00:00:00 2001 From: ahmed Date: Fri, 15 May 2026 07:58:58 -0500 Subject: [PATCH] feat: support loading actions in esm environments --- src/lib/run-dev.js | 23 ++++++++++++++++--- .../successReturnAction-temp/package.json | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 test/__fixtures__/dist/my-package/successReturnAction-temp/package.json diff --git a/src/lib/run-dev.js b/src/lib/run-dev.js index 190173b..687e72e 100644 --- a/src/lib/run-dev.js +++ b/src/lib/run-dev.js @@ -14,6 +14,7 @@ const cloneDeep = require('lodash.clonedeep') const express = require('express') const fs = require('fs-extra') const path = require('node:path') +const fsp = require('node:fs/promises') const https = require('node:https') const crypto = require('node:crypto') const livereload = require('livereload') @@ -316,9 +317,25 @@ async function invokeSequence ({ actionRequestContext, logger }) { * @returns {object} the action function */ async function defaultActionLoader ({ distFolder, packageName, actionName }) { - const actionFolder = path.join(distFolder, packageName, actionName) - const actionPath = `${actionFolder}-temp/index.js` - delete require.cache[actionPath] + const actionTempDir = path.join(distFolder, packageName, `${actionName}-temp`) + const actionPath = path.join(actionTempDir, 'index.js') + + // Bundled actions are CommonJS. If an ancestor app package.json has "type": "module", + // Node would treat index.js here as ESM unless this directory declares otherwise. + try { + await fsp.access(actionPath) + } catch { + return require(actionPath)?.main + } + + await fsp.writeFile(path.join(actionTempDir, 'package.json'), '{"type":"commonjs"}', 'utf8') + + try { + delete require.cache[require.resolve(actionPath)] + } catch { + // ignore: module not yet loaded + } + return require(actionPath)?.main } diff --git a/test/__fixtures__/dist/my-package/successReturnAction-temp/package.json b/test/__fixtures__/dist/my-package/successReturnAction-temp/package.json new file mode 100644 index 0000000..0292b99 --- /dev/null +++ b/test/__fixtures__/dist/my-package/successReturnAction-temp/package.json @@ -0,0 +1 @@ +{"type":"commonjs"} \ No newline at end of file