mirror of
https://github.com/morluto/rea.git
synced 2026-11-07 07:16:13 +00:00
test(javascript): execute the registered Webpack export fixture (#6951)
This commit is contained in:
1 parent
b5c42dfc2a
commit
d87e73f2db
1 file changed
+20
-14
@@ -5,6 +5,7 @@ import { createRequire } from "node:module";
|
|||||||
import { join, relative, resolve } from "node:path";
|
import { join, relative, resolve } from "node:path";
|
||||||
import { pathToFileURL } from "node:url";
|
import { pathToFileURL } from "node:url";
|
||||||
import { promisify } from "node:util";
|
import { promisify } from "node:util";
|
||||||
|
import { runInNewContext } from "node:vm";
|
||||||
|
|
||||||
import { Client } from "@modelcontextprotocol/client";
|
import { Client } from "@modelcontextprotocol/client";
|
||||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||||
@@ -74,15 +75,6 @@ afterAll(async () => {
|
|||||||
});
|
});
|
||||||
const WEBPACK_FACTORY_BODY = `
|
const WEBPACK_FACTORY_BODY = `
|
||||||
const key = "dynamic";
|
const key = "dynamic";
|
||||||
const __webpack_require__ = {
|
|
||||||
d(target, definitions) {
|
|
||||||
for (const name of Object.keys(definitions))
|
|
||||||
Object.defineProperty(target, name, {
|
|
||||||
enumerable: true,
|
|
||||||
get: definitions[name],
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
exports[key] = 1;
|
exports[key] = 1;
|
||||||
exports["dot.key"] = 2;
|
exports["dot.key"] = 2;
|
||||||
exports[""] = 3;
|
exports[""] = 3;
|
||||||
@@ -1106,14 +1098,28 @@ describe("Webpack factory CommonJS export keys", () => {
|
|||||||
}]);`;
|
}]);`;
|
||||||
await writeFile(join(root, "bundle.js"), bundle);
|
await writeFile(join(root, "bundle.js"), bundle);
|
||||||
|
|
||||||
// Execute the same factory body as CommonJS, outside the analyzer, to
|
// Execute the exact bundle and invoke its registered factory with a loader.
|
||||||
// verify Node's alias behavior and the exact runtime object keys.
|
// This also rejects invalid factory syntax before trusting the export oracle.
|
||||||
|
const actual = runInNewContext(`
|
||||||
|
globalThis.webpackChunkStaticExports = [];
|
||||||
|
${bundle}
|
||||||
|
const module = { exports: {} };
|
||||||
|
const loader = {
|
||||||
|
d(target, definitions) {
|
||||||
|
for (const name of Object.keys(definitions))
|
||||||
|
Object.defineProperty(target, name, {
|
||||||
|
enumerable: true,
|
||||||
|
get: definitions[name],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const [, factories] = globalThis.webpackChunkStaticExports[0];
|
||||||
|
factories[1](module, module.exports, loader);
|
||||||
|
module.exports;
|
||||||
|
`) as Record<string, unknown>;
|
||||||
const oracleRoot = await createTestTempDirectory(
|
const oracleRoot = await createTestTempDirectory(
|
||||||
"rea-webpack-export-oracle-",
|
"rea-webpack-export-oracle-",
|
||||||
);
|
);
|
||||||
const oraclePath = join(oracleRoot, "oracle.cjs");
|
|
||||||
await writeFile(oraclePath, WEBPACK_FACTORY_BODY);
|
|
||||||
const actual = requireFixture(oraclePath) as Record<string, unknown>;
|
|
||||||
expect(Reflect.ownKeys(actual).sort()).toEqual([
|
expect(Reflect.ownKeys(actual).sort()).toEqual([
|
||||||
"",
|
"",
|
||||||
"dot.key",
|
"dot.key",
|
||||||
|
|||||||
Reference in new issue
Block a user