import { SpeedyPlugin } from '@speedy-js/speedy-types'; import type { TransformOptions as BabelTransformOptions } from '@babel/core'; import { isJsExt, resolvePathAndQuery, isJsLoader } from '@speedy-js/speedy-utils'; export const babelPlugin = (options: BabelTransformOptions): SpeedyPlugin => { return { name: 'speedy:babel', apply(compiler) { if (options) { compiler.hooks.transform.tapPromise('babel', async (args) => { const { originalFilePath } = resolvePathAndQuery(args.path); if (isJsExt(originalFilePath) || isJsLoader(args.loader)) { const result = await require('@babel/core').transformAsync(args.code, { filename: originalFilePath, sourceMaps: true, babelrc: false, configFile: false, compact: false, exclude: [/\bcore-js\b/], ...options, }); return { ...args, code: result?.code!, map: result?.map, }; } return args; }); } }, }; };