UNPKG

3.95 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../../build/compiler.ts"],"names":["generateStats","result","stat","errors","warnings","toJson","length","push","closeCompiler","compiler","Promise","resolve","reject","close","err","runCompiler","config","run","statsOrMultiStats","then","reason","toString","stats","reduce"],"mappings":"qEAAA,2DAOA,QAASA,CAAAA,aAAT,CACEC,MADF,CAEEC,IAFF,CAGkB,CAChB,KAAM,CAAEC,MAAF,CAAUC,QAAV,EAAuBF,IAAI,CAACG,MAAL,CAAY,iBAAZ,CAA7B,CACA,GAAIF,MAAM,CAACG,MAAP,CAAgB,CAApB,CAAuB,CACrBL,MAAM,CAACE,MAAP,CAAcI,IAAd,CAAmB,GAAGJ,MAAtB,EACD,CAED,GAAIC,QAAQ,CAACE,MAAT,CAAkB,CAAtB,CAAyB,CACvBL,MAAM,CAACG,QAAP,CAAgBG,IAAhB,CAAqB,GAAGH,QAAxB,EACD,CAED,MAAOH,CAAAA,MAAP,CACD,CAED;AACA;AACA,QAASO,CAAAA,aAAT,CAAuBC,QAAvB,CAA2E,CACzE,MAAO,IAAIC,CAAAA,OAAJ,CAAkB,CAACC,OAAD,CAAUC,MAAV,GAAqB,CAC5C,GAAI,SAAWH,CAAAA,QAAf,CAAyB,CACvB;AACA,MAAOA,CAAAA,QAAQ,CAACI,KAAT,CAAgBC,GAAD,EAAeA,GAAG,CAAGF,MAAM,CAACE,GAAD,CAAT,CAAiBH,OAAO,EAAzD,CAAP,CACD,CAEDA,OAAO,GACR,CAPM,CAAP,CAQD,CAEM,QAASI,CAAAA,WAAT,CACLC,MADK,CAEoB,CACzB,MAAO,IAAIN,CAAAA,OAAJ,CAAY,CAACC,OAAD,CAAUC,MAAV,GAAqB,CACtC,KAAMH,CAAAA,QAAQ,CAAG,qBAAQO,MAAR,CAAjB,CACAP,QAAQ,CAACQ,GAAT,CACE,CACEH,GADF,CAEEI,iBAFF,GAGK,CACHV,aAAa,CAACC,QAAD,CAAb,CAAwBU,IAAxB,CAA6B,IAAM,CACjC,GAAIL,GAAJ,CAAS,CACP,KAAMM,CAAAA,MAAM,CAAGN,GAAH,cAAGA,GAAG,CAAEO,QAAL,EAAf,CACA,GAAID,MAAJ,CAAY,CACV,MAAOT,CAAAA,OAAO,CAAC,CAAER,MAAM,CAAE,CAACiB,MAAD,CAAV,CAAoBhB,QAAQ,CAAE,EAA9B,CAAD,CAAd,CACD,CACD,MAAOQ,CAAAA,MAAM,CAACE,GAAD,CAAb,CACD,CAED,GAAI,SAAWI,CAAAA,iBAAf,CAAkC,CAChC,KAAMjB,CAAAA,MAAsB,CAAGiB,iBAAiB,CAACI,KAAlB,CAAwBC,MAAxB,CAC7BvB,aAD6B,CAE7B,CAAEG,MAAM,CAAE,EAAV,CAAcC,QAAQ,CAAE,EAAxB,CAF6B,CAA/B,CAIA,MAAOO,CAAAA,OAAO,CAACV,MAAD,CAAd,CACD,CAED,KAAMA,CAAAA,MAAM,CAAGD,aAAa,CAC1B,CAAEG,MAAM,CAAE,EAAV,CAAcC,QAAQ,CAAE,EAAxB,CAD0B,CAE1Bc,iBAF0B,CAA5B,CAIA,MAAOP,CAAAA,OAAO,CAACV,MAAD,CAAd,CACD,CAtBD,EAuBD,CA5BH,EA8BD,CAhCM,CAAP,CAiCD","sourcesContent":["import { webpack } from 'next/dist/compiled/webpack/webpack'\n\nexport type CompilerResult = {\n errors: string[]\n warnings: string[]\n}\n\nfunction generateStats(\n result: CompilerResult,\n stat: webpack.Stats\n): CompilerResult {\n const { errors, warnings } = stat.toJson('errors-warnings')\n if (errors.length > 0) {\n result.errors.push(...errors)\n }\n\n if (warnings.length > 0) {\n result.warnings.push(...warnings)\n }\n\n return result\n}\n\n// Webpack 5 requires the compiler to be closed (to save caches)\n// Webpack 4 does not have this close method so in order to be backwards compatible we check if it exists\nfunction closeCompiler(compiler: webpack.Compiler | webpack.MultiCompiler) {\n return new Promise<void>((resolve, reject) => {\n if ('close' in compiler) {\n // @ts-ignore Close only exists on the compiler in webpack 5\n return compiler.close((err: any) => (err ? reject(err) : resolve()))\n }\n\n resolve()\n })\n}\n\nexport function runCompiler(\n config: webpack.Configuration | webpack.Configuration[]\n): Promise<CompilerResult> {\n return new Promise((resolve, reject) => {\n const compiler = webpack(config)\n compiler.run(\n (\n err: Error,\n statsOrMultiStats: { stats: webpack.Stats[] } | webpack.Stats\n ) => {\n closeCompiler(compiler).then(() => {\n if (err) {\n const reason = err?.toString()\n if (reason) {\n return resolve({ errors: [reason], warnings: [] })\n }\n return reject(err)\n }\n\n if ('stats' in statsOrMultiStats) {\n const result: CompilerResult = statsOrMultiStats.stats.reduce(\n generateStats,\n { errors: [], warnings: [] }\n )\n return resolve(result)\n }\n\n const result = generateStats(\n { errors: [], warnings: [] },\n statsOrMultiStats\n )\n return resolve(result)\n })\n }\n )\n })\n}\n"]}
\No newline at end of file