options.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getParserOptions = getParserOptions;
  6. exports.getJestSWCOptions = getJestSWCOptions;
  7. exports.getLoaderSWCOptions = getLoaderSWCOptions;
  8. const nextDistPath = /(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/;
  9. const regeneratorRuntimePath = require.resolve("next/dist/compiled/regenerator-runtime");
  10. function getParserOptions({ filename , jsConfig , ...rest }) {
  11. var ref;
  12. const isTSFile = filename.endsWith(".ts");
  13. const isTypeScript = isTSFile || filename.endsWith(".tsx");
  14. const enableDecorators = Boolean(jsConfig == null ? void 0 : (ref = jsConfig.compilerOptions) == null ? void 0 : ref.experimentalDecorators);
  15. return {
  16. ...rest,
  17. syntax: isTypeScript ? "typescript" : "ecmascript",
  18. dynamicImport: true,
  19. decorators: enableDecorators,
  20. // Exclude regular TypeScript files from React transformation to prevent e.g. generic parameters and angle-bracket type assertion from being interpreted as JSX tags.
  21. [isTypeScript ? "tsx" : "jsx"]: !isTSFile,
  22. importAssertions: true
  23. };
  24. }
  25. function getBaseSWCOptions({ filename , jest , development , hasReactRefresh , globalWindow , nextConfig , resolvedBaseUrl , jsConfig , swcCacheDir , isServerLayer , }) {
  26. var ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, ref10, ref11;
  27. const parserConfig = getParserOptions({
  28. filename,
  29. jsConfig
  30. });
  31. const paths = jsConfig == null ? void 0 : (ref = jsConfig.compilerOptions) == null ? void 0 : ref.paths;
  32. const enableDecorators = Boolean(jsConfig == null ? void 0 : (ref1 = jsConfig.compilerOptions) == null ? void 0 : ref1.experimentalDecorators);
  33. const emitDecoratorMetadata = Boolean(jsConfig == null ? void 0 : (ref2 = jsConfig.compilerOptions) == null ? void 0 : ref2.emitDecoratorMetadata);
  34. const useDefineForClassFields = Boolean(jsConfig == null ? void 0 : (ref3 = jsConfig.compilerOptions) == null ? void 0 : ref3.useDefineForClassFields);
  35. var ref12;
  36. const plugins = ((ref12 = nextConfig == null ? void 0 : (ref4 = nextConfig.experimental) == null ? void 0 : ref4.swcPlugins) != null ? ref12 : []).filter(Array.isArray).map(([name, options])=>[
  37. require.resolve(name),
  38. options
  39. ]);
  40. var ref13;
  41. return {
  42. jsc: {
  43. ...resolvedBaseUrl && paths ? {
  44. baseUrl: resolvedBaseUrl,
  45. paths
  46. } : {},
  47. externalHelpers: !process.versions.pnp && !jest,
  48. parser: parserConfig,
  49. experimental: {
  50. keepImportAssertions: true,
  51. plugins,
  52. cacheRoot: swcCacheDir
  53. },
  54. transform: {
  55. // Enables https://github.com/swc-project/swc/blob/0359deb4841be743d73db4536d4a22ac797d7f65/crates/swc_ecma_ext_transforms/src/jest.rs
  56. ...jest ? {
  57. hidden: {
  58. jest: true
  59. }
  60. } : {},
  61. legacyDecorator: enableDecorators,
  62. decoratorMetadata: emitDecoratorMetadata,
  63. useDefineForClassFields: useDefineForClassFields,
  64. react: {
  65. importSource: (ref13 = jsConfig == null ? void 0 : (ref5 = jsConfig.compilerOptions) == null ? void 0 : ref5.jsxImportSource) != null ? ref13 : (nextConfig == null ? void 0 : (ref6 = nextConfig.compiler) == null ? void 0 : ref6.emotion) ? "@emotion/react" : "react",
  66. runtime: "automatic",
  67. pragma: "React.createElement",
  68. pragmaFrag: "React.Fragment",
  69. throwIfNamespace: true,
  70. development: !!development,
  71. useBuiltins: true,
  72. refresh: !!hasReactRefresh
  73. },
  74. optimizer: {
  75. simplify: false,
  76. globals: jest ? null : {
  77. typeofs: {
  78. window: globalWindow ? "object" : "undefined"
  79. },
  80. envs: {
  81. NODE_ENV: development ? '"development"' : '"production"'
  82. }
  83. }
  84. },
  85. regenerator: {
  86. importPath: regeneratorRuntimePath
  87. }
  88. }
  89. },
  90. sourceMaps: jest ? "inline" : undefined,
  91. styledComponents: getStyledComponentsOptions(nextConfig, development),
  92. removeConsole: nextConfig == null ? void 0 : (ref7 = nextConfig.compiler) == null ? void 0 : ref7.removeConsole,
  93. // disable "reactRemoveProperties" when "jest" is true
  94. // otherwise the setting from next.config.js will be used
  95. reactRemoveProperties: jest ? false : nextConfig == null ? void 0 : (ref8 = nextConfig.compiler) == null ? void 0 : ref8.reactRemoveProperties,
  96. modularizeImports: nextConfig == null ? void 0 : (ref9 = nextConfig.experimental) == null ? void 0 : ref9.modularizeImports,
  97. relay: nextConfig == null ? void 0 : (ref10 = nextConfig.compiler) == null ? void 0 : ref10.relay,
  98. emotion: getEmotionOptions(nextConfig, development),
  99. serverComponents: (nextConfig == null ? void 0 : (ref11 = nextConfig.experimental) == null ? void 0 : ref11.serverComponents) ? {
  100. isServer: !!isServerLayer
  101. } : false
  102. };
  103. }
  104. function getStyledComponentsOptions(nextConfig, development) {
  105. var ref;
  106. let styledComponentsOptions = nextConfig == null ? void 0 : (ref = nextConfig.compiler) == null ? void 0 : ref.styledComponents;
  107. if (!styledComponentsOptions) {
  108. return null;
  109. }
  110. var _displayName;
  111. return {
  112. ...styledComponentsOptions,
  113. displayName: (_displayName = styledComponentsOptions.displayName) != null ? _displayName : Boolean(development)
  114. };
  115. }
  116. function getEmotionOptions(nextConfig, development) {
  117. var ref, ref14, ref15, ref16, ref17, ref18, ref19;
  118. if (!(nextConfig == null ? void 0 : (ref = nextConfig.compiler) == null ? void 0 : ref.emotion)) {
  119. return null;
  120. }
  121. let autoLabel = false;
  122. switch(nextConfig == null ? void 0 : (ref14 = nextConfig.compiler) == null ? void 0 : (ref15 = ref14.emotion) == null ? void 0 : ref15.autoLabel){
  123. case "never":
  124. autoLabel = false;
  125. break;
  126. case "always":
  127. autoLabel = true;
  128. break;
  129. case "dev-only":
  130. default:
  131. autoLabel = !!development;
  132. break;
  133. }
  134. var ref20;
  135. return {
  136. enabled: true,
  137. autoLabel,
  138. labelFormat: nextConfig == null ? void 0 : (ref16 = nextConfig.compiler) == null ? void 0 : (ref17 = ref16.emotion) == null ? void 0 : ref17.labelFormat,
  139. sourcemap: development ? (ref20 = nextConfig == null ? void 0 : (ref18 = nextConfig.compiler) == null ? void 0 : (ref19 = ref18.emotion) == null ? void 0 : ref19.sourceMap) != null ? ref20 : true : false
  140. };
  141. }
  142. function getJestSWCOptions({ isServer , filename , esm , nextConfig , jsConfig , pagesDir , }) {
  143. let baseOptions = getBaseSWCOptions({
  144. filename,
  145. jest: true,
  146. development: false,
  147. hasReactRefresh: false,
  148. globalWindow: !isServer,
  149. nextConfig,
  150. jsConfig
  151. });
  152. const isNextDist = nextDistPath.test(filename);
  153. return {
  154. ...baseOptions,
  155. env: {
  156. targets: {
  157. // Targets the current version of Node.js
  158. node: process.versions.node
  159. }
  160. },
  161. module: {
  162. type: esm && !isNextDist ? "es6" : "commonjs"
  163. },
  164. disableNextSsg: true,
  165. disablePageConfig: true,
  166. pagesDir
  167. };
  168. }
  169. function getLoaderSWCOptions({ filename , development , isServer , isServerLayer , pagesDir , isPageFile , hasReactRefresh , nextConfig , jsConfig , supportedBrowsers , swcCacheDir , }) {
  170. let baseOptions = getBaseSWCOptions({
  171. filename,
  172. development,
  173. globalWindow: !isServer,
  174. hasReactRefresh,
  175. nextConfig,
  176. jsConfig,
  177. // resolvedBaseUrl,
  178. swcCacheDir,
  179. isServerLayer
  180. });
  181. const isNextDist = nextDistPath.test(filename);
  182. if (isServer) {
  183. return {
  184. ...baseOptions,
  185. // Disables getStaticProps/getServerSideProps tree shaking on the server compilation for pages
  186. disableNextSsg: true,
  187. disablePageConfig: true,
  188. isDevelopment: development,
  189. isServer,
  190. pagesDir,
  191. isPageFile,
  192. env: {
  193. targets: {
  194. // Targets the current version of Node.js
  195. node: process.versions.node
  196. }
  197. }
  198. };
  199. } else {
  200. // Matches default @babel/preset-env behavior
  201. baseOptions.jsc.target = "es5";
  202. return {
  203. ...baseOptions,
  204. // Ensure Next.js internals are output as commonjs modules
  205. ...isNextDist ? {
  206. module: {
  207. type: "commonjs"
  208. }
  209. } : {},
  210. disableNextSsg: !isPageFile,
  211. isDevelopment: development,
  212. isServer,
  213. pagesDir,
  214. isPageFile,
  215. ...supportedBrowsers && supportedBrowsers.length > 0 ? {
  216. env: {
  217. targets: supportedBrowsers
  218. }
  219. } : {}
  220. };
  221. }
  222. }
  223. //# sourceMappingURL=options.js.map