Merge branch 'worktree-iso-phase-4' into macros
This commit is contained in:
@@ -20,19 +20,39 @@ SX_TEST_JS = Path(__file__).resolve().parents[2] / "static" / "scripts" / "sx-te
|
|||||||
|
|
||||||
def _js_render(sx_text: str, components_text: str = "") -> str:
|
def _js_render(sx_text: str, components_text: str = "") -> str:
|
||||||
"""Run sx.js + sx-test.js in Node and return the renderToString result."""
|
"""Run sx.js + sx-test.js in Node and return the renderToString result."""
|
||||||
# Build a small Node script
|
import tempfile, os
|
||||||
|
# Build a small Node script that requires the source files
|
||||||
script = f"""
|
script = f"""
|
||||||
global.document = undefined; // no DOM needed for string render
|
globalThis.document = undefined; // no DOM needed for string render
|
||||||
{SX_JS.read_text()}
|
// sx.js IIFE uses (typeof window !== "undefined" ? window : this).
|
||||||
|
// In Node file mode, `this` is module.exports, not globalThis.
|
||||||
|
// Patch: make the IIFE target globalThis so Sx is accessible.
|
||||||
|
var _origThis = this;
|
||||||
|
Object.defineProperty(globalThis, 'document', {{ value: undefined, writable: true }});
|
||||||
|
(function() {{
|
||||||
|
var _savedThis = globalThis;
|
||||||
|
{SX_JS.read_text()}
|
||||||
|
// Hoist Sx from module.exports to globalThis if needed
|
||||||
|
if (typeof Sx === 'undefined' && typeof module !== 'undefined' && module.exports && module.exports.Sx) {{
|
||||||
|
globalThis.Sx = module.exports.Sx;
|
||||||
|
}}
|
||||||
|
}}).call(globalThis);
|
||||||
{SX_TEST_JS.read_text()}
|
{SX_TEST_JS.read_text()}
|
||||||
if ({json.dumps(components_text)}) Sx.loadComponents({json.dumps(components_text)});
|
if ({json.dumps(components_text)}) Sx.loadComponents({json.dumps(components_text)});
|
||||||
var result = Sx.renderToString({json.dumps(sx_text)});
|
var result = Sx.renderToString({json.dumps(sx_text)});
|
||||||
process.stdout.write(result);
|
process.stdout.write(result);
|
||||||
"""
|
"""
|
||||||
result = subprocess.run(
|
# Write to temp file to avoid OS arg length limits
|
||||||
["node", "-e", script],
|
fd, tmp = tempfile.mkstemp(suffix=".js")
|
||||||
capture_output=True, text=True, timeout=5,
|
try:
|
||||||
)
|
with os.fdopen(fd, "w") as f:
|
||||||
|
f.write(script)
|
||||||
|
result = subprocess.run(
|
||||||
|
["node", tmp],
|
||||||
|
capture_output=True, text=True, timeout=5,
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
os.unlink(tmp)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
pytest.fail(f"Node.js error:\n{result.stderr}")
|
pytest.fail(f"Node.js error:\n{result.stderr}")
|
||||||
return result.stdout
|
return result.stdout
|
||||||
|
|||||||
Reference in New Issue
Block a user