2021-11-08 17:35:04 +01:00
const nextDistPath =
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/
2021-12-21 16:13:45 +01:00
const regeneratorRuntimePath = require . resolve (
'next/dist/compiled/regenerator-runtime'
)
2021-11-26 15:30:32 +01:00
2022-03-02 18:41:41 +01:00
export function getParserOptions ( { filename , jsConfig , ... rest } ) {
const isTSFile = filename . endsWith ( '.ts' )
const isTypeScript = isTSFile || filename . endsWith ( '.tsx' )
const enableDecorators = Boolean (
jsConfig ? . compilerOptions ? . experimentalDecorators
)
return {
... rest ,
syntax : isTypeScript ? 'typescript' : 'ecmascript' ,
dynamicImport : true ,
decorators : enableDecorators ,
// Exclude regular TypeScript files from React transformation to prevent e.g. generic parameters and angle-bracket type assertion from being interpreted as JSX tags.
[ isTypeScript ? 'tsx' : 'jsx' ] : isTSFile ? false : true ,
2022-03-06 21:41:22 +01:00
importAssertions : true ,
2022-03-02 18:41:41 +01:00
}
}
function getBaseSWCOptions ( {
2021-11-08 17:35:04 +01:00
filename ,
2022-01-05 16:22:22 +01:00
jest ,
2021-11-08 17:35:04 +01:00
development ,
hasReactRefresh ,
globalWindow ,
2021-11-13 18:26:13 +01:00
nextConfig ,
resolvedBaseUrl ,
jsConfig ,
2021-11-08 17:35:04 +01:00
} ) {
2022-03-02 18:41:41 +01:00
const parserConfig = getParserOptions ( { filename , jsConfig } )
2021-11-13 18:26:13 +01:00
const paths = jsConfig ? . compilerOptions ? . paths
const enableDecorators = Boolean (
jsConfig ? . compilerOptions ? . experimentalDecorators
)
2022-02-06 17:48:09 +01:00
const emitDecoratorMetadata = Boolean (
jsConfig ? . compilerOptions ? . emitDecoratorMetadata
)
2021-11-08 17:35:04 +01:00
return {
jsc : {
2021-11-13 18:26:13 +01:00
... ( resolvedBaseUrl && paths
2021-11-11 01:16:15 +01:00
? {
2021-11-13 18:26:13 +01:00
baseUrl : resolvedBaseUrl ,
2021-11-11 01:16:15 +01:00
paths ,
}
: { } ) ,
2022-03-02 18:41:41 +01:00
parser : parserConfig ,
2022-03-06 21:41:22 +01:00
experimental : {
keepImportAssertions : true ,
} ,
2021-11-08 17:35:04 +01:00
transform : {
2022-01-31 17:15:15 +01:00
// Enables https://github.com/swc-project/swc/blob/0359deb4841be743d73db4536d4a22ac797d7f65/crates/swc_ecma_ext_transforms/src/jest.rs
... ( jest
? {
hidden : {
jest : true ,
} ,
}
: { } ) ,
2021-11-13 18:26:13 +01:00
legacyDecorator : enableDecorators ,
2022-02-06 17:48:09 +01:00
decoratorMetadata : emitDecoratorMetadata ,
2021-11-08 17:35:04 +01:00
react : {
2021-11-13 18:26:13 +01:00
importSource : jsConfig ? . compilerOptions ? . jsxImportSource || 'react' ,
2021-11-08 17:35:04 +01:00
runtime : 'automatic' ,
pragma : 'React.createElement' ,
pragmaFrag : 'React.Fragment' ,
throwIfNamespace : true ,
2022-01-27 16:48:09 +01:00
development : ! ! development ,
2021-11-08 17:35:04 +01:00
useBuiltins : true ,
2022-01-27 16:48:09 +01:00
refresh : ! ! hasReactRefresh ,
2021-11-08 17:35:04 +01:00
} ,
optimizer : {
simplify : false ,
2022-01-05 16:22:22 +01:00
globals : jest
? null
: {
typeofs : {
window : globalWindow ? 'object' : 'undefined' ,
} ,
envs : {
NODE _ENV : development ? '"development"' : '"production"' ,
} ,
// TODO: handle process.browser to match babel replacing as well
} ,
2021-11-08 17:35:04 +01:00
} ,
regenerator : {
2021-11-26 15:30:32 +01:00
importPath : regeneratorRuntimePath ,
2021-11-08 17:35:04 +01:00
} ,
} ,
} ,
2022-02-03 18:39:37 +01:00
sourceMaps : jest ? 'inline' : undefined ,
2022-02-10 02:54:28 +01:00
styledComponents : nextConfig ? . compiler ? . styledComponents
2021-11-09 19:24:01 +01:00
? {
displayName : Boolean ( development ) ,
}
: null ,
2022-02-10 02:54:28 +01:00
removeConsole : nextConfig ? . compiler ? . removeConsole ,
reactRemoveProperties : nextConfig ? . compiler ? . reactRemoveProperties ,
relay : nextConfig ? . compiler ? . relay ,
2021-11-08 17:35:04 +01:00
}
}
2021-11-11 01:16:15 +01:00
export function getJestSWCOptions ( {
2021-11-11 16:05:32 +01:00
isServer ,
2021-11-11 01:16:15 +01:00
filename ,
esm ,
2021-11-13 18:26:13 +01:00
nextConfig ,
jsConfig ,
// This is not passed yet as "paths" resolving needs a test first
// resolvedBaseUrl,
2021-11-11 01:16:15 +01:00
} ) {
2021-11-08 17:35:04 +01:00
let baseOptions = getBaseSWCOptions ( {
filename ,
2022-01-05 16:22:22 +01:00
jest : true ,
2021-11-08 17:35:04 +01:00
development : false ,
hasReactRefresh : false ,
2021-11-11 16:05:32 +01:00
globalWindow : ! isServer ,
2021-11-13 18:26:13 +01:00
nextConfig ,
jsConfig ,
// resolvedBaseUrl,
2021-11-08 17:35:04 +01:00
} )
const isNextDist = nextDistPath . test ( filename )
return {
... baseOptions ,
env : {
targets : {
// Targets the current version of Node.js
node : process . versions . node ,
} ,
2022-02-04 18:08:01 +01:00
// we always transpile optional chaining and nullish coalescing
// since it can cause issues with webpack even if the node target
// supports them
include : [
'proposal-optional-chaining' ,
'proposal-nullish-coalescing-operator' ,
] ,
2021-11-08 17:35:04 +01:00
} ,
module : {
type : esm && ! isNextDist ? 'es6' : 'commonjs' ,
} ,
disableNextSsg : true ,
disablePageConfig : true ,
}
}
export function getLoaderSWCOptions ( {
filename ,
development ,
isServer ,
pagesDir ,
isPageFile ,
hasReactRefresh ,
2021-11-13 18:26:13 +01:00
nextConfig ,
jsConfig ,
// This is not passed yet as "paths" resolving is handled by webpack currently.
// resolvedBaseUrl,
2021-11-08 17:35:04 +01:00
} ) {
let baseOptions = getBaseSWCOptions ( {
filename ,
development ,
globalWindow : ! isServer ,
hasReactRefresh ,
2021-11-13 18:26:13 +01:00
nextConfig ,
jsConfig ,
// resolvedBaseUrl,
2021-11-08 17:35:04 +01:00
} )
const isNextDist = nextDistPath . test ( filename )
if ( isServer ) {
return {
... baseOptions ,
// Disables getStaticProps/getServerSideProps tree shaking on the server compilation for pages
disableNextSsg : true ,
disablePageConfig : true ,
isDevelopment : development ,
2021-12-14 11:33:04 +01:00
isServer ,
2021-11-08 17:35:04 +01:00
pagesDir ,
isPageFile ,
env : {
targets : {
// Targets the current version of Node.js
node : process . versions . node ,
} ,
2022-02-04 18:08:01 +01:00
// we always transpile optional chaining and nullish coalescing
// since it can cause issues with webpack even if the node target
// supports them
include : [
'proposal-optional-chaining' ,
'proposal-nullish-coalescing-operator' ,
] ,
2021-11-08 17:35:04 +01:00
} ,
}
} else {
// Matches default @babel/preset-env behavior
baseOptions . jsc . target = 'es5'
return {
... baseOptions ,
// Ensure Next.js internals are output as commonjs modules
... ( isNextDist
? {
module : {
type : 'commonjs' ,
} ,
}
: { } ) ,
disableNextSsg : ! isPageFile ,
isDevelopment : development ,
2021-12-14 11:33:04 +01:00
isServer ,
2021-11-08 17:35:04 +01:00
pagesDir ,
isPageFile ,
}
}
}