{"version":3,"sources":["../../src/swc/compile.ts"],"sourcesContent":["import slash from \"slash\";\nimport { promises } from \"fs\";\nimport { dirname, relative } from \"path\";\nimport { transformFile, transformFileSync } from \"@swc/core\";\nimport type { Options, Output } from \"@swc/core\";\n\nconst { mkdir, stat, writeFile } = promises;\n\nfunction withSourceMap(\n output: Output,\n options: Options,\n destFile: string,\n destDir: string\n) {\n if (!output.map || options.sourceMaps === \"inline\") {\n return {\n sourceCode: output.code,\n };\n }\n // TODO: remove once fixed in core https://github.com/swc-project/swc/issues/1388\n const sourceMap = JSON.parse(output.map);\n if (options.sourceFileName) {\n sourceMap[\"sources\"][0] = options.sourceFileName;\n }\n if (options.sourceRoot) {\n sourceMap[\"sourceRoot\"] = options.sourceRoot;\n }\n output.map = JSON.stringify(sourceMap);\n\n const sourceMapPath = destFile + \".map\";\n output.code += `\\n//# sourceMappingURL=${slash(\n relative(destDir, sourceMapPath)\n )}`;\n\n return {\n sourceMap: output.map,\n sourceMapPath,\n sourceCode: output.code,\n };\n}\n\nexport async function outputResult(\n output: Output,\n sourceFile: string,\n destFile: string,\n options: Options\n) {\n const destDir = dirname(destFile);\n\n const { sourceMap, sourceMapPath, sourceCode } = withSourceMap(\n output,\n options,\n destFile,\n destDir\n );\n\n await mkdir(destDir, { recursive: true });\n const { mode } = await stat(sourceFile);\n\n if (!sourceMapPath) {\n await writeFile(destFile, sourceCode, { mode });\n } else {\n await Promise.all([\n writeFile(destFile, sourceCode, { mode }),\n writeFile(sourceMapPath, sourceMap!, { mode }),\n ]);\n }\n}\n\nexport async function compile(\n filename: string,\n opts: Options,\n sync: boolean,\n outputPath: string | undefined\n): Promise {\n const options = { ...opts };\n if (outputPath) {\n options.outputPath = outputPath;\n }\n\n try {\n const result = sync\n ? transformFileSync(filename, options)\n : await transformFile(filename, options);\n\n return result;\n } catch (err: any) {\n if (!err.message.includes(\"ignored by .swcrc\")) {\n throw err;\n }\n }\n}\n"],"names":["compile","outputResult","mkdir","stat","writeFile","promises","withSourceMap","output","options","destFile","destDir","map","sourceMaps","sourceCode","code","sourceMap","JSON","parse","sourceFileName","sourceRoot","stringify","sourceMapPath","slash","relative","sourceFile","dirname","recursive","mode","Promise","all","filename","opts","sync","outputPath","result","transformFileSync","transformFile","err","message","includes"],"mappings":";;;;;;;;;;;IAqEsBA,OAAO;eAAPA;;IA5BAC,YAAY;eAAZA;;;8DAzCJ;oBACO;sBACS;sBACe;;;;;;AAGjD,MAAM,EAAEC,KAAK,EAAEC,IAAI,EAAEC,SAAS,EAAE,GAAGC,YAAQ;AAE3C,SAASC,cACLC,MAAc,EACdC,OAAgB,EAChBC,QAAgB,EAChBC,OAAe;IAEf,IAAI,CAACH,OAAOI,GAAG,IAAIH,QAAQI,UAAU,KAAK,UAAU;QAChD,OAAO;YACHC,YAAYN,OAAOO,IAAI;QAC3B;IACJ;IACA,iFAAiF;IACjF,MAAMC,YAAYC,KAAKC,KAAK,CAACV,OAAOI,GAAG;IACvC,IAAIH,QAAQU,cAAc,EAAE;QACxBH,SAAS,CAAC,UAAU,CAAC,EAAE,GAAGP,QAAQU,cAAc;IACpD;IACA,IAAIV,QAAQW,UAAU,EAAE;QACpBJ,SAAS,CAAC,aAAa,GAAGP,QAAQW,UAAU;IAChD;IACAZ,OAAOI,GAAG,GAAGK,KAAKI,SAAS,CAACL;IAE5B,MAAMM,gBAAgBZ,WAAW;IACjCF,OAAOO,IAAI,IAAI,CAAC,uBAAuB,EAAEQ,IAAAA,cAAK,EAC1CC,IAAAA,cAAQ,EAACb,SAASW,gBACpB,CAAC;IAEH,OAAO;QACHN,WAAWR,OAAOI,GAAG;QACrBU;QACAR,YAAYN,OAAOO,IAAI;IAC3B;AACJ;AAEO,eAAeb,aAClBM,MAAc,EACdiB,UAAkB,EAClBf,QAAgB,EAChBD,OAAgB;IAEhB,MAAME,UAAUe,IAAAA,aAAO,EAAChB;IAExB,MAAM,EAAEM,SAAS,EAAEM,aAAa,EAAER,UAAU,EAAE,GAAGP,cAC7CC,QACAC,S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