73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
"use strict";
|
|
|
|
const { transformSync } = require("@babel/core");
|
|
const presetEnv = require("@babel/preset-env");
|
|
|
|
// Firefox 52 implements async functions, but async iteration arrived later.
|
|
// Babel must therefore lower both async-generator declarations and `for await`
|
|
// consumers. Leaving the latter to SWC turns them into a synchronous
|
|
// `value[Symbol.iterator]()` loop and breaks LangGraph streams at runtime.
|
|
const ASYNC_ITERATION_RE =
|
|
/async\s+(?:function\s*)?\*|\bfor\s+await\s*\(/;
|
|
|
|
module.exports = function firefox52LegacyGeneratorLoader(source, inputMap) {
|
|
if (!ASYNC_ITERATION_RE.test(source)) {
|
|
this.callback(null, source, inputMap);
|
|
return;
|
|
}
|
|
|
|
this.cacheable?.(true);
|
|
const common = {
|
|
filename: this.resourcePath,
|
|
babelrc: false,
|
|
configFile: false,
|
|
sourceType: "unambiguous",
|
|
sourceMaps: false,
|
|
compact: false,
|
|
};
|
|
|
|
// Babel's regenerator transform cannot consume several modern expressions
|
|
// inside async generators in one pass. Lower those expressions first while
|
|
// deliberately preserving async/generator syntax, then run the Firefox 52
|
|
// pass. Only 24 LangChain/LangGraph modules currently enter this slow path.
|
|
const normalized = transformSync(source, {
|
|
...common,
|
|
presets: [
|
|
[
|
|
presetEnv,
|
|
{
|
|
targets: { ie: "11" },
|
|
bugfixes: true,
|
|
modules: false,
|
|
useBuiltIns: false,
|
|
exclude: [
|
|
"@babel/plugin-transform-async-generator-functions",
|
|
"@babel/plugin-transform-async-to-generator",
|
|
"@babel/plugin-transform-regenerator",
|
|
// Preserve `for await` for the second pass. Transforming it as a
|
|
// plain `for of` here loses async-iterator semantics permanently.
|
|
"@babel/plugin-transform-for-of",
|
|
],
|
|
},
|
|
],
|
|
],
|
|
});
|
|
|
|
const result = transformSync(normalized?.code ?? source, {
|
|
...common,
|
|
presets: [
|
|
[
|
|
presetEnv,
|
|
{
|
|
targets: { firefox: "52" },
|
|
bugfixes: true,
|
|
modules: false,
|
|
useBuiltIns: false,
|
|
},
|
|
],
|
|
],
|
|
});
|
|
|
|
this.callback(null, result?.code ?? normalized?.code ?? source, inputMap);
|
|
};
|