in packages/shared-config/src/unPlugins/compilation.ts [76:177]
async transform(source: string, fileId: string) {
const id = formatId(fileId);
const suffix = (['jsx', 'tsx'] as JSXSuffix[]).find(suffix => new RegExp(`\\.${suffix}?$`).test(id));
const programmaticOptions: SwcConfig = {
swcrc: false,
filename: id,
sourceMaps: !!sourceMap,
};
const compileOptions = { rootDir, mode, suffix, fastRefresh, polyfill, enableEnv };
const commonOptions = getJsxTransformOptions(compileOptions);
// auto detect development mode
if (
mode &&
commonOptions?.jsc?.transform?.react &&
!Object.prototype.hasOwnProperty.call(commonOptions.jsc.transform.react, 'development')
) {
commonOptions.jsc.transform.react.development = mode === 'development';
}
merge(programmaticOptions, commonOptions);
if (typeof compilationConfig === 'function') {
merge(programmaticOptions, compilationConfig(source, fileId, compileOptions));
} else if (compilationConfig) {
merge(programmaticOptions, compilationConfig);
}
const swcPlugins = [];
// handle app.tsx and page entries only
if (removeExportExprs) {
if (isRouteEntry(id) || isAppEntry(id)) {
swcPlugins.push([
swcPluginRemoveExport,
removeExportExprs,
]);
}
}
if (keepExports) {
// Make a copy of keepExports, otherwise it will be modified by side effects operation such as push.
const keepList = [...(Array.isArray(keepExports) ? keepExports : keepExports.value)];
const customInlcude = !Array.isArray(keepExports) && keepExports?.include;
let matchRule = false;
if (customInlcude) {
matchRule = customInlcude(id);
} else {
const matchRoute = isRouteEntry(id);
const matchEntry = isAppEntry(id);
if (matchEntry && keepList.indexOf('pageConfig') > -1) {
// when build for pageConfig, should keep default, it equals to getAppConfig
keepList.push('default');
}
matchRule = matchRoute || matchEntry;
}
if (matchRule) {
swcPlugins.push([
swcPluginKeepExport,
keepList,
]);
}
}
if (nodeTransform) {
swcPlugins.push([swcPluginNodeTransform, {}]);
}
if (swcPlugins.length > 0) {
merge(programmaticOptions, {
jsc: {
experimental: {
cacheRoot: cacheDir,
plugins: swcPlugins,
},
},
});
}
try {
const output = await swc.transform(source, programmaticOptions);
const { code } = output;
let { map } = output;
return {
code: await transformImport(
code,
coreJsPath,
),
map,
};
} catch (error) {
// Catch error for unhandled promise rejection.
if (this) {
// Handled by unplugin.
this.error(error);
return { code: null, map: null };
} else {
// Handled by webpack.
throw error;
}
}
},