diff --git a/test/electron/renderer.js b/test/electron/renderer.js index 6f1d6bf..6651093 100644 --- a/test/electron/renderer.js +++ b/test/electron/renderer.js @@ -22,22 +22,24 @@ /** * Evaluates plugin source code and returns the plugin object. - * Tries CJS eval first; falls back to ESM data-URL import for ES module syntax. + * Tries CJS eval first; on SyntaxError falls back to ESM data-URL import. * @param {string} code * @returns {Promise} */ async function evalPluginCode(code) { - const isESM = /^\s*(import\s|export\s)/m.test(code); - - if (!isESM) { - // CommonJS path + try { const m = { exports: {} }; // eslint-disable-next-line no-new-func new Function('module', 'exports', code)(m, m.exports); - return m.exports?.default ?? m.exports; + const result = m.exports?.default ?? m.exports; + if (result && typeof result === 'object') return result; + // Fall through to ESM if exports came back empty + } catch (err) { + if (!(err instanceof SyntaxError)) throw err; + // SyntaxError means ESM — fall through below } - // ESM path — import via data URL so the JS engine handles the module syntax + // ESM path — import via data URL so the engine handles module syntax natively const dataUrl = `data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`; const mod = await import(dataUrl); return mod.default ?? mod;