11import { copyFileSync , mkdirSync , mkdtempSync , realpathSync , rmSync , writeFileSync } from "node:fs" ;
22import { tmpdir } from "node:os" ;
33import path from "node:path" ;
4+ import { pathToFileURL } from "node:url" ;
45import { inspect } from "node:util" ;
56import { runNodeScript } from "../helpers/run-node-script.js" ;
67
@@ -26,6 +27,51 @@ export const TSX_SHIM_WRAPPERS = [
2627 "scripts/perf/summarize-cpuprofile.mjs" ,
2728] as const ;
2829
30+ export function writeEsmPluginFixture ( fixtureRoot : string ) {
31+ const pluginRoot = path . join ( fixtureRoot , "compiled-plugin" ) ;
32+ const dependencyRoot = path . join ( pluginRoot , "node_modules" , "import-only" ) ;
33+ mkdirSync ( dependencyRoot , { recursive : true } ) ;
34+ writeFileSync ( path . join ( pluginRoot , "package.json" ) , JSON . stringify ( { type : "module" } ) ) ;
35+ writeFileSync (
36+ path . join ( dependencyRoot , "package.json" ) ,
37+ JSON . stringify ( { type : "module" , exports : { import : "./index.js" } } ) ,
38+ ) ;
39+ writeFileSync ( path . join ( dependencyRoot , "index.js" ) , 'export const value = "import-only";' ) ;
40+ const pluginPath = path . join ( pluginRoot , "index.js" ) ;
41+ writeFileSync (
42+ pluginPath ,
43+ `export { value } from "import-only";
44+ globalThis.pluginEvaluations = (globalThis.pluginEvaluations ?? 0) + 1;
45+ export const instance = {};
46+ ` ,
47+ ) ;
48+ writeFileSync (
49+ path . join ( pluginRoot , "loader.cjs" ) ,
50+ 'module.exports = { load: () => require("./index.js") };' ,
51+ ) ;
52+ return `
53+ import assert from "node:assert/strict";
54+ import { createRequire } from "node:module";
55+ import { parseBoolean } from "@openclaw/normalization-core/boolean-coercion";
56+ enum Transformed { Value = "transformed" }
57+ try {
58+ const require = createRequire(${ JSON . stringify ( pathToFileURL ( pluginPath ) . href ) } );
59+ const loader = require("./loader.cjs");
60+ const first = loader.load();
61+ const imported = await import(${ JSON . stringify ( pathToFileURL ( pluginPath ) . href ) } );
62+ assert.strictEqual(loader.load(), first);
63+ assert.strictEqual(imported.instance, first.instance);
64+ assert.strictEqual(require("./loader.cjs"), loader);
65+ assert.strictEqual(require(${ JSON . stringify ( path . resolve ( "packages/normalization-core/src/boolean-coercion.ts" ) ) } ).parseBoolean, parseBoolean);
66+ console.log(JSON.stringify({ value: first.value, evaluations: globalThis.pluginEvaluations,
67+ transformed: Transformed.Value, sourceAlias: parseBoolean(" TRUE ") }));
68+ } catch (error) {
69+ console.error(error.code + ": " + error.message);
70+ process.exitCode = 1;
71+ }
72+ ` ;
73+ }
74+
2975export async function withShimFixture < T > (
3076 wrapper : ( typeof TSX_SHIM_WRAPPERS ) [ number ] ,
3177 run : ( paths : {
0 commit comments