preset.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = require("path");
  7. const isLoadIntentTest = process.env.NODE_ENV === "test";
  8. const isLoadIntentDevelopment = process.env.NODE_ENV === "development";
  9. // Resolve styled-jsx plugins
  10. function styledJsxOptions(options) {
  11. options = options || {};
  12. options.styleModule = "styled-jsx/style";
  13. if (!Array.isArray(options.plugins)) {
  14. return options;
  15. }
  16. options.plugins = options.plugins.map((plugin)=>{
  17. if (Array.isArray(plugin)) {
  18. const [name, pluginOptions] = plugin;
  19. return [
  20. require.resolve(name),
  21. pluginOptions
  22. ];
  23. }
  24. return require.resolve(plugin);
  25. });
  26. return options;
  27. }
  28. // Taken from https://github.com/babel/babel/commit/d60c5e1736543a6eac4b549553e107a9ba967051#diff-b4beead8ad9195361b4537601cc22532R158
  29. function supportsStaticESM(caller) {
  30. return !!(caller == null ? void 0 : caller.supportsStaticESM);
  31. }
  32. var _default = (api, options = {})=>{
  33. var ref, ref1;
  34. const supportsESM = api.caller(supportsStaticESM);
  35. const isServer = api.caller((caller)=>!!caller && caller.isServer);
  36. const isCallerDevelopment = api.caller((caller)=>{
  37. return caller == null ? void 0 : caller.isDev;
  38. });
  39. // Look at external intent if used without a caller (e.g. via Jest):
  40. const isTest = isCallerDevelopment == null && isLoadIntentTest;
  41. // Look at external intent if used without a caller (e.g. Storybook):
  42. const isDevelopment = isCallerDevelopment === true || isCallerDevelopment == null && isLoadIntentDevelopment;
  43. // Default to production mode if not `test` nor `development`:
  44. const isProduction = !(isTest || isDevelopment);
  45. const isBabelLoader = api.caller((caller)=>!!caller && (caller.name === "babel-loader" || caller.name === "next-babel-turbo-loader"));
  46. const useJsxRuntime = ((ref = options["preset-react"]) == null ? void 0 : ref.runtime) === "automatic" || Boolean(api.caller((caller)=>!!caller && caller.hasJsxRuntime)) && ((ref1 = options["preset-react"]) == null ? void 0 : ref1.runtime) !== "classic";
  47. const presetEnvConfig = {
  48. // In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option
  49. // In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
  50. modules: "auto",
  51. exclude: [
  52. "transform-typeof-symbol"
  53. ],
  54. ...options["preset-env"]
  55. };
  56. // When transpiling for the server or tests, target the current Node version
  57. // if not explicitly specified:
  58. if ((isServer || isTest) && (!presetEnvConfig.targets || !(typeof presetEnvConfig.targets === "object" && "node" in presetEnvConfig.targets))) {
  59. presetEnvConfig.targets = {
  60. // Targets the current process' version of Node. This requires apps be
  61. // built and deployed on the same version of Node.
  62. // This is the same as using "current" but explicit
  63. node: process.versions.node
  64. };
  65. }
  66. return {
  67. sourceType: "unambiguous",
  68. presets: [
  69. [
  70. require("next/dist/compiled/babel/preset-env"),
  71. presetEnvConfig
  72. ],
  73. [
  74. require("next/dist/compiled/babel/preset-react"),
  75. {
  76. // This adds @babel/plugin-transform-react-jsx-source and
  77. // @babel/plugin-transform-react-jsx-self automatically in development
  78. development: isDevelopment || isTest,
  79. ...useJsxRuntime ? {
  80. runtime: "automatic"
  81. } : {
  82. pragma: "__jsx"
  83. },
  84. ...options["preset-react"]
  85. },
  86. ],
  87. [
  88. require("next/dist/compiled/babel/preset-typescript"),
  89. {
  90. allowNamespaces: true,
  91. ...options["preset-typescript"]
  92. },
  93. ],
  94. ],
  95. plugins: [
  96. !useJsxRuntime && [
  97. require("./plugins/jsx-pragma"),
  98. {
  99. // This produces the following injected import for modules containing JSX:
  100. // import React from 'react';
  101. // var __jsx = React.createElement;
  102. module: "react",
  103. importAs: "React",
  104. pragma: "__jsx",
  105. property: "createElement"
  106. },
  107. ],
  108. [
  109. require("./plugins/optimize-hook-destructuring"),
  110. {
  111. // only optimize hook functions imported from React/Preact
  112. lib: true
  113. },
  114. ],
  115. require("next/dist/compiled/babel/plugin-syntax-dynamic-import"),
  116. require("next/dist/compiled/babel/plugin-syntax-import-assertions"),
  117. require("./plugins/react-loadable-plugin"),
  118. [
  119. require("next/dist/compiled/babel/plugin-proposal-class-properties"),
  120. options["class-properties"] || {},
  121. ],
  122. [
  123. require("next/dist/compiled/babel/plugin-proposal-object-rest-spread"),
  124. {
  125. useBuiltIns: true
  126. },
  127. ],
  128. !isServer && [
  129. require("next/dist/compiled/babel/plugin-transform-runtime"),
  130. {
  131. corejs: false,
  132. helpers: true,
  133. regenerator: true,
  134. useESModules: supportsESM && presetEnvConfig.modules !== "commonjs",
  135. absoluteRuntime: isBabelLoader ? (0, _path).dirname(require.resolve("next/dist/compiled/@babel/runtime/package.json")) : undefined,
  136. ...options["transform-runtime"]
  137. },
  138. ],
  139. [
  140. isTest && options["styled-jsx"] && options["styled-jsx"]["babel-test"] ? require("styled-jsx/babel-test") : require("styled-jsx/babel"),
  141. styledJsxOptions(options["styled-jsx"]),
  142. ],
  143. require("./plugins/amp-attributes"),
  144. isProduction && [
  145. require("next/dist/compiled/babel/plugin-transform-react-remove-prop-types"),
  146. {
  147. removeImport: true
  148. },
  149. ],
  150. isServer && require("next/dist/compiled/babel/plugin-syntax-bigint"),
  151. // Always compile numeric separator because the resulting number is
  152. // smaller.
  153. require("next/dist/compiled/babel/plugin-proposal-numeric-separator"),
  154. require("next/dist/compiled/babel/plugin-proposal-export-namespace-from"),
  155. ].filter(Boolean)
  156. };
  157. };
  158. exports.default = _default;
  159. //# sourceMappingURL=preset.js.map