refactor extend babel options

This commit is contained in:
Nacho Codoñer
2025-05-26 16:16:58 +02:00
parent 606379c20b
commit d983e791e1

View File

@@ -304,22 +304,33 @@ BCp.processOneFileForTarget = function (inputFile, source) {
},
};
this.inferTypeScriptConfig(features, inputFile, cacheOptions.cacheDeps);
// Helper function to setup Babel options
const setupBabelOptions = () => {
this.inferTypeScriptConfig(features, inputFile, cacheOptions.cacheDeps);
var babelOptions = Babel.getDefaultOptions(features);
babelOptions.caller = { name: "meteor", arch };
var babelOptions = Babel.getDefaultOptions(features);
babelOptions.caller = { name: "meteor", arch };
this.inferExtraBabelOptions(inputFile, babelOptions, cacheOptions.cacheDeps);
babelOptions.sourceMaps = true;
const filename = packageName
? `packages/${packageName}/${inputFilePath}`
: inputFilePath;
babelOptions.filename = babelOptions.sourceFileName = filename;
babelOptions.sourceMaps = true;
this.inferExtraBabelOptions(inputFile, babelOptions, cacheOptions.cacheDeps);
if (this.modifyBabelConfig) {
this.modifyBabelConfig(babelOptions, inputFile);
}
return babelOptions;
};
// Define babelOptions at the outer scope so it's available for source map
var babelOptions;
const filename = packageName
? `packages/${packageName}/${inputFilePath}`
: inputFilePath;
babelOptions.filename = babelOptions.sourceFileName = filename;
if (this.modifyBabelConfig) {
this.modifyBabelConfig(babelOptions, inputFile);
}
try {
var result = (() => {
@@ -384,6 +395,8 @@ BCp.processOneFileForTarget = function (inputFile, source) {
arch,
});
}
// Set babelOptions.filename for source map
babelOptions = { filename };
return compilation;
}
@@ -396,7 +409,12 @@ BCp.processOneFileForTarget = function (inputFile, source) {
// Save result in cache
this.writeToSwcCache({ cacheKey, compilation });
usedSwc = true;
// Set babelOptions.filename for source map
babelOptions = { filename };
} else {
// Set up Babel options only when compiling with Babel
babelOptions = setupBabelOptions();
compilation = compileWithBabel(source, babelOptions, cacheOptions);
usedSwc = false;
}
@@ -414,6 +432,10 @@ BCp.processOneFileForTarget = function (inputFile, source) {
} catch (e) {
this._swcIncompatible[cacheKey] = true;
// If SWC fails, fall back to Babel
// Set up Babel options for fallback
babelOptions = setupBabelOptions();
compilation = compileWithBabel(source, babelOptions, cacheOptions);
if (config?.verbose) {
logTranspilation({