next-swc-loader.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = swcLoader;
  6. exports.pitch = pitch;
  7. exports.raw = void 0;
  8. var _swc = require("../../swc");
  9. var _options = require("../../swc/options");
  10. var _path = require("path");
  11. function swcLoader(inputSource, inputSourceMap) {
  12. const loaderSpan = this.currentTraceSpan.traceChild("next-swc-loader");
  13. const callback = this.async();
  14. loaderSpan.traceAsyncFn(()=>loaderTransform.call(this, loaderSpan, inputSource, inputSourceMap)).then(([transformedSource, outputSourceMap])=>{
  15. callback(null, transformedSource, outputSourceMap || inputSourceMap);
  16. }, (err)=>{
  17. callback(err);
  18. });
  19. }
  20. async function loaderTransform(parentTrace, source, inputSourceMap) {
  21. // Make the loader async
  22. const filename = this.resourcePath;
  23. let loaderOptions = this.getOptions() || {};
  24. const { isServer , isServerLayer , pagesDir , hasReactRefresh , nextConfig , jsConfig , supportedBrowsers , swcCacheDir , } = loaderOptions;
  25. const isPageFile = filename.startsWith(pagesDir);
  26. const swcOptions = (0, _options).getLoaderSWCOptions({
  27. pagesDir,
  28. filename,
  29. isServer,
  30. isServerLayer,
  31. isPageFile,
  32. development: this.mode === "development",
  33. hasReactRefresh,
  34. nextConfig,
  35. jsConfig,
  36. supportedBrowsers,
  37. swcCacheDir
  38. });
  39. const programmaticOptions = {
  40. ...swcOptions,
  41. filename,
  42. inputSourceMap: inputSourceMap ? JSON.stringify(inputSourceMap) : undefined,
  43. // Set the default sourcemap behavior based on Webpack's mapping flag,
  44. sourceMaps: this.sourceMap,
  45. inlineSourcesContent: this.sourceMap,
  46. // Ensure that Webpack will get a full absolute path in the sourcemap
  47. // so that it can properly map the module back to its internal cached
  48. // modules.
  49. sourceFileName: filename
  50. };
  51. if (!programmaticOptions.inputSourceMap) {
  52. delete programmaticOptions.inputSourceMap;
  53. }
  54. // auto detect development mode
  55. if (this.mode && programmaticOptions.jsc && programmaticOptions.jsc.transform && programmaticOptions.jsc.transform.react && !Object.prototype.hasOwnProperty.call(programmaticOptions.jsc.transform.react, "development")) {
  56. programmaticOptions.jsc.transform.react.development = this.mode === "development";
  57. }
  58. const swcSpan = parentTrace.traceChild("next-swc-transform");
  59. return swcSpan.traceAsyncFn(()=>(0, _swc).transform(source, programmaticOptions).then((output)=>{
  60. if (output.eliminatedPackages && this.eliminatedPackages) {
  61. for (const pkg of JSON.parse(output.eliminatedPackages)){
  62. this.eliminatedPackages.add(pkg);
  63. }
  64. }
  65. return [
  66. output.code,
  67. output.map ? JSON.parse(output.map) : undefined
  68. ];
  69. }));
  70. }
  71. const EXCLUDED_PATHS = /[\\/](cache[\\/][^\\/]+\.zip[\\/]node_modules|__virtual__)[\\/]/g;
  72. function pitch() {
  73. const callback = this.async();
  74. (async ()=>{
  75. let loaderOptions = this.getOptions() || {};
  76. if (// TODO: investigate swc file reading in PnP mode?
  77. !process.versions.pnp && loaderOptions.fileReading && !EXCLUDED_PATHS.test(this.resourcePath) && this.loaders.length - 1 === this.loaderIndex && (0, _path).isAbsolute(this.resourcePath) && !await (0, _swc).isWasm()) {
  78. const loaderSpan = this.currentTraceSpan.traceChild("next-swc-loader");
  79. this.addDependency(this.resourcePath);
  80. return loaderSpan.traceAsyncFn(()=>loaderTransform.call(this, loaderSpan));
  81. }
  82. })().then((r)=>{
  83. if (r) return callback(null, ...r);
  84. callback();
  85. }, callback);
  86. }
  87. const raw = true;
  88. exports.raw = raw;
  89. //# sourceMappingURL=next-swc-loader.js.map