index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = transformSource;
  6. var _constants = require("../../../../shared/lib/constants");
  7. var _getPageStaticInfo = require("../../../analysis/get-page-static-info");
  8. var _swc = require("../../../swc");
  9. var _getModuleBuildInfo = require("../get-module-build-info");
  10. async function transformSource(source, sourceMap) {
  11. var ref;
  12. // Avoid buffer to be consumed
  13. if (typeof source !== "string") {
  14. throw new Error("Expected source to have been transformed to a string.");
  15. }
  16. const { resourcePath } = this;
  17. const callback = this.async();
  18. const buildInfo = (0, _getModuleBuildInfo).getModuleBuildInfo(this._module);
  19. const swcAST = await (0, _swc).parse(source, {
  20. filename: resourcePath,
  21. isModule: "unknown"
  22. });
  23. const rscType = (0, _getPageStaticInfo).getRSCModuleType(source);
  24. // Assign the RSC meta information to buildInfo.
  25. // Exclude next internal files which are not marked as client files
  26. buildInfo.rsc = {
  27. type: rscType
  28. };
  29. if (((ref = buildInfo.rsc) == null ? void 0 : ref.type) === _constants.RSC_MODULE_TYPES.client) {
  30. return callback(null, source, sourceMap);
  31. }
  32. const isModule = swcAST.type === "Module";
  33. const code = transformServer(source, isModule);
  34. return callback(null, code, sourceMap);
  35. }
  36. function transformServer(source, isESModule) {
  37. return source + (isESModule ? `export const __next_rsc__` : `exports.__next_rsc__`) + ` = { __webpack_require__, server: true }\n`;
  38. }
  39. //# sourceMappingURL=index.js.map